Sha256: 1b88bad10a1abc7d1e3a9049dca2a976b60ec51ff62b9485f6bcb4fe27f5b169

Contents?: true

Size: 1.72 KB

Versions: 6

Compression:

Stored size: 1.72 KB

Contents

module ActiveRecord
  module Associations
    class HasManyAssociation
      def delete_records(records, method)
        if method == :destroy
          records.each { |r| r.destroy }
          update_counter(-records.length) unless inverse_updates_counter_cache?
        else
          if records == :all
            scope = self.scope
          else
            # CPK
            # keys  = records.map { |r| r[reflection.association_primary_key] }
            # scope = scope.where(reflection.association_primary_key => keys)
            table = Arel::Table.new(reflection.table_name)
            and_conditions = records.map do |record|
              eq_conditions = Array(reflection.association_primary_key).map do |name|
                table[name].eq(record[name])
              end
              Arel::Nodes::And.new(eq_conditions)
            end

            condition = and_conditions.shift
            and_conditions.each do |and_condition|
              condition = condition.or(and_condition)
            end

            scope = self.scope.where(condition)
          end

          if method == :delete_all
            update_counter(-scope.delete_all)
          else
            # CPK
            # update_counter(-scope.update_all(reflection.foreign_key => nil))
            updates = Array(reflection.foreign_key).inject(Hash.new) do |hash, name|
              hash[name] = nil
              hash
            end
            update_counter(-scope.update_all(updates))
          end
        end
      end

      def foreign_key_present?
        # CPK
        # owner.attribute_present?(reflection.association_primary_key)
        Array(reflection.association_primary_key).all? {|key| owner.attribute_present?(key)}
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
composite_primary_keys-6.0.8 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-6.0.7 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-6.0.6 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-6.0.5 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-6.0.3 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-6.0.1 lib/composite_primary_keys/associations/has_many_association.rb