Class: BabySqueel::Association

Inherits:
Table
  • Object
show all
Defined in:
lib/baby_squeel/association.rb

Defined Under Namespace

Classes: AliasingError

Instance Attribute Summary collapse

Attributes inherited from Table

#_join, #_on, #_table

Instance Method Summary collapse

Methods inherited from Table

#[], #alias, #alias!, #association, #inner, #inner!, #on, #on!, #outer, #outer!

Constructor Details

#initialize(parent, reflection) ⇒ Association

Returns a new instance of Association.



18
19
20
21
22
# File 'lib/baby_squeel/association.rb', line 18

def initialize(parent, reflection)
  @parent = parent
  @_reflection = reflection
  super(@_reflection.klass)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BabySqueel::Table

Instance Attribute Details

#_reflectionObject (readonly)

Returns the value of attribute _reflection.



16
17
18
# File 'lib/baby_squeel/association.rb', line 16

def _reflection
  @_reflection
end

Instance Method Details

#_arel(associations = []) ⇒ Object

Intelligently constructs Arel nodes. There are three outcomes:

  1. The user explicitly constructed their join using #on. See BabySqueel::Table#_arel.

  2. The user aliased an implicitly joined association. ActiveRecord’s join dependency gives us no way of handling this, so we have to throw an error.

  3. The user implicitly joined this association, so we pass this association up the tree until it hits the top-level BabySqueel::Table. Once it gets there, Arel join nodes will be constructed.



36
37
38
39
40
41
42
43
44
# File 'lib/baby_squeel/association.rb', line 36

def _arel(associations = [])
  if _on
    super
  elsif _table.is_a? Arel::Nodes::TableAlias
    raise AliasingError.new(_reflection.name, _table.right)
  else
    @parent._arel([self, *associations])
  end
end