File: comment.rb

package info (click to toggle)
ruby-ramaze 2012.12.08-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,060 kB
  • ctags: 1,200
  • sloc: ruby: 10,446; makefile: 8
file content (42 lines) | stat: -rw-r--r-- 894 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
##
# The Comment model is used for creating and managing comments.
#
# @since  26-09-2011
#
class Comment < Sequel::Model
  plugin :timestamps, :create => :created_at, :update => :updated_at

  # A comment can belong to only one post and one user
  many_to_one :post
  many_to_one :user

  ##
  # Validates a comment before saving it to the database.
  #
  # @since  26-09-2011
  #
  def validate
    validates_presence(:comment)

    # Comments can either have user ID or a custom name. The user ID is only set
    # when the user is logged in.
    unless self.user_id
      validates_presence(:username)
    end
  end

  ##
  # Gets the name of the author from either an associated user or the "name"
  # field.
  #
  # @since  26-09-2011
  # @return [String]
  #
  def username
    if user and user.username
      return user.username
    else
      return super
    end
  end
end # Comment