Sha256: 2605990076ec849a81fd76a835a433aa4d9231b1d87cad630f879f73b4ffaad7

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

class SofaBlog::Comment < ActiveRecord::Base
  
  set_table_name :sofa_blog_comments
  
  # -- Relationships --------------------------------------------------------
  belongs_to :post,
    :counter_cache => :comments_count
    
  # -- Validations ----------------------------------------------------------
  validates :post_id, :presence => true
  validates :content, :name, :email, 
    :presence => true
  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
    
  # -- Callbacks ------------------------------------------------------------
  after_save    :update_approved_comments_counter, :update_comments_counter
  after_destroy :update_approved_comments_counter, :update_comments_counter
    
  # -- Scopes ---------------------------------------------------------------
  scope :approved, where(:is_approved => true)
  
  # -- Instance Methods -----------------------------------------------------
  def approve!
    update_attribute(:is_approved, true)
  end
  
  def disapprove!
    update_attribute(:is_approved, false)
  end
  
protected
  def update_approved_comments_counter
    self.connection.execute("
      UPDATE sofa_blog_posts 
      SET approved_comments_count = #{post.comments.approved.count} 
      WHERE id = #{post.id}
    ")
  end
  
  def update_comments_counter
    self.connection.execute("
      UPDATE sofa_blog_posts
      SET comments_count = #{post.comments.count}
      WHERE id = #{post.id}
    ")
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sofa_blog-0.1.3 app/models/sofa_blog/comment.rb
sofa_blog-0.1.2 app/models/sofa_blog/comment.rb
sofa_blog-0.1.1 app/models/sofa_blog/comment.rb
sofa_blog-0.1.0 app/models/sofa_blog/comment.rb