Module: CommentableModel::CommentableInstanceMethods

Defined in:
lib/commentable_model.rb

Instance Method Summary collapse

Instance Method Details

#has_posts?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/commentable_model.rb', line 34

def has_posts?
  posts.any?
end

#has_replies?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/commentable_model.rb', line 38

def has_replies?
  posts.count > 1
end

#last_pageObject



74
75
76
# File 'lib/commentable_model.rb', line 74

def last_page
  (posts.count.to_f/posts_per_page.to_f).ceil.to_i
end

#notice_creation_of(post) ⇒ Object



91
92
93
# File 'lib/commentable_model.rb', line 91

def notice_creation_of(post)
  set_last_post(post)
end

#notice_destruction_of(post) ⇒ Object



85
86
87
88
89
# File 'lib/commentable_model.rb', line 85

def notice_destruction_of(post)
  if self.respond_to?(:replied_at) && self.replied_at == post.created_at
    set_last_post(self.posts.except(post).last)
  end
end

#other_voice_countObject



62
63
64
# File 'lib/commentable_model.rb', line 62

def other_voice_count
  voice_count - 1
end

#other_voicesObject



58
59
60
# File 'lib/commentable_model.rb', line 58

def other_voices
  voices - [posts.first.reader]
end

#page_for(post) ⇒ Object



78
79
80
81
82
83
# File 'lib/commentable_model.rb', line 78

def page_for(post)
  return nil unless post.holder == self
  return 1 unless paged?
  position = posts.index(post) || posts.count + 1             # new post not always present in cached posts collection
  (position.to_f/posts_per_page.to_f).ceil.to_i
end

#paged?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/commentable_model.rb', line 66

def paged?
  Radiant.config['forum.paginate_posts?'] && posts.count > posts_per_page
end

#posts_per_pageObject



70
71
72
# File 'lib/commentable_model.rb', line 70

def posts_per_page
  (Radiant.config['forum.posts_per_page'] || 25).to_i
end

#repliesObject



42
43
44
# File 'lib/commentable_model.rb', line 42

def replies
  posts.except(posts.first)    # double query but therefore still chainable and paginatable
end

#reply_countObject



46
47
48
# File 'lib/commentable_model.rb', line 46

def reply_count
  posts.count - 1
end

#set_last_post(post = nil) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/commentable_model.rb', line 95

def set_last_post(post=nil)
  if post
    self.replied_by = post.reader if self.respond_to? :replied_by
    self.replied_at = post.created_at if self.respond_to? :replied_at
    self.save if self.changed?
  end
end

#voice_countObject



54
55
56
# File 'lib/commentable_model.rb', line 54

def voice_count
  posts.distinct_readers.count  #nb. actually counting posts from distinct readers
end

#voicesObject



50
51
52
# File 'lib/commentable_model.rb', line 50

def voices
  posts.distinct_readers.map(&:reader)
end