Class: ActiveRecord::Associations::ClassMethods::JoinDependency

Inherits:
Object
  • Object
show all
Defined in:
lib/referrable_joins/active_record_hacks.rb

Instance Method Summary collapse

Constructor Details

#initialize(base, inner_associations, outer_associations, joins) ⇒ JoinDependency

Returns a new instance of JoinDependency.



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/referrable_joins/active_record_hacks.rb', line 233

def initialize(base, inner_associations, outer_associations, joins)
  @joins                 = [JoinBase.new(base, joins)]
  @associations          = {}
  @reflections           = []
  @base_records_hash     = {}
  @base_records_in_order = []
  @table_aliases         = Hash.new { |aliases, table| aliases[table] = 0 }
  @table_aliases[base.table_name] = 1
  build(outer_associations, @joins.first, Arel::OuterJoin)
  build(inner_associations, @joins.first, Arel::InnerJoin)
end

Instance Method Details

#build(associations, parent = nil, join_type = Arel::InnerJoin) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/referrable_joins/active_record_hacks.rb', line 245

def build(associations, parent = nil, join_type = Arel::InnerJoin)
  parent ||= @joins.last
  case associations
  # MONKEYPATCH Added ActiveRecord::ReflectionTable here
  when Symbol, String, ActiveRecord::ReflectionTable
    reflection = parent.reflections[associations.to_s.intern] or
      raise ConfigurationError, "Association named '#{ associations }' was not found; perhaps you misspelled it\?"
    unless join_association = find_join_association(reflection, parent)
      @reflections << reflection
      join_association = build_join_association(reflection, parent)
      # <MONKEYPATCH added this conditional
      if associations.kind_of?(ActiveRecord::ReflectionTable)
        associations.mirror_relation(join_association.relation)
      end 
      # >MONKEYPATCH
      join_association.join_type = join_type
      @joins << join_association
      cache_joined_association(join_association)
    end
    join_association
  when Array
    associations.each do |association|
      build(association, parent, join_type)
    end
  when Hash
    associations.keys.sort{|a,b|a.to_s<=>b.to_s}.each do |name|
      join_association = build(name, parent, join_type)
      build(associations[name], join_association, join_type)
    end
  else
    raise ConfigurationError, associations.inspect
  end
end