Module: AssociateJsonb::Associations::AssociationScope

Defined in:
lib/associate_jsonb/associations/association_scope.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#apply_jsonb_equality(scope, table, jsonb_column, store_key, foreign_key, value, foreign_klass) ⇒ Object

rubocop:enable Metrics/MethodLength, Metrics/AbcSize



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/associate_jsonb/associations/association_scope.rb', line 46

def apply_jsonb_equality(scope, table, jsonb_column, store_key, foreign_key, value, foreign_klass)
  sql_type = type = nil
  begin
    type = foreign_klass.attribute_types[foreign_key.to_s]
    raise "type not found" unless type.present?
    sql_type = foreign_klass.columns_hash[foreign_key.to_s]
    raise "not a column" unless sql_type.present?
    sql_type = sql_type.sql_type
  rescue
    type = ActiveModel::Type::String.new
    sql_type = "text"
  end

  scope.where!(
    Arel::Nodes::SqlCastedEquality.new(
      Arel::Nodes::Jsonb::DashDoubleArrow.new(table, table[jsonb_column], store_key),
      sql_type,
      Arel::Nodes::BindParam.new(
        ActiveRecord::Relation::QueryAttribute.new(
          store_key, value, type
        )
      )
    )
  )
end

#get_chain(reflection, association, tracker) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/associate_jsonb/associations/association_scope.rb', line 8

def get_chain(reflection, association, tracker)
  name = reflection.name
  chain = [ActiveRecord::Reflection::RuntimeReflection.new(reflection, association)]
  reflection.chain.drop(1).each do |refl|
    aliased_table = tracker.aliased_table_for(
      refl.table_name,
      refl.alias_candidate(name),
      refl.klass.type_caster,
      refl.klass.store_column_attribute_tracker
    )
    chain << ActiveRecord::Associations::AssociationScope::ReflectionProxy.new(refl, aliased_table)
  end
  chain
end

#last_chain_scope(scope, owner_reflection, owner) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/associate_jsonb/associations/association_scope.rb', line 24

def last_chain_scope(scope, owner_reflection, owner)
  reflection = owner_reflection.instance_variable_get(:@reflection)
  return super unless reflection&.foreign_store?


  join_keys = owner_reflection.join_keys
  table = owner_reflection.aliased_table
  key = reflection.foreign_store_key || join_keys.key
  value = transform_value(owner[join_keys.foreign_key])

  apply_jsonb_equality(
    scope,
    table,
    reflection.foreign_store_attr,
    key.to_s,
    join_keys.foreign_key,
    value,
    reflection.active_record
  )
end