Class: Comment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Comment
- Includes:
- ActsAsCommentable::Comment, Rakismet::Model
- Defined in:
- app/models/comment.rb
Class Method Summary collapse
Instance Method Summary collapse
- #can_be_deleted_by(person) ⇒ Object
- #check_spam ⇒ Object
- #commentable_name ⇒ Object
- #notify_previous_anonymous_commenters ⇒ Object
- #notify_previous_commenters ⇒ Object
- #previous_commenters_to_notify ⇒ Object
- #send_notifications ⇒ Object
- #should_notify_recipient? ⇒ Boolean
- #title_for_rss ⇒ Object
- #token_for(email) ⇒ Object
- #unsubscribe_notifications(email) ⇒ Object
- #username ⇒ Object
Class Method Details
.find_photo_comments_for(user) ⇒ Object
25 26 27 |
# File 'app/models/comment.rb', line 25 def self.find_photo_comments_for(user) Comment.find(:all, :conditions => ["recipient_id = ? AND commentable_type = ?", user.id, 'Photo'], :order => 'created_at DESC') end |
.find_recent(options = {:limit => 5}) ⇒ Object
65 66 67 |
# File 'app/models/comment.rb', line 65 def self.find_recent( = {:limit => 5}) find(:all, :conditions => "created_at > '#{14.days.ago.iso8601}'", :order => "created_at DESC", :limit => [:limit]) end |
Instance Method Details
#can_be_deleted_by(person) ⇒ Object
69 70 71 |
# File 'app/models/comment.rb', line 69 def can_be_deleted_by(person) person && (person.admin? || person.id.eql?(user_id) || person.id.eql?(recipient_id) ) end |
#check_spam ⇒ Object
110 111 112 113 114 |
# File 'app/models/comment.rb', line 110 def check_spam if !configatron..nil? && self.spam? self.errors.add(:base, :comment_spam_error.l) end end |
#commentable_name ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/comment.rb', line 41 def commentable_name type = self.commentable_type.underscore case type when 'user' commentable.login when 'post' commentable.title when 'clipping' commentable.description || "Clipping from #{commentable.user.login}" when 'photo' commentable.description || "Photo from #{commentable.user.login}" else commentable.class.to_s.humanize end end |
#notify_previous_anonymous_commenters ⇒ Object
86 87 88 89 90 91 |
# File 'app/models/comment.rb', line 86 def notify_previous_anonymous_commenters anonymous_commenters_emails = commentable.comments.map{|c| c. if (c.notify_by_email? && !c.user && !c..eql?(self.) && c.) }.uniq.compact anonymous_commenters_emails.each do |email| UserNotifier.follow_up_comment_notice_anonymous(email, self).deliver end end |
#notify_previous_commenters ⇒ Object
80 81 82 83 84 |
# File 'app/models/comment.rb', line 80 def notify_previous_commenters previous_commenters_to_notify.each do |commenter| UserNotifier.follow_up_comment_notice(commenter, self).deliver if commenter.email end end |
#previous_commenters_to_notify ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/models/comment.rb', line 29 def previous_commenters_to_notify # only send a notification on recent comments # limit the number of emails we'll send (or posting will be slooowww) User.find(:all, :conditions => ["users.id NOT IN (?) AND users.notify_comments = ? AND commentable_id = ? AND commentable_type = ? AND comments.notify_by_email = ? AND comments.created_at > ?", [user_id, recipient_id.to_i], true, commentable_id, commentable_type, true, 2.weeks.ago], :include => :comments_as_author, :limit => 20) end |
#send_notifications ⇒ Object
93 94 95 96 97 98 |
# File 'app/models/comment.rb', line 93 def send_notifications return if commentable.respond_to?(:send_comment_notifications?) && !commentable.send_comment_notifications? UserNotifier.comment_notice(self).deliver if should_notify_recipient? self.notify_previous_commenters self.notify_previous_anonymous_commenters if configatron.allow_anonymous_commenting end |
#should_notify_recipient? ⇒ Boolean
73 74 75 76 77 78 |
# File 'app/models/comment.rb', line 73 def should_notify_recipient? return unless recipient && recipient.email return false if recipient.eql?(user) return false unless recipient.notify_comments? true end |
#title_for_rss ⇒ Object
57 58 59 |
# File 'app/models/comment.rb', line 57 def title_for_rss "Comment from #{username}" end |
#token_for(email) ⇒ Object
100 101 102 |
# File 'app/models/comment.rb', line 100 def token_for(email) Digest::SHA1.hexdigest("#{id}--#{email}--#{created_at}") end |
#unsubscribe_notifications(email) ⇒ Object
104 105 106 107 108 |
# File 'app/models/comment.rb', line 104 def unsubscribe_notifications(email) commentable.comments.(email).each do |previous_comment| previous_comment.update_attribute :notify_by_email, false end end |
#username ⇒ Object
61 62 63 |
# File 'app/models/comment.rb', line 61 def username user ? user.login : (.blank? ? 'Anonymous' : ) end |