Sha256: 52540ebe6493d3bec41dad87217acdc20fe0ce1531241e1e81c648aaad850029

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

module Commontator
  module ActsAsCommontable
    def self.included(base)
      base.class_attribute :is_commontable
      base.is_commontable = false
      base.extend(ClassMethods)
    end
    
    module ClassMethods
      def acts_as_commontable(options = {})
        class_eval do
          has_one :thread, :as => :commontable, :class_name => 'Commontator::Thread', :dependent => :destroy
          has_many :comments, :class_name => 'Commontator::Comment', :through => :thread
          has_many :subscriptions, :class_name => 'Commontator::Subscription', :through => :thread
          
          after_initialize :create_thread, :unless => :thread, :if => :id
          before_validation :build_thread, :unless => :thread
          
          validates_presence_of :thread
          
          cattr_accessor :commontable_config
          self.commontable_config = Commontator::CommontableConfig.new
          self.is_commontable = true
          
          def create_thread
            thread = Commontator::Thread.new
            thread.commontable = self
            thread.save!
            self.reload
          end
        end
      end
      
      alias_method :acts_as_commentable, :acts_as_commontable
      def acts_as_commentable(options = {})
        acts_as_commontable(options)
      end
    end
  end
end

ActiveRecord::Base.send :include, Commontator::ActsAsCommontable

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
commontator-0.3.10 lib/commontator/acts_as_commontable.rb~
commontator-0.2.4 lib/commontator/acts_as_commontable.rb~
commontator-0.2.0 lib/commontator/acts_as_commontable.rb~
commontator-0.1.46 lib/commontator/acts_as_commontable.rb~