Sha256: 2bb8b009b1e13dbad57a3f163c8dafd57134251ad7e3d33ee50bbd2c589a2870

Contents?: true

Size: 1.4 KB

Versions: 30

Compression:

Stored size: 1.4 KB

Contents

module CompositePrimaryKeys
  module CompositeRelation
    include CompositePrimaryKeys::ActiveRecord::Batches
    include CompositePrimaryKeys::ActiveRecord::Calculations
    include CompositePrimaryKeys::ActiveRecord::FinderMethods
    include CompositePrimaryKeys::ActiveRecord::QueryMethods

    def delete(id_or_array)
      # CPK
      if self.composite?
        id_or_array = if id_or_array.is_a?(CompositePrimaryKeys::CompositeKeys)
                        [id_or_array]
                      else
                        Array(id_or_array)
                      end

        id_or_array.each do |id|
          # Is the passed in id actually a record?
          id = id.kind_of?(::ActiveRecord::Base) ? id.id : id
          where(cpk_id_predicate(table, self.primary_key, id)).delete_all
        end
      else
        where(primary_key => id_or_array).delete_all
      end
    end

    def destroy(id_or_array)
      # Without CPK:
      #if id.is_a?(Array)
      #  id.map { |one_id| destroy(one_id) }
      #else
      #  find(id).destroy
      #end

      id_or_array = if id_or_array.kind_of?(CompositePrimaryKeys::CompositeKeys)
        [id_or_array]
      else
        Array(id_or_array)
      end

      id_or_array.each do |id|
        where(cpk_id_predicate(table, self.primary_key, id)).each do |record|
          record.destroy
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
composite_primary_keys-8.1.8 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-8.1.7 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-10.0.5 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-9.0.10 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-10.0.4 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-9.0.9 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-10.0.3 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-11.0.0.rc1 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-11.0.0.beta4 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-11.0.0.beta3 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-11.0.0.beta2 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-11.0.0.beta1 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-10.0.2 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-10.0.1 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-9.0.8 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-10.0.0 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-9.0.7 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-8.1.6 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-9.0.6 lib/composite_primary_keys/composite_relation.rb
composite_primary_keys-9.0.5 lib/composite_primary_keys/composite_relation.rb