Sha256: c389cc48bac1704ca338f7d087e75aaa81dbfe00e5f5ed61ec7c5fc0b63d2fc3
Contents?: true
Size: 1.57 KB
Versions: 32
Compression:
Stored size: 1.57 KB
Contents
module ActiveRecord class PredicateBuilder class PolymorphicArrayHandler # :nodoc: def initialize(predicate_builder) @predicate_builder = predicate_builder end def call(attribute, value) table = value.associated_table queries = value.type_to_ids_mapping.map do |type, ids| { table.association_foreign_type.to_s => type, table.association_foreign_key.to_s => ids } end predicates = queries.map { |query| predicate_builder.build_from_hash(query) } if predicates.size > 1 type_and_ids_predicates = predicates.map { |type_predicate, id_predicate| Arel::Nodes::Grouping.new(type_predicate.and(id_predicate)) } type_and_ids_predicates.inject(&:or) else predicates.first end end protected attr_reader :predicate_builder end class PolymorphicArrayValue # :nodoc: attr_reader :associated_table, :values def initialize(associated_table, values) @associated_table = associated_table @values = values end def type_to_ids_mapping default_hash = Hash.new { |hsh, key| hsh[key] = [] } values.each_with_object(default_hash) { |value, hash| hash[base_class(value).name] << convert_to_id(value) } end private def primary_key(value) associated_table.association_primary_key(base_class(value)) end def base_class(value) value.class.base_class end def convert_to_id(value) value._read_attribute(primary_key(value)) end end end end
Version data entries
32 entries across 32 versions & 4 rubygems