Class: Surus::JSON::HasManyThroughScopeBuilder

Inherits:
AssociationScopeBuilder show all
Defined in:
lib/surus/json/has_many_through_scope_builder.rb

Instance Attribute Summary

Attributes inherited from AssociationScopeBuilder

#association, #outside_scope

Instance Method Summary collapse

Methods inherited from AssociationScopeBuilder

#conditions, #initialize, #order, #outside_class

Constructor Details

This class inherits a constructor from Surus::JSON::AssociationScopeBuilder

Instance Method Details

#association_primary_keyObject



64
65
66
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 64

def association_primary_key
  quote_column_name association.association_primary_key
end

#association_tableObject



60
61
62
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 60

def association_table
  quote_table_name association.klass.table_name
end

#foreign_keyObject



40
41
42
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 40

def foreign_key
  quote_column_name association.foreign_key
end

#join_through_table_foreign_key_on_associationObject



24
25
26
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 24

def join_through_table_foreign_key_on_association
  "JOIN #{through_table} ON #{through_table}.#{through_primary_key}=#{association_table}.#{foreign_key}"
end

#join_through_table_foreign_key_on_throughObject



28
29
30
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 28

def join_through_table_foreign_key_on_through
  "JOIN #{through_table} ON #{through_table}.#{foreign_key}=#{association_table}.#{association_primary_key}"
end

#outside_primary_keyObject



36
37
38
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 36

def outside_primary_key
  quote_column_name outside_class.primary_key
end

#outside_tableObject



32
33
34
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 32

def outside_table
  quote_table_name outside_class.table_name
end

#scopeObject



4
5
6
7
8
9
10
11
12
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 4

def scope
  s = association
    .klass
    .joins(through_table_join)
    .where("#{through_table}.#{through_foreign_key}=#{outside_table}.#{outside_primary_key}")

  s = s.instance_eval(&association.scope) if association.scope
  s
end

#through_association_foreign_keyObject



56
57
58
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 56

def through_association_foreign_key
  quote_column_name through_reflection.association_foreign_key
end

#through_foreign_keyObject



52
53
54
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 52

def through_foreign_key
  quote_column_name through_reflection.foreign_key
end

#through_primary_keyObject



48
49
50
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 48

def through_primary_key
  quote_column_name through_reflection.active_record_primary_key
end

#through_tableObject



44
45
46
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 44

def through_table
  quote_table_name through_reflection.table_name
end

#through_table_joinObject



16
17
18
19
20
21
22
# File 'lib/surus/json/has_many_through_scope_builder.rb', line 16

def through_table_join
  if foreign_key == through_association_foreign_key
    join_through_table_foreign_key_on_association
  else
    join_through_table_foreign_key_on_through
  end
end