Sha256: 1d02460b384f3724386cb111b522524d3ddbdb7c70a879aae02372a81d3a1af1

Contents?: true

Size: 922 Bytes

Versions: 8

Compression:

Stored size: 922 Bytes

Contents

module CompositePrimaryKeys
  module ActiveRecord
    module Relation
      module InstanceMethods
        def ids_predicate(id)
          predicate = nil

          if id.kind_of?(CompositePrimaryKeys::CompositeKeys)
            id = [id]
          end

          id.each do |composite_id|
            self.primary_keys.zip(composite_id).each do |key, value|
              eq = table[key].eq(value)
              predicate = predicate ? predicate.and(eq) : eq
            end
          end
          predicate
        end

        def delete(id_or_array)
          # CPK
          # where(@klass.primary_key => id_or_array).delete_all
          where(ids_predicate(id_or_array)).delete_all
        end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
composite_primary_keys-3.1.4 lib/composite_primary_keys/relation.rb
composite_primary_keys-3.1.2 lib/composite_primary_keys/relation.rb
composite_primary_keys-3.1.1 lib/composite_primary_keys/relation.rb
composite_primary_keys-3.1.0 lib/composite_primary_keys/relation.rb
composite_primary_keys-3.0.9 lib/composite_primary_keys/relation.rb
composite_primary_keys-3.0.8 lib/composite_primary_keys/relation.rb
composite_primary_keys-3.0.7 lib/composite_primary_keys/relation.rb
composite_primary_keys-3.0.4 lib/composite_primary_keys/relation.rb