Sha256: 50ce5458fc57b6278aa3a6d8f1ea3c7e4ac5377ace0d48f41596928ac44bf4c2

Contents?: true

Size: 1.75 KB

Versions: 17

Compression:

Stored size: 1.75 KB

Contents

module CompositePrimaryKeys
  module Predicates
    def cpk_and_predicate(predicates)
      if predicates.length == 1
        predicates.first
      else
        Arel::Nodes::And.new(predicates)
      end
    end

    def cpk_or_predicate(predicates)
      or_predicate = predicates.map do |predicate|
        ::Arel::Nodes::Grouping.new(predicate)
      end.inject do |memo, node|
        ::Arel::Nodes::Or.new(memo, node)
      end
      ::Arel::Nodes::Grouping.new(or_predicate)
    end

    def cpk_id_predicate(table, keys, values)
      eq_predicates = keys.zip(values).map do |key, value|
        table[key].eq(value)
      end
      cpk_and_predicate(eq_predicates)
    end

    def cpk_join_predicate(table1, key1, table2, key2)
      key1_fields = Array(key1).map {|key| table1[key]}
      key2_fields = Array(key2).map {|key| table2[key]}

      eq_predicates = key1_fields.zip(key2_fields).map do |key_field1, key_field2|
        key_field1.eq(key_field2)
      end
      cpk_and_predicate(eq_predicates)
    end

    def cpk_in_predicate(table, primary_keys, ids)
      and_predicates = ids.map do |id|
        cpk_id_predicate(table, primary_keys, id)
      end
      cpk_or_predicate(and_predicates)
    end
  end
end

ActiveRecord::Associations::AssociationScope.send(:include, CompositePrimaryKeys::Predicates)
ActiveRecord::Associations::JoinDependency::JoinAssociation.send(:include, CompositePrimaryKeys::Predicates)
ActiveRecord::Associations::Preloader::Association.send(:include, CompositePrimaryKeys::Predicates)
ActiveRecord::Associations::HasManyThroughAssociation.send(:include, CompositePrimaryKeys::Predicates)
ActiveRecord::Relation.send(:include, CompositePrimaryKeys::Predicates)
ActiveRecord::PredicateBuilder.send(:extend, CompositePrimaryKeys::Predicates)

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
composite_primary_keys-7.0.15 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-8.1.1 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.14 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-8.1.0 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.13 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-8.0.0 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.12 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.11 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.10 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.9 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.8 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.7 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.6 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.5 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.4 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.3 lib/composite_primary_keys/composite_predicates.rb
composite_primary_keys-7.0.2 lib/composite_primary_keys/composite_predicates.rb