Class: Comments::Model::Comment

Inherits:
Sequel::Model show all
Includes:
Zen::Model::Helper
Defined in:
lib/zen/package/comments/lib/comments/model/comment.rb

Overview

Model for managing and retrieving comments.

Since:

Constant Summary

Constant Summary

Constants included from Zen::Model::Helper

Zen::Model::Helper::NoRegexpSupport

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Zen::Model::Helper

included

Methods inherited from Sequel::Model

pk_hash

Class Method Details

+ (Array) search(query)

Searches for a number of comments based on the given search query. The following fields can be searched:

  • comments.comment
  • comments.email
  • comments.name
  • users.email
  • users.name

Parameters:

  • query (String)

    The search query.

Returns:

  • (Array)

Since:

  • 16-10-2011



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 34

def self.search(query)
  return select_all(:comments) \
    .filter(
      search_column(:comment, query) |
      search_column(:users__email, query) |
      search_column(:comments__email, query) |
      search_column(:comments__name, query) |
      search_column(:users__name, query)
    ) \
    .eager(:user, :comment_status) \
    .left_join(:users, :comments__user_id => :users__id)
end

+ (Hash) status_hash

Returns a hash containing all available statuses for each comment.

Examples:

Comments::Model::Comment.status_hash

Returns:

  • (Hash)

Since:

  • 0.2



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 56

def self.status_hash
  statuses = {}

  ::Comments::Model::CommentStatus.all.each do |status|
    statuses[status.id] = Zen::Language.lang(
      "comments.labels.#{status.name}"
    )
  end

  return statuses
end

Instance Method Details

- (Object) before_save

Hook run before saving an existing comment.

Since:

  • 0.2.6



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 88

def before_save
  [:name, :website, :email, :comment].each do |field|
    got = send(field)

    if !got.nil?
      send("#{field}=", Loofah.fragment(got) \
        .scrub!(:whitewash) \
        .scrub!(:nofollow).to_s)
    end
  end

  # Get the default status of a comment
  if self.comment_status_id.nil?
    self.comment_status_id = ::Comments::Model::CommentStatus[
      :name => 'closed'
    ].id
  end

  super
end

- (String) html

Returns the text of the comment in HTML based on the markup engine used by the section that the comment belongs to.

Returns:

  • (String)

Since:

  • 0.3



193
194
195
196
197
198
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 193

def html
  return Zen::Markup.convert(
    section_entry.section.comment_format,
    comment
  )
end

- (String) status_name

Returns the name of the comment status.

Returns:

  • (String)

Since:

  • 17-10-2011



206
207
208
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 206

def status_name
  return lang("comments.labels.#{comment_status.name}")
end

- (String) summary(with_link = false)

Returns the first 15 characters of a comment, optionally wrapped in a link that points to the form to edit the comment.

Parameters:

  • with_link (TrueClass|FalseClass) (defaults to: false)

    When set to true the comment will be wrapped in an <a> tag.

Returns:

  • (String)

Since:

  • 17-10-2011



175
176
177
178
179
180
181
182
183
184
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 175

def summary(with_link = false)
  _comment = comment || ''
  _comment = _comment[0, 15] + '...'

  if with_link == true
    return ::Comments::Controller::Comments.a(_comment, :edit, id)
  else
    return _comment
  end
end

- (String) user_email

Gets the Email address of the author of the comment.

Returns:

  • (String)

Since:

  • 16-10-2011



131
132
133
134
135
136
137
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 131

def user_email
  if user.nil?
    return email
  else
    return user.email
  end
end

- (String) user_name

Gets the name of the author of the comment. This name is either retrieved from the current comment row or from an associated user object.

Returns:

  • (String)

Since:

  • 16-10-2011



117
118
119
120
121
122
123
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 117

def user_name
  if user.nil?
    return name
  else
    return user.name
  end
end

- (String) user_website(with_link = false)

Gets the website of the author of the comment and optionally creates an anchor tag for it.

Parameters:

  • with_link (TrueClass|FalseClass) (defaults to: false)

    When set to true the website will be returned as an <a> tag.

Returns:

  • (String)

Since:

  • 16-10-2011



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 148

def user_website(with_link = false)
  if user.nil?
    website = website
  else
    website = user.website
  end

  if !website.nil? and !website.empty? and with_link == true
    website = '<a href="%s" title="%s">%s</a>' % [
      website,
      website,
      website
    ]
  end

  return website
end

- (Object) validate

Specify the validation rules for each comment.

Since:

  • 0.1



73
74
75
76
77
78
79
80
81
# File 'lib/zen/package/comments/lib/comments/model/comment.rb', line 73

def validate
  validates_presence([:comment, :section_entry_id])
  validates_integer([:comment_status_id, :section_entry_id])
  validates_max_length(255, [:name, :email, :website])

  if user_id.nil?
    validates_presence([:name, :email])
  end
end