module Torque::PostgreSQL::Associations::AssociationScope

Private Instance Methods

build_bind_param_for_constraint(reflection, value, foreign_key) click to toggle source
# File lib/torque/postgresql/associations/association_scope.rb, line 65
def build_bind_param_for_constraint(reflection, value, foreign_key)
  ::Arel::Nodes::BindParam.new(::ActiveRecord::Relation::QueryAttribute.new(
    foreign_key, value, reflection.klass.attribute_types[foreign_key],
  ))
end
build_id_constraint(reflection, keys, value, bind_param = false) click to toggle source

Trigger the same method on the relation which will build the constraint condition using array logics

# File lib/torque/postgresql/associations/association_scope.rb, line 46
def build_id_constraint(reflection, keys, value, bind_param = false)
  table = reflection.aliased_table
  value = Array.wrap(value).map do |value|
    build_bind_param_for_constraint(reflection, value, keys.foreign_key)
  end if bind_param

  reflection.build_id_constraint(table[keys.key], value)
end
last_chain_scope(scope, reflection, owner) click to toggle source

When the relation is connected through an array, intercept the condition builder and uses an overlap condition building it on build_id_constraint

Calls superclass method
# File lib/torque/postgresql/associations/association_scope.rb, line 19
def last_chain_scope(scope, reflection, owner)
  return super unless reflection.connected_through_array?

  keys = reflection.join_keys
  value = transform_value(owner[keys.foreign_key])
  constraint = build_id_constraint(reflection, keys, value, true)

  scope.where!(constraint)
end
next_chain_scope(scope, reflection, next_reflection) click to toggle source

When the relation is connected through an array, intercept the condition builder and uses an overlap condition building it on build_id_constraint

Calls superclass method
# File lib/torque/postgresql/associations/association_scope.rb, line 32
def next_chain_scope(scope, reflection, next_reflection)
  return super unless reflection.connected_through_array?

  keys = reflection.join_keys
  foreign_table = next_reflection.aliased_table

  value = foreign_table[keys.foreign_key]
  constraint = build_id_constraint(reflection, keys, value)

  scope.joins!(join(foreign_table, constraint))
end
transform_value(value) click to toggle source

For array-like values, it needs to call the method as many times as the array size

# File lib/torque/postgresql/associations/association_scope.rb, line 57
def transform_value(value)
  if value.is_a?(::Enumerable)
    value.map { |v| value_transformation.call(v) }
  else
    value_transformation.call(value)
  end
end