Class: Comments::Model::Comment
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Comments::Model::Comment
- Includes:
- Zen::Model::Helper
- Defined in:
- lib/zen/package/comments/lib/comments/model/comment.rb
Overview
Model for managing and retrieving comments.
Constant Summary
Constant Summary
Constants included from Zen::Model::Helper
Zen::Model::Helper::NoRegexpSupport
Class Method Summary (collapse)
-
+ (Array) search(query)
Searches for a number of comments based on the given search query.
-
+ (Hash) status_hash
Returns a hash containing all available statuses for each comment.
Instance Method Summary (collapse)
-
- (Object) before_save
Hook run before saving an existing comment.
-
- (String) html
Returns the text of the comment in HTML based on the markup engine used by the section that the comment belongs to.
-
- (String) status_name
Returns the name of the comment status.
-
- (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.
-
- (String) user_email
Gets the Email address of the author of the comment.
-
- (String) user_name
Gets the name of the author of the comment.
-
- (String) user_website(with_link = false)
Gets the website of the author of the comment and optionally creates an anchor tag for it.
-
- (Object) validate
Specify the validation rules for each comment.
Methods included from Zen::Model::Helper
Methods inherited from Sequel::Model
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |