Class: Comment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActsAsCommentable::Comment, Rakismet::Model
Defined in:
app/models/comment.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_photo_comments_for(user) ⇒ Object


26
27
28
# File 'app/models/comment.rb', line 26

def self.find_photo_comments_for(user)
  Comment.where("recipient_id = ? AND commentable_type = ?", user.id, 'Photo').order('created_at DESC')
end

.find_recent(options = {:limit => 5}) ⇒ Object


67
68
69
# File 'app/models/comment.rb', line 67

def self.find_recent(options = {:limit => 5})
  where("created_at > '#{14.days.ago.iso8601}'").order("created_at DESC").limit(options[:limit])
end

Instance Method Details

#can_be_deleted_by(person) ⇒ Object


71
72
73
# File 'app/models/comment.rb', line 71

def can_be_deleted_by(person)
  person && (person.admin? || person.id.eql?(user_id) || person.id.eql?(recipient_id) )
end

#check_spamObject


113
114
115
116
117
# File 'app/models/comment.rb', line 113

def check_spam
  if configatron.has_key?(:akismet_key) && self.spam?
    self.role = 'pending'
  end
end

#commentable_nameObject


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/comment.rb', line 43

def commentable_name
  type = self.commentable_type.underscore
  case type
    when 'user'
      commentable.
    when 'post'
      commentable.title
    when 'clipping'
      commentable.description || "Clipping from #{commentable.user.}"
    when 'photo'
      commentable.description || "Photo from #{commentable.user.}"
    else
      commentable.class.to_s.humanize
  end
end

#notify_previous_anonymous_commentersObject


88
89
90
91
92
93
# File 'app/models/comment.rb', line 88

def notify_previous_anonymous_commenters
  anonymous_commenters_emails = commentable.comments.map{|c|  c.author_email if (c.notify_by_email? && !c.user && !c.author_email.eql?(self.author_email) && c.author_email) }.uniq.compact
  anonymous_commenters_emails.each do |email|
    UserNotifier.follow_up_comment_notice_anonymous(email, self).deliver
  end
end

#notify_previous_commentersObject


82
83
84
85
86
# File 'app/models/comment.rb', line 82

def notify_previous_commenters
  previous_commenters_to_notify.each do |commenter|
    UserNotifier.follow_up_comment_notice(commenter, self).deliver if commenter.email
  end
end

#pending?Boolean

Returns:

  • (Boolean)

119
120
121
# File 'app/models/comment.rb', line 119

def pending?
  role.eql?('pending')
end

#previous_commenters_to_notifyObject


30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/comment.rb', line 30

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)
  users = User.includes(:comments_as_author).limit(20)
  users = users.where(:notify_comments => true).where('users.id NOT IN (?)', [user_id, recipient_id.to_i])
  users = users.where(:comments => {
      :commentable_id => commentable_id,
      :commentable_type =>  commentable_type,
      :notify_by_email => true
      })
  users = users.where('comments.created_at > ?', 2.weeks.ago)
end

#send_notificationsObject


95
96
97
98
99
100
101
# File 'app/models/comment.rb', line 95

def send_notifications
  return if commentable.respond_to?(:send_comment_notifications?) && !commentable.send_comment_notifications?
  return if pending?
  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

Returns:

  • (Boolean)

75
76
77
78
79
80
# File 'app/models/comment.rb', line 75

def should_notify_recipient?
  return false unless recipient && recipient.email
  return false if recipient.eql?(user)
  return false unless recipient.notify_comments?
  true
end

#title_for_rssObject


59
60
61
# File 'app/models/comment.rb', line 59

def title_for_rss
  "Comment from #{username}"
end

#token_for(email) ⇒ Object


103
104
105
# File 'app/models/comment.rb', line 103

def token_for(email)
  Digest::SHA1.hexdigest("#{id}--#{email}--#{created_at}")
end

#unsubscribe_notifications(email) ⇒ Object


107
108
109
110
111
# File 'app/models/comment.rb', line 107

def unsubscribe_notifications(email)
  commentable.comments.where(:author_email => email).each do |previous_comment|
    previous_comment.update_attribute :notify_by_email, false
  end
end

#usernameObject


63
64
65
# File 'app/models/comment.rb', line 63

def username
  user ? user. : (author_name.blank? ? 'Anonymous' : author_name)
end