Sha256: 341620bfb7e971341665831d13eb15500574c8e5a8417cf34c046f462ada5c0f

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 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
          association_options = options.extract!(:dependent)
          self.commontable_config = Commontator::CommontableConfig.new(options)
          self.is_commontable = true

          has_one :thread, as: :commontable,
                           class_name: 'Commontator::Thread',
                           dependent: association_options[:dependent]

          validates_presence_of :thread

          prepend ThreadWithCommontator
        end
      end

      module ThreadWithCommontator
        def thread
          @thread ||= super
          return @thread unless @thread.nil?

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

      alias_method :acts_as_commentable, :acts_as_commontable
    end
  end
end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
commontator-5.1.0 lib/commontator/acts_as_commontable.rb