Module: Diaspora::Commentable

Included in:
Photo, Post
Defined in:
lib/diaspora/commentable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object


7
8
9
10
11
# File 'lib/diaspora/commentable.rb', line 7

def self.included(model)
  model.instance_eval do
    has_many :comments, :as => :commentable, :dependent => :destroy
  end
end

Instance Method Details

#last_three_commentsArray<Comment>


14
15
16
17
18
19
# File 'lib/diaspora/commentable.rb', line 14

def last_three_comments
  return [] if self.comments_count == 0
  # DO NOT USE .last(3) HERE.  IT WILL FETCH ALL COMMENTS AND RETURN THE LAST THREE
  # INSTEAD OF DOING THE FOLLOWING, AS EXPECTED (THX AR):
  self.comments.order('created_at DESC').limit(3).includes(:author => :profile).reverse
end

#update_comments_counterInteger


22
23
24
25
# File 'lib/diaspora/commentable.rb', line 22

def update_comments_counter
  self.class.where(:id => self.id).
    update_all(:comments_count => self.comments.count)
end