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

#composite_join_clause, #composite_where_clause, #full_columns_equals, #full_keys

Instance Method Details

#construct_conditionsObject



313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/composite_primary_keys/associations.rb', line 313

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



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/composite_primary_keys/associations.rb', line 326

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 #{@reflection.options[:joins]} #{custom_joins}" % [
        @reflection.through_reflection.table_name,
        composite_join_clause(
          full_keys(@reflection.table_name, reflection_primary_key), 
          full_keys(@reflection.through_reflection.table_name, source_primary_key)),
        polymorphic_join
      ]
end

#construct_sqlObject



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/composite_primary_keys/associations.rb', line 351

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

      @finder_sql << " AND (#{conditions})" if conditions
    when @reflection.options[:as]
      @finder_sql = 
        "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " + 
        "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
      @finder_sql << " AND (#{conditions})" if conditions
    
    else
      @finder_sql = composite_where_clause(
        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