Class: ArOuterJoins::JoinBuilder
- Inherits:
-
Object
- Object
- ArOuterJoins::JoinBuilder
- Defined in:
- lib/ar_outer_joins/join_builder.rb
Defined Under Namespace
Classes: OuterJoinError
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(association) ⇒ JoinBuilder
constructor
A new instance of JoinBuilder.
Constructor Details
#initialize(association) ⇒ JoinBuilder
Returns a new instance of JoinBuilder.
7 8 9 |
# File 'lib/ar_outer_joins/join_builder.rb', line 7 def initialize(association) @association = association end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
5 6 7 |
# File 'lib/ar_outer_joins/join_builder.rb', line 5 def association @association end |
Instance Method Details
#build ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ar_outer_joins/join_builder.rb', line 11 def build if association.is_a? ActiveRecord::Reflection::ThroughReflection [ JoinBuilder.new(association.through_reflection).build, JoinBuilder.new(association.source_reflection).build ].flatten else table = association.active_record.arel_table primary_key = association.active_record.primary_key joined_table = association.klass.arel_table case association.macro when :belongs_to on = Arel::Nodes::On.new(table[association.foreign_key].eq(joined_table[primary_key])) [Arel::Nodes::OuterJoin.new(joined_table, on)] when :has_and_belongs_to_many join_model_table = if association.respond_to?(:join_table) Arel::Table.new(association.join_table) else Arel::Table.new(association.[:join_table]) end joined_primary_key = association.klass.primary_key on1 = Arel::Nodes::On.new(join_model_table[association.foreign_key].eq(table[primary_key])) on2 = Arel::Nodes::On.new(join_model_table[association.association_foreign_key].eq(joined_table[joined_primary_key])) [Arel::Nodes::OuterJoin.new(join_model_table, on1), Arel::Nodes::OuterJoin.new(joined_table, on2)] when :has_many, :has_one on = Arel::Nodes::On.new(joined_table[association.foreign_key].eq(table[primary_key])) [Arel::Nodes::OuterJoin.new(joined_table, on)] else raise OuterJoinError, "don't know what to do with #{association.macro} association" end end end |