Module: Sequel::Plugins::Comments::InstanceMethods

Defined in:
lib/cortex_reaver/support/comments.rb

Instance Method Summary collapse

Instance Method Details

#before_destroyObject

When we delete a model that has comments, remove the comments too.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cortex_reaver/support/comments.rb', line 16

def before_destroy
  return false if super == false

  comments = self.comments
  remove_all_comments
  comments.each do |comment|
    comment.destroy
  end

  true
end

#parent(refresh = false) ⇒ Object

Returns the parent of a given comment. Caches, pass true to refresh.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cortex_reaver/support/comments.rb', line 46

def parent(refresh = false)
  if refresh or @parent_cache.nil?
    [:comment, :journal, :photograph, :page].each do |p|
      if self.respond_to?(p) and parent = self.send(p)
        # We found an applicable parent.
        @parent_cache = parent
        return parent
      end
    end
    # We didn't find any parent
    nil
  else
    @parent_cache
  end
end

#refresh_comment_countObject

Recalculates the number of comments on this record (and all comments below it, recursively) and saves those values. Returns the comment count on this record.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cortex_reaver/support/comments.rb', line 31

def refresh_comment_count
  count = 0
  comments.each do |comment|
    # Recalculate for sub-comments and sum.
    count += comment.refresh_comment_count + 1
  end
  self[:comment_count] = count
  self.skip_timestamp_update = true

  # Save and return
  self.save
  self[:comment_count]
end

#root_parentObject

Returns the top-level parent of a given comment.



63
64
65
66
67
68
69
# File 'lib/cortex_reaver/support/comments.rb', line 63

def root_parent
  if parent
    parent.root_parent
  else
    self
  end
end