Class: Squeel::Adapters::ActiveRecord::JoinAssociation

Inherits:
ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
  • Object
show all
Defined in:
lib/squeel/adapters/active_record/join_association.rb,
lib/squeel/adapters/active_record/3.0/join_association.rb

Instance Method Summary collapse

Constructor Details

#initialize(reflection, join_dependency, parent = nil, polymorphic_class = nil) ⇒ JoinAssociation

Returns a new instance of JoinAssociation.



8
9
10
11
12
13
14
15
16
# File 'lib/squeel/adapters/active_record/join_association.rb', line 8

def initialize(reflection, join_dependency, parent = nil, polymorphic_class = nil)
  if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
    swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
      super(reflection, join_dependency, parent)
    end
  else
    super(reflection, join_dependency, parent)
  end
end

Instance Method Details

#==(other) ⇒ Object



27
28
29
# File 'lib/squeel/adapters/active_record/join_association.rb', line 27

def ==(other)
  super && active_record == other.active_record
end

#association_joinObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/squeel/adapters/active_record/3.0/join_association.rb', line 31

def association_join
  return @join if @Join

  @join = super

  if reflection.macro == :belongs_to && reflection.options[:polymorphic]
    aliased_table = Arel::Table.new(table_name, :as      => @aliased_table_name,
                                                :engine  => arel_engine,
                                                :columns => klass.columns)

    parent_table = Arel::Table.new(parent.table_name, :as      => parent.aliased_table_name,
                                                      :engine  => arel_engine,
                                                      :columns => parent.active_record.columns)

    @join << parent_table[reflection.options[:foreign_type]].eq(reflection.klass.name)
  end

  @join
end

#build_constraint(reflection, table, key, foreign_table, foreign_key) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/squeel/adapters/active_record/join_association.rb', line 31

def build_constraint(reflection, table, key, foreign_table, foreign_key)
  if reflection.options[:polymorphic]
    super.and(
      foreign_table[reflection.foreign_type].eq(reflection.klass.name)
    )
  else
    super
  end
end

#swapping_reflection_klass(reflection, klass) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/squeel/adapters/active_record/join_association.rb', line 18

def swapping_reflection_klass(reflection, klass)
  reflection = reflection.clone
  original_polymorphic = reflection.options.delete(:polymorphic)
  reflection.instance_variable_set(:@klass, klass)
  yield reflection
ensure
  reflection.options[:polymorphic] = original_polymorphic
end