Sha256: c59dba0d5d522a6209c77a95326d33e274b28d8907493925f84ec75b9c38556f

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module ActiveRecord::Associations
  class HasManyForActiveModelAssociation < HasManyAssociation
    # remove conditions: owner.new_record?, foreign_key_present? 
    def find_target?
      !loaded? && klass
    end

    # no dependent action
    def null_scope?
      false
    end

    # full replace simplely
    def replace(other_array)
      original_target = load_target.dup
      other_array.each { |val| raise_on_type_mismatch!(val) }
      target_ids = reflection.options[:target_ids]
      owner[target_ids] = other_array.map(&:id)

      old_records = original_target - other_array
      old_records.each do |record|
        @target.delete(record)
      end

      other_array.each do |record|
        if index = @target.index(record)
          @target[index] = record
        else
          @target << record
        end
      end
    end

    # no need transaction
    def concat(*records)
      load_target
      flatten_records = records.flatten
      flatten_records.each { |val| raise_on_type_mismatch!(val) }
      target_ids = reflection.options[:target_ids]
      owner[target_ids] ||= []
      owner[target_ids].concat(flatten_records.map(&:id))

      flatten_records.each do |record|
        if index = @target.index(record)
          @target[index] = record
        else
          @target << record
        end
      end

      target
    end

    private

    def get_records
      return scope.to_a if reflection.scope_chain.any?(&:any?)

      target_ids = reflection.options[:target_ids]
      klass.where(id: owner[target_ids]).to_a
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activemodel-associations-0.0.3 lib/active_record/associations/has_many_for_active_model_association.rb