comment.rb
1.32 KB
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
43
44
45
46
47
48
class Comment < ActiveRecord::Base
validates_presence_of :title, :body
belongs_to :article, :counter_cache => true
belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id'
# unauthenticated authors:
validates_presence_of :name, :if => (lambda { |record| !record.email.blank? })
validates_presence_of :email, :if => (lambda { |record| !record.name.blank? })
validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |record| !record.email.blank? })
# require either a recognized author or an external person
validates_presence_of :author_id, :if => (lambda { |rec| rec.name.blank? && rec.email.blank? })
validates_each :name do |rec,attribute,value|
if rec.author_id && (!rec.name.blank? || !rec.email.blank?)
rec.errors.add(:name, _('%{fn} can only be informed for unauthenticated authors'))
end
end
xss_terminate :only => [ :body, :title ]
def author_name
if author
author.name
else
name
end
end
def url
article.url.merge(:anchor => anchor)
end
def anchor
"comment-#{id}"
end
def self.recent(limit = nil)
self.find(:all, :order => 'created_at desc, id desc', :limit => limit)
end
after_save :notify_article
after_destroy :notify_article
def notify_article
article.comments_updated
end
end