Sha256: 66275710761a1a8902191064ffbb7a089e6a6d925ac1863c8040f4aa39d2d29a

Contents?: true

Size: 1.61 KB

Versions: 31

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true
module ActiveMocker
  class HasMany < Association
    include Queries

    def self.new(collection, options = {})
      return Relation.new(collection) if options[:relation_class].nil?
      super(collection, options)
    end

    def initialize(collection, options = {})
      @relation_class = options[:relation_class]
      @foreign_key    = options[:foreign_key]
      @foreign_id     = options[:foreign_id]
      @source         = options[:source]
      self.class.include "#{@relation_class.name}::Scopes".constantize
      super(collection)
      set_foreign_key
    end

    def set_foreign_key
      collection.each do |item|
        item.send(:write_attribute, foreign_key, foreign_id) if item.respond_to?("#{foreign_key}=")
      end
    end

    # @api private
    attr_reader :relation_class, :foreign_key, :foreign_id, :source

    def build(options = {}, &block)
      new_record = relation_class.new(init_options.merge!(options), &block)

      # @private
      def new_record._belongs_to(collection)
        @belongs_to_collection = collection
      end

      new_record._belongs_to(self)

      # @private
      def new_record.save
        @belongs_to_collection << self
        super
      end

      new_record
    end

    def create(options = {}, &block)
      created_record = relation_class.create(init_options.merge!(options), &block)
      collection << created_record
      created_record
    end

    alias create! create

    # @api private
    def init_options
      { foreign_key => foreign_id }
    end
  end
  module Mock
    # @deprecated
    HasMany = ActiveMocker::HasMany
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
active_mocker-2.6.2 lib/active_mocker/mock/has_many.rb
active_mocker-2.6.1.beta2 lib/active_mocker/mock/has_many.rb
active_mocker-2.6.1.beta lib/active_mocker/mock/has_many.rb
active_mocker-2.6.0 lib/active_mocker/mock/has_many.rb
active_mocker-2.5.4 lib/active_mocker/mock/has_many.rb
active_mocker-2.5.3 lib/active_mocker/mock/has_many.rb
active_mocker-2.5.2 lib/active_mocker/mock/has_many.rb
active_mocker-2.5.1 lib/active_mocker/mock/has_many.rb
active_mocker-2.5.1.pre lib/active_mocker/mock/has_many.rb
active_mocker-2.5.0 lib/active_mocker/mock/has_many.rb
active_mocker-2.4.4 lib/active_mocker/mock/has_many.rb
active_mocker-2.4.3 lib/active_mocker/mock/has_many.rb
active_mocker-2.4.2 lib/active_mocker/mock/has_many.rb
active_mocker-2.4.1 lib/active_mocker/mock/has_many.rb
active_mocker-2.4.0 lib/active_mocker/mock/has_many.rb
active_mocker-2.4.0.pre5 lib/active_mocker/mock/has_many.rb
active_mocker-2.3.4 lib/active_mocker/mock/has_many.rb
active_mocker-2.4.0.pre4 lib/active_mocker/mock/has_many.rb
active_mocker-2.4.0.pre3 lib/active_mocker/mock/has_many.rb
active_mocker-2.4.0.pre2 lib/active_mocker/mock/has_many.rb