Sha256: f3ce2697af2799a87e235a1d2957954acf29b71fa5f9a0d87a556ab6c33972b6

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

require 'active_model/associations/initialize_extension'
require 'active_model/associations/active_record_reflection'
require 'active_model/associations/autosave_association'
require 'active_model/associations/override_methods'
require 'active_record/associations/builder/has_many_for_active_model'
require 'active_record/associations/has_many_for_active_model_association'
require 'active_support/core_ext/module'

module ActiveModel
  module Associations
    extend ActiveSupport::Concern

    include InitializeExtension
    include AutosaveAssociation
    include ActiveRecordReflection
    include OverrideMethods

    included do
      mattr_accessor :belongs_to_required_by_default, instance_accessor: false
    end

    module ClassMethods
      # define association like ActiveRecord
      def belongs_to(name, scope = nil, options = {})
        reflection = ActiveRecord::Associations::Builder::BelongsTo.build(self, name, scope, options)
        ActiveRecord::Reflection.add_reflection self, name, reflection
      end

      # define association like ActiveRecord
      def has_many(name, scope = nil, options = {}, &extension)
        options.reverse_merge!(active_model: true, target_ids: "#{name.to_s.singularize}_ids")
        if scope.is_a?(Hash)
          options.merge!(scope)
          scope = nil
        end

        reflection = ActiveRecord::Associations::Builder::HasManyForActiveModel.build(self, name, scope, options, &extension)
        ActiveRecord::Reflection.add_reflection self, name, reflection

        mixin = generated_association_methods
        mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
          def #{options[:target_ids]}=(other_ids)
            @#{options[:target_ids]} = other_ids
            association(:#{name}).reset
            association(:#{name}).reset_scope
            @#{options[:target_ids]}
          end
        CODE
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
eac_rails_utils-0.20.0 lib/active_model/associations.rb
activemodel-associations-0.2.0 lib/active_model/associations.rb
activemodel-associations-0.2.0.beta2 lib/active_model/associations.rb
activemodel-associations-0.2.0.beta1 lib/active_model/associations.rb