class Thoth::Comment
Constants
- CONFIG_SANITIZE
Public Class Methods
recent(page = 1, limit = 10)
click to toggle source
Recently-posted comments (up to limit) sorted in reverse order by creation time.
# File lib/thoth/model/comment.rb, line 68 def self.recent(page = 1, limit = 10) filter(:deleted => false).reverse_order(:created_at).paginate(page, limit) end
Public Instance Methods
body=(body)
click to toggle source
# File lib/thoth/model/comment.rb, line 89 def body=(body) body = body.force_encoding('utf-8') redcloth = RedCloth.new(body, [:filter_styles]) self[:body] = body self[:body_rendered] = insert_breaks(Sanitize.clean(redcloth.to_html( :refs_textile, :block_textile_lists, :inline_textile_link, :inline_textile_code, :glyphs_textile, :inline_textile_span ), CONFIG_SANITIZE)) summary = Sanitize.clean(body[0..128].gsub(/[\r\n]/, ' ')) if summary.length >= 64 summary = summary[0..64] + '...' end self[:summary] = summary end
created_at(format = nil)
click to toggle source
Gets the creation time of this comment. If format is provided, the time will be returned as a formatted String. See Time.strftime for details.
# File lib/thoth/model/comment.rb, line 114 def created_at(format = nil) if new? format ? Time.now.strftime(format) : Time.now else format ? self[:created_at].strftime(format) : self[:created_at] end end
gravatar_url()
click to toggle source
Gets the Gravatar URL for this comment.
# File lib/thoth/model/comment.rb, line 123 def gravatar_url return @gravatar_url if @gravatar_url md5 = Digest::MD5.hexdigest((author_email || author).downcase) default = CGI.escape(Config.site['gravatar']['default']) rating = Config.site['gravatar']['rating'] size = Config.site['gravatar']['size'] @gravatar_url = "http://www.gravatar.com/avatar/#{md5}.jpg?d=#{default}&r=#{rating}&s=#{size}" end
post()
click to toggle source
Gets the post to which this comment is attached.
# File lib/thoth/model/comment.rb, line 135 def post @post ||= Post[post_id] end
relative_url()
click to toggle source
# File lib/thoth/model/comment.rb, line 139 def relative_url new? ? '#' : "#comment-#{id}" end
title=(title)
click to toggle source
# File lib/thoth/model/comment.rb, line 143 def title=(title) self[:title] = title.strip unless title.nil? end
updated_at(format = nil)
click to toggle source
Gets the time this comment was last updated. If format is provided, the time will be returned as a formatted String. See Time.strftime for details.
# File lib/thoth/model/comment.rb, line 149 def updated_at(format = nil) if new? format ? Time.now.strftime(format) : Time.now else format ? self[:updated_at].strftime(format) : self[:updated_at] end end
url()
click to toggle source
URL for this comment.
# File lib/thoth/model/comment.rb, line 158 def url new? ? '#' : post.url + "#comment-#{id}" end
validate()
click to toggle source
# File lib/thoth/model/comment.rb, line 162 def validate validates_presence(:author, :message => 'Please enter your name.') validates_max_length(64, :author, :message => 'Please enter a name under 64 characters.') validates_max_length(255, :author_email, :message => 'Please enter a shorter email address.') validates_max_length(255, :author_url, :message => 'Please enter a shorter URL.') validates_max_length(65536, :body, :message => 'You appear to be writing a novel. Please try to keep it under 64K.') validates_format(/[^\s@]+@[^\s@]+\.[^\s@]+/, :author_email, :message => 'Please enter a valid email address.') validates_format(/^(?:$|https?:\/\/\S+\.\S+)/i, :author_url, :message => 'Please enter a valid URL or leave the URL field blank.') end
Protected Instance Methods
insert_breaks(str, length = 30)
click to toggle source
Inserts <wbr /> tags in long strings without spaces, while being careful not to break HTML tags.
# File lib/thoth/model/comment.rb, line 178 def insert_breaks(str, length = 30) scanner = StringScanner.new(str) char = '' count = 0 in_tag = 0 new_str = '' while char = scanner.getch do case char when '<' in_tag += 1 when '>' in_tag -= 1 in_tag = 0 if in_tag < 0 when /\s/ count = 0 if in_tag == 0 else if in_tag == 0 if count == length new_str << '<wbr />' count = 0 end count += 1 end end new_str << char end return new_str end