Class: ActiveRecord::Associations::AssociationScope

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_primary_keys/associations/association_scope.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_bind_values(owner, chain) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/composite_primary_keys/associations/association_scope.rb', line 4

def self.get_bind_values(owner, chain)
  binds = []
  last_reflection = chain.last

  # CPK
  # binds << last_reflection.join_id_for(owner)
  values = last_reflection.join_id_for(owner)
  binds += Array(values)

  if last_reflection.type
    binds << owner.class.polymorphic_name
  end

  chain.each_cons(2).each do |reflection, next_reflection|
    if reflection.type
      binds << next_reflection.klass.polymorphic_name
    end
  end
  binds
end

Instance Method Details

#last_chain_scope(scope, reflection, owner) ⇒ Object



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

def last_chain_scope(scope, reflection, owner)
  key = reflection.join_primary_key
  foreign_key = reflection.join_foreign_key

  table = reflection.aliased_table

  # CPK
  # value = transform_value(owner[foreign_key])
  # scope = apply_scope(scope, table, key, value)
  Array(key).zip(Array(foreign_key)).each do |a_join_key, a_foreign_key|
    value = transform_value(owner[a_foreign_key])
    scope = apply_scope(scope, table, a_join_key, value)
  end

  if reflection.type
    polymorphic_type = transform_value(owner.class.polymorphic_name)
    scope = apply_scope(scope, table, reflection.type, polymorphic_type)
  end

  scope
end

#next_chain_scope(scope, reflection, next_reflection) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/composite_primary_keys/associations/association_scope.rb', line 47

def next_chain_scope(scope, reflection, next_reflection)
  key = reflection.join_primary_key
  foreign_key = reflection.join_foreign_key

  table = reflection.aliased_table
  foreign_table = next_reflection.aliased_table

  # CPK
  # constraint = table[key].eq(foreign_table[foreign_key])
  constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)

  if reflection.type
    value = transform_value(next_reflection.klass.polymorphic_name)
    scope = apply_scope(scope, table, reflection.type, value)
  end

  scope.joins!(join(foreign_table, constraint))
end