Sha256: 0eae18e5e32400ad681368ef70e5301a916730974ed580a1b302c60ff4ea7d8f

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module ActiveRecord
  module ActsAs
    module Relation
      extend ActiveSupport::Concern

      module ClassMethods
        def acts_as(name, scope = nil, options = {})
          options, scope = scope, nil if Hash === scope
          options = {as: :actable, dependent: :destroy, validate: false, autosave: true}.merge options

          reflections = has_one name, scope, options
          default_scope -> { eager_load(name) }
          validate :actable_must_be_valid

          cattr_reader(:acting_as_reflection) { reflections[name.to_sym] }
          cattr_reader(:acting_as_name) { name.to_s }
          cattr_reader(:acting_as_model) { name.to_s.camelize.constantize }
          class_eval "def acting_as() #{name} || build_#{name} end"

          include ActsAs::InstanceMethods
          extend  ActsAs::Querying
        end

        def acting_as?(other = nil)
          if respond_to? :acting_as_name
            other.nil? || acting_as_name == other.to_s.underscore
          else
            false
          end
        end

        def is_a?(klass)
          super || acting_as?(klass)
        end

        def actable(options = {})
          name = options.delete(:as) || :actable

          belongs_to name, {polymorphic: true, dependent: :delete, autosave: true}.merge(options)

          alias_method :specific, name
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record-acts_as-1.0.0.pre lib/active_record/acts_as/relation.rb