Class: Surus::JSON::HasAndBelongsToManyScopeBuilder

Inherits:
AssociationScopeBuilder show all
Defined in:
lib/surus/json/has_and_belongs_to_many_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_foreign_keyObject



17
18
19
# File 'lib/surus/json/has_and_belongs_to_many_scope_builder.rb', line 17

def association_foreign_key
  connection.quote_column_name association.association_foreign_key
end

#association_primary_keyObject



29
30
31
# File 'lib/surus/json/has_and_belongs_to_many_scope_builder.rb', line 29

def association_primary_key
  connection.quote_column_name association.association_primary_key
end

#association_tableObject



25
26
27
# File 'lib/surus/json/has_and_belongs_to_many_scope_builder.rb', line 25

def association_table
  connection.quote_table_name association.klass.table_name
end

#foreign_keyObject



21
22
23
# File 'lib/surus/json/has_and_belongs_to_many_scope_builder.rb', line 21

def foreign_key
  connection.quote_column_name association.foreign_key
end

#join_tableObject



13
14
15
# File 'lib/surus/json/has_and_belongs_to_many_scope_builder.rb', line 13

def join_table
  connection.quote_table_name association.join_table
end

#scopeObject



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

def scope
  s = association
    .klass
    .joins("JOIN #{join_table} ON #{join_table}.#{association_foreign_key}=#{association_table}.#{association_primary_key}")
    .where("#{outside_class.quoted_table_name}.#{association_primary_key}=#{join_table}.#{foreign_key}")
  s = s.instance_eval(&association.scope) if association.scope
  s
end