Module: Detailed::AssociationScope

Included in:
ActiveRecord::Associations::AssociationScope
Defined in:
lib/detailed.rb

Class Method Summary collapse

Class Method Details

.included(cl) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/detailed.rb', line 107

def self.included cl
  cl.class_eval do
    def maybe_split(table, field, reflection)
      if field.match /\./
        table, field = field.split(/\./, 2)
        table = alias_tracker.aliased_table_for(table, table_alias_for(reflection, self.reflection != reflection))
      end
                
      [table, field]
    end
    
    # Extend add_constraints support for "table.column" notation
    #
    # This gives us power to do eg.:
    #  has_many :frames, foreign_key: "project_frame_details.glass_id"        
    def add_constraints(scope)
      tables = construct_tables
      
      chain.each_with_index do |reflection, i|
        table, foreign_table = tables.shift, tables.first
        
        if reflection.source_macro == :has_and_belongs_to_many
          join_table = tables.shift
                       
          scope = scope.joins(join(
                                   join_table,
                                   table[association_primary_key].
                                  eq(join_table[association_foreign_key])
                                  ))
          
          table, foreign_table = join_table, tables.first
        end
      
        if reflection.source_macro == :belongs_to
          if reflection.options[:polymorphic]
            key = reflection.association_primary_key(self.klass)
          else
            key = reflection.association_primary_key
          end
          
          foreign_key = reflection.foreign_key
        else
          key         = reflection.foreign_key
          foreign_key = reflection.active_record_primary_key
        end
        
        # this is our addition
        table, key = maybe_split(table, key, reflection)
        foreign_table, foreign_key = maybe_split(foreign_table, foreign_key, reflection)
        # end
        
        if reflection == chain.last              
          bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key]
          scope    = scope.where(table[key].eq(bind_val))
          
          if reflection.type
            value    = owner.class.base_class.name
            bind_val = bind scope, table.table_name, reflection.type.to_s, value
            scope    = scope.where(table[reflection.type].eq(bind_val))
          end
        else
          constraint = table[key].eq(foreign_table[foreign_key])
          
          if reflection.type
            type = chain[i + 1].klass.base_class.name
            constraint = constraint.and(table[reflection.type].eq(type))
          end
          
          scope = scope.joins(join(foreign_table, constraint))
        end
        
        # Exclude the scope of the association itself, because that
        # was already merged in the #scope method.
        scope_chain[i].each do |scope_chain_item|
          klass = i == 0 ? self.klass : reflection.klass
          item  = eval_scope(klass, scope_chain_item)
          
          if scope_chain_item == self.reflection.scope
            scope.merge! item.except(:where, :includes)
          end
          
          scope.includes! item.includes_values
          scope.where_values += item.where_values
          scope.order_values |= item.order_values
        end
      end
      
      scope
    end
  end
end