Skip to content

Commit

Permalink
Standardise constraint generation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeiemam authored and MaxLap committed Aug 29, 2024
1 parent 7b62db1 commit a21dedd
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/active_record_where_assoc/core_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,14 @@ def self.build_alias_scope_for_recursive_association(reflection, poly_belongs_to
alias_scope = foreign_klass.base_class.unscoped
alias_scope = alias_scope.from("#{table.name} #{ALIAS_TABLE.name}")

Array.wrap(primary_key).each_with_index do |a_primary_key, i|
a_constraint = table[a_primary_key].eq(ALIAS_TABLE[a_primary_key])
the_operation = i == 0 ? :where : :and
alias_scope = alias_scope.send(the_operation, a_constraint)
end

alias_scope
primary_key_constraints =
Array.wrap(primary_key).map do |a_primary_key|
table[a_primary_key].eq(ALIAS_TABLE[a_primary_key])
end

primary_key_constraints.any? ?
alias_scope.where(primary_key_constraints.inject(&:and)) :
alias_scope
end

def self.wrapper_and_join_constraints(record_class, reflection, options = {})
Expand Down Expand Up @@ -430,16 +431,18 @@ def self.wrapper_and_join_constraints(record_class, reflection, options = {})
foreign_table = ALIAS_TABLE
end

constraints = nil
constraint_keys = Array.wrap(key)
constraint_foreign_keys = Array.wrap(foreign_key)
constraint_key_map = constraint_keys.zip(constraint_foreign_keys)

constraint_key_map.each do |primary_and_foreign_keys|
a_primary_key, a_foreign_key = primary_and_foreign_keys
a_constraint = table[a_primary_key].eq(foreign_table[a_foreign_key])
constraints = constraints ? constraints.and(a_constraint) : a_constraint
end
primary_foreign_key_constraints =
constraint_key_map.map do |primary_and_foreign_keys|
a_primary_key, a_foreign_key = primary_and_foreign_keys

table[a_primary_key].eq(foreign_table[a_foreign_key])
end

constraints = primary_foreign_key_constraints.inject(&:and)

if reflection.type
# Handling of the polymorphic has_many/has_one's type column
Expand Down

0 comments on commit a21dedd

Please sign in to comment.