Class: CortexReaver::Comment

Inherits:
Object
  • Object
show all
Includes:
Model::Renderer
Defined in:
lib/cortex_reaver/model/comment.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model::Renderer

#bluecloth, #erubis_filter, #macro, #render, #render_comment, #sanitize_html, #syntax_highlight

Methods included from Ramaze::Helper::Form

#attr_h, #errors_list, #errors_on, #form_for, #form_p, #form_submit

Methods included from Ramaze::Helper::Attachments

#delete_attachment, included

Methods included from Ramaze::Helper::Pages

#page_navigation, #page_navigation_helper, #page_select, #subpage_navigation

Class Method Details

.get(id) ⇒ Object



45
46
47
# File 'lib/cortex_reaver/model/comment.rb', line 45

def self.get(id)
  self[id]
end

.recentObject



49
50
51
# File 'lib/cortex_reaver/model/comment.rb', line 49

def self.recent
  reverse_order(:created_on).limit(16)
end

.urlObject



53
54
55
# File 'lib/cortex_reaver/model/comment.rb', line 53

def self.url
  '/comments'
end

Instance Method Details

#after_createObject

Increment parent comment count WARNING: If we reparent comments as opposed to just posting, this will break.



32
33
34
35
36
37
38
39
40
41
# File 'lib/cortex_reaver/model/comment.rb', line 32

def after_create
  super

  parent = self.parent
  parent.comment_count += 1
  parent.skip_timestamp_update = true
  parent.save

  true
end

#before_destroyObject

Update parent comment counts



18
19
20
21
22
23
24
25
26
27
# File 'lib/cortex_reaver/model/comment.rb', line 18

def before_destroy
  return false unless super

  parent = self.parent
  parent.comment_count -= 1
  parent.skip_timestamp_update = true
  parent.save

  true
end

#to_sObject



57
58
59
# File 'lib/cortex_reaver/model/comment.rb', line 57

def to_s
  'comment ' + id.to_s
end

#urlObject



61
62
63
# File 'lib/cortex_reaver/model/comment.rb', line 61

def url
  root_parent.url + '#comment_' + id.to_s
end

#validateObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cortex_reaver/model/comment.rb', line 65

def validate
  validates_presence :body
  validates_max_length 255, :name, :allow_blank => true 
  validates_max_length 255, :http, :allow_blank => true
  validates_max_length 255, :email, :allow_blank => true
  validates_format(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :email, :allow_blank => true)

  # Ensure comments with an email specified do *not* conflict with another
  # user.
  if (not email.blank?) and User.filter(:email => email).count > 0
    self.errors[:email] << 'conflicts with a registered user'
  end

  # Ensures comments belong to exactly one parent.
  count = 0
  [:page_id, :journal_id, :comment_id, :photograph_id].each do |field|
    unless self[field].blank?
      count += 1
      if count > 1
        self.errors[:comment] << 'has too many kinds of parents'
        break
      end
    end
  end

  if count == 0
    self.errors[:comment] << "doesn't have a parent"
  end
end