Class: ActiveRecord::Associations::HasManyThroughAssociation

Inherits:
AssociationProxy show all
Defined in:
lib/composite_primary_keys/associations.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from AssociationProxy

#full_columns_equals, #full_keys

Instance Method Details

#construct_conditionsObject



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/composite_primary_keys/associations.rb', line 272

def construct_conditions
  conditions = if @reflection.through_reflection.options[:as]
      "#{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.options[:as]}_id = #{@owner.quoted_id} " + 
      "AND #{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}"
  else
    @finder_sql = full_columns_equals(@reflection.through_reflection.table_name, 
                              @reflection.through_reflection.primary_key_name, @owner.quoted_id)
  end
  conditions << " AND (#{sql_conditions})" if sql_conditions
  
  return conditions
end

#construct_joins(custom_joins = nil) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/composite_primary_keys/associations.rb', line 285

def construct_joins(custom_joins = nil)          
  polymorphic_join = nil
  if @reflection.through_reflection.options[:as] || @reflection.source_reflection.macro == :belongs_to
    reflection_primary_key = @reflection.klass.primary_key
    source_primary_key     = @reflection.source_reflection.primary_key_name
  else
    reflection_primary_key = @reflection.source_reflection.primary_key_name
    source_primary_key     = @reflection.klass.primary_key
    if @reflection.source_reflection.options[:as]
      polymorphic_join = "AND %s.%s = %s" % [
        @reflection.table_name, "#{@reflection.source_reflection.options[:as]}_type",
            @owner.class.quote(@reflection.through_reflection.klass.name)
          ]
        end
      end

      "INNER JOIN %s ON (%s) = (%s) %s #{@reflection.options[:joins]} #{custom_joins}" % [
        @reflection.through_reflection.table_name,
        full_keys(@reflection.table_name, reflection_primary_key),
        full_keys(@reflection.through_reflection.table_name, source_primary_key),
        polymorphic_join
      ]
end

#construct_sqlObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/composite_primary_keys/associations.rb', line 309

def construct_sql
  case
    when @reflection.options[:finder_sql]
      @finder_sql = interpolate_sql(@reflection.options[:finder_sql])

      @finder_sql = "(%s) = (%s)" % [
                      full_keys(@reflection.klass.table_name, @reflection.primary_key_name),
                      @owner.quoted_id
                    ]
      @finder_sql << " AND (#{conditions})" if conditions
  end

  if @reflection.options[:counter_sql]
    @counter_sql = interpolate_sql(@reflection.options[:counter_sql])
  elsif @reflection.options[:finder_sql]
    # replace the SELECT clause with COUNT(*), preserving any hints within /* ... */
    @reflection.options[:counter_sql] = @reflection.options[:finder_sql].sub(/SELECT (\/\*.*?\*\/ )?(.*)\bFROM\b/im) { "SELECT #{$1}COUNT(*) FROM" }
    @counter_sql = interpolate_sql(@reflection.options[:counter_sql])
  else
    @counter_sql = @finder_sql
  end
end