Sha256: 9a6f9b4bbeee2c69b5ab95a3f370df394dc9e3cbcaf1de95b669dcf5c89671cf

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 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
          cattr_accessor :commontable_config
          self.commontable_config = Commontator::CommontableConfig.new(options)
          self.is_commontable = true

          has_one :thread, :as => :commontable,
                           :class_name => 'Commontator::Thread'

          validates_presence_of :thread

          def thread_with_commontator
            @thread ||= thread_without_commontator
            return @thread unless @thread.nil?

            @thread = build_thread
            @thread.save if persisted?
            @thread
          end

          alias_method_chain :thread, :commontator
        end
      end
      
      alias_method :acts_as_commentable, :acts_as_commontable
    end
  end
end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
commontator-4.11.1 lib/commontator/acts_as_commontable.rb
commontator-4.11.0 lib/commontator/acts_as_commontable.rb
commontator-4.10.5 lib/commontator/acts_as_commontable.rb
commontator-4.10.4 lib/commontator/acts_as_commontable.rb
commontator-4.10.3 lib/commontator/acts_as_commontable.rb
commontator-4.10.2 lib/commontator/acts_as_commontable.rb