Sha256: e02d0d76eeb2315d1ada82ab43411181a4926b8cd8f78c71aa6d7b44dce86c11

Contents?: true

Size: 997 Bytes

Versions: 1

Compression:

Stored size: 997 Bytes

Contents

class Comment  
  include Mongoid::Document
  include Mongoid::Timestamps
  
  field :body, :type => String
  
  belongs_to :commentable, :polymorphic => true
  belongs_to :user
  
  validates :body, :presence => { :allow_blank => false, :message => 'required' }
  after_create  :update_comment_count
  after_destroy :cleanup_comment_count
  delegate :username, :to => :user
    
  # Stringify the created_at date.
  def timestamp
    self.created_at.in_time_zone("Eastern Time (US & Canada)").strftime("%m/%d/%Y at %I:%M %P")
  end
  
  private
  
  ##
  # When created increment a comment_count field on the parent 
  # to avoid parent.comments.count calls and the db overhead.
  # 
  def update_comment_count
    self.commentable.inc(:comment_count, 1)
  end
  
  ##
  # Inverse of update_comment_count to ensure comment counts are updated on delete/destroy
  # 
  def cleanup_comment_count
    self.commentable.update_attribute(:comment_count, (self.commentable.comment_count - 1))
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transit-0.0.2 app/models/comment.rb