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



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

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.base_class.name
  end

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

Instance Method Details

#last_chain_scope(scope, table, reflection, owner, association_klass) ⇒ Object



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

def last_chain_scope(scope, table, reflection, owner, association_klass)
  join_keys = reflection.join_keys(association_klass)
  key = join_keys.key
  foreign_key = join_keys.foreign_key

  # CPK
  #value = transform_value(owner[foreign_key])
  #scope = scope.where(table.name => { key => value })
  mappings = Array(key).zip(Array(foreign_key))
  joins = mappings.reduce(Hash.new) do |hash, mapping|
    hash[mapping.first] = transform_value(owner[mapping.last])
    hash
  end
  scope = scope.where(table.name => joins)

  if reflection.type
    polymorphic_type = transform_value(owner.class.base_class.name)
    scope = scope.where(table.name => { reflection.type => polymorphic_type })
  end

  scope
end

#next_chain_scope(scope, table, reflection, association_klass, foreign_table, next_reflection) ⇒ Object



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 49

def next_chain_scope(scope, table, reflection, association_klass, foreign_table, next_reflection)
  join_keys = reflection.join_keys(association_klass)
  key = join_keys.key
  foreign_key = join_keys.foreign_key

  # 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.base_class.name)
    scope = scope.where(table.name => { reflection.type => value })
  end

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