Class: BatchAgg::AssocMagic
- Inherits:
-
Object
- Object
- BatchAgg::AssocMagic
- Defined in:
- lib/batchagg.rb
Instance Method Summary collapse
-
#initialize(model, outer_table) ⇒ AssocMagic
constructor
A new instance of AssocMagic.
- #method_missing(name, *args) ⇒ Object
- #respond_to_missing?(name) ⇒ Boolean
Constructor Details
#initialize(model, outer_table) ⇒ AssocMagic
Returns a new instance of AssocMagic.
13 14 15 16 |
# File 'lib/batchagg.rb', line 13 def initialize(model, outer_table) @model = model @outer_table = outer_table end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
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 46 47 48 49 50 51 |
# File 'lib/batchagg.rb', line 18 def method_missing(name, *args, &) reflection = @model.reflect_on_association(name) if reflection target = reflection.klass if reflection.through_reflection # has_many :through through = reflection.through_reflection source = reflection.source_reflection final_target = reflection.klass.arel_table through_table = through.klass.arel_table cond1 = if source.macro == :belongs_to through_table[source.foreign_key].eq(final_target[source.association_primary_key]) else final_target[source.foreign_key].eq(through_table[source.active_record_primary_key]) end cond2 = if through.macro == :belongs_to @outer_table[through.foreign_key].eq(through_table[through.association_primary_key]) else through_table[through.foreign_key].eq(@outer_table[through.active_record_primary_key]) end subquery = through_table.project(Arel.sql("1")).where(cond1.and(cond2)) target.where(subquery.exists) elsif reflection.macro == :belongs_to target.where(target.arel_table[reflection.association_primary_key].eq(@outer_table[reflection.foreign_key])) else target.where(target.arel_table[reflection.foreign_key].eq(@outer_table[reflection.active_record_primary_key])) end else super end end |
Instance Method Details
#respond_to_missing?(name) ⇒ Boolean
53 54 55 |
# File 'lib/batchagg.rb', line 53 def respond_to_missing?(name, *) @model.reflect_on_association(name) || super end |