Class: Thoth::Comment

Inherits:
Sequel::Model show all
Defined in:
lib/thoth/model/comment.rb

Constant Summary collapse

CONFIG_SANITIZE =
{
  :elements => [
    'a', 'b', 'blockquote', 'br', 'code', 'dd', 'dl', 'dt', 'em', 'i',
    'li', 'ol', 'p', 'pre', 'small', 'strike', 'strong', 'sub', 'sup',
    'u', 'ul'
  ],

  :attributes => {
    'a'          => ['href', 'title'],
    'blockquote' => ['cite'],
    'pre'        => ['class']
  },

  :add_attributes => {'a' => {'rel' => 'nofollow'}},
  :protocols => {'a' => {'href' => ['ftp', 'http', 'https', 'mailto']}}
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.recent(page = 1, limit = 10) ⇒ Object

Recently-posted comments (up to limit) sorted in reverse order by creation time.



68
69
70
# 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

Instance Method Details

#author=(author) ⇒ Object

– Instance Methods ++



76
77
78
# File 'lib/thoth/model/comment.rb', line 76

def author=(author)
  self[:author] = author.strip unless author.nil?
end

#author_email=(email) ⇒ Object



80
81
82
83
# File 'lib/thoth/model/comment.rb', line 80

def author_email=(email)
  @gravatar_url = nil
  self[:author_email] = email.strip unless email.nil?
end

#author_url=(url) ⇒ Object



85
86
87
# File 'lib/thoth/model/comment.rb', line 85

def author_url=(url)
  self[:author_url] = url.strip unless url.nil?
end

#body=(body) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object

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.



114
115
116
117
118
119
120
# 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_urlObject

Gets the Gravatar URL for this comment.



123
124
125
126
127
128
129
130
131
132
# 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

#postObject

Gets the post to which this comment is attached.



135
136
137
# File 'lib/thoth/model/comment.rb', line 135

def post
  @post ||= Post[post_id]
end

#relative_urlObject



139
140
141
# File 'lib/thoth/model/comment.rb', line 139

def relative_url
  new? ? '#' : "#comment-#{id}"
end

#title=(title) ⇒ Object



143
144
145
# File 'lib/thoth/model/comment.rb', line 143

def title=(title)
  self[:title] = title.strip unless title.nil?
end

#updated_at(format = nil) ⇒ Object

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.



149
150
151
152
153
154
155
# 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

#urlObject

URL for this comment.



158
159
160
# File 'lib/thoth/model/comment.rb', line 158

def url
  new? ? '#' : post.url + "#comment-#{id}"
end

#validateObject



162
163
164
165
166
167
168
169
170
171
172
# 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