Sha256: ed9885f19779bfa687518fa582552d7a382d8d9ac38b34aae953f9aec823d20a

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

class BlogComment < ActiveRecord::Base
  
  # -- Relationships --------------------------------------------------------
  
  belongs_to :blog_post, :counter_cache => :comments_count
  
  # -- Validations ----------------------------------------------------------
  
  validates_presence_of   :content
  validates_length_of :email,
    :in         => 6..100,
    :too_short  => 'Your email address needs to be at least 6 characters long.'
  validates_format_of :email,
    :message    => 'The email you entered is not valid.',
    :with       => /^([\w.%-+]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
  
  # -- Scopes ----------------------------------------------------------

  scope :approved, where(:approved => true)
  
  # -- AR Callbacks ---------------------------------------------------------
  
  after_save :update_approved_comments_counter, :update_comments_counter
  after_destroy :update_approved_comments_counter, :update_comments_counter
  
  # -- Instance Methods --------------------------------------------------------
  
  def approve!
    update_attribute(:approved, true)
  end
  
  def disapprove!
    update_attribute(:approved, false)
  end
  
  
protected
  def update_approved_comments_counter
    self.connection.execute("UPDATE blog_posts SET approved_comments_count = #{blog_post.blog_comments.approved.count} WHERE id = #{blog_post.id}")
  end

  def update_comments_counter
    self.connection.execute("UPDATE blog_posts SET comments_count = #{blog_post.blog_comments.count} WHERE id = #{blog_post.id}")
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sofa_blog-0.0.1 app/models/blog_comment.rb