Sha256: 2992b727ba63b8ab4b89b2bd6f2ac459d6f4d78764b0c792493cf1b7dc2cf2f0

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

# encoding: utf-8
module Freeberry
  module Models
    module Comment
      def self.included(base)
        base.send :include, InstanceMethods
        base.send :extend,  ClassMethods
      end
      
      module ClassMethods
        def self.extended(base)
          base.class_eval do
            belongs_to :commentable, :polymorphic => true, :counter_cache => true
            belongs_to :author, :polymorphic => true
            
            before_validation :make_author
            
            scope :recently, order("#{quoted_table_name}.created_at DESC")
            scope :siblings_for, lambda { |item| where(["commentable_type = ? AND commentable_id = ?", item.commentable_type, item.commentable_id]) }
            scope :follows, where(:is_follow => true)
          end
        end
      end
      
      module InstanceMethods
        def comments_count
          @comments_count ||= siblings.count
          @comments_count
        end
        
        def siblings
          self.class.siblings_for(self)
        end
    
        protected
    
          def make_author
          	unless author.nil?
            	self.user_email = author.email if author.respond_to?(:email)
            	self.user_name = author.name if author.respond_to?(:name)
          	end
          end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
freeberry-0.3.0 lib/freeberry/models/comment.rb
freeberry-0.2.9 lib/freeberry/models/comment.rb
freeberry-0.2.7 lib/freeberry/models/comment.rb
freeberry-0.2.6 lib/freeberry/models/comment.rb