Sha256: 39087cf6e8ec38f306767d0f1bbddaf11d851dcb5278cbec69ee3a95a12a08e6

Contents?: true

Size: 1.4 KB

Versions: 12

Compression:

Stored size: 1.4 KB

Contents

require 'commontator/commontable_config'

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'
          has_many :comments, :class_name => 'Commontator::Comment', :through => :thread
          has_many :subscriptions, :class_name => 'Commontator::Subscription', :through => :thread
          
          validates_presence_of :thread
          
          cattr_accessor :commontable_config
          self.commontable_config = Commontator::CommontableConfig.new
          self.is_commontable = true
          
          alias_method :thread_raw, :thread
          
          def thread
            raw = thread_raw
            return raw unless raw.nil?
            return Commontator::Thread.find_or_create_by_commontable_id_and_commontable_type(self.id, self.class.name) \
              unless self.id.nil?
            self.thread = Commontator::Thread.new
            self.thread.commontable = self
            self.thread
          end
        end
      end
      
      alias_method :acts_as_commentable, :acts_as_commontable
    end
  end
end

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
commontator-1.1.3 lib/commontator/acts_as_commontable.rb~
commontator-1.1.2 lib/commontator/acts_as_commontable.rb~
commontator-1.1.1 lib/commontator/acts_as_commontable.rb
commontator-1.1.0 lib/commontator/acts_as_commontable.rb
commontator-1.0.6 lib/commontator/acts_as_commontable.rb
commontator-1.0.5 lib/commontator/acts_as_commontable.rb
commontator-1.0.4 lib/commontator/acts_as_commontable.rb
commontator-1.0.3 lib/commontator/acts_as_commontable.rb
commontator-1.0.2 lib/commontator/acts_as_commontable.rb
commontator-1.0.1 lib/commontator/acts_as_commontable.rb
commontator-1.0.0 lib/commontator/acts_as_commontable.rb
commontator-0.5.14 lib/commontator/acts_as_commontable.rb