Class: BabySqueel::Association

Inherits:
Relation show all
Defined in:
lib/baby_squeel/association.rb

Instance Attribute Summary collapse

Attributes inherited from Relation

#_scope

Attributes inherited from Table

#_join, #_on, #_table

Instance Method Summary collapse

Methods inherited from Relation

#association, #sift

Methods inherited from Table

#[], #alias, #alias!, #alias?, #as, #evaluate, #inner, #inner!, #on, #on!, #outer, #outer!

Constructor Details

#initialize(parent, reflection) ⇒ Association

Returns a new instance of Association.



12
13
14
15
16
17
18
19
20
21
# File 'lib/baby_squeel/association.rb', line 12

def initialize(parent, reflection)
  @parent = parent
  @_reflection = reflection

  # In the case of a polymorphic reflection these
  # attributes will be set after calling #of
  unless @_reflection.polymorphic?
    super @_reflection.klass
  end
end

Dynamic Method Handling

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

Instance Attribute Details

#_polymorphic_klassObject

Specifies the model that the polymorphic association should join with



10
11
12
# File 'lib/baby_squeel/association.rb', line 10

def _polymorphic_klass
  @_polymorphic_klass
end

#_reflectionObject (readonly)

An Active Record association reflection



6
7
8
# File 'lib/baby_squeel/association.rb', line 6

def _reflection
  @_reflection
end

Instance Method Details

#!=(other) ⇒ Object



27
28
29
# File 'lib/baby_squeel/association.rb', line 27

def !=(other)
  Nodes.wrap build_where_clause(other).invert.ast
end

#==(other) ⇒ Object



23
24
25
# File 'lib/baby_squeel/association.rb', line 23

def ==(other)
  Nodes.wrap build_where_clause(other).ast
end

#_arel(associations = []) ⇒ Object

Intelligently constructs Arel nodes. There are three outcomes:

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

    Post.joining { author.on(author_id == author.id) }
    
  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.

    Post.joining { author.as('some_alias') }
    
  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.

    Post.joining { author }
    


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/baby_squeel/association.rb', line 85

def _arel(associations = [])
  if _on
    super
  elsif alias?
    raise AssociationAliasingError.new(_reflection.name, _table.right)
  elsif _reflection.polymorphic? && _polymorphic_klass.nil?
    raise PolymorphicNotSpecifiedError.new(_reflection.name)
  else
    @parent._arel([self, *associations])
  end
end

#add_to_tree(hash) ⇒ Object

See Join#add_to_tree.



51
52
53
54
55
56
57
58
59
# File 'lib/baby_squeel/association.rb', line 51

def add_to_tree(hash)
  polyamorous = Polyamorous::Join.new(
    _reflection.name,
    _join,
    _polymorphic_klass
  )

  hash[polyamorous] ||= {}
end

#find_alias(associations = []) ⇒ Object

See BabySqueel::Table#find_alias.



62
63
64
# File 'lib/baby_squeel/association.rb', line 62

def find_alias(associations = [])
  @parent.find_alias([self, *associations])
end

#needs_polyamorous?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/baby_squeel/association.rb', line 46

def needs_polyamorous?
  _join == Arel::Nodes::OuterJoin || _reflection.polymorphic?
end

#of(klass) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/baby_squeel/association.rb', line 31

def of(klass)
  unless _reflection.polymorphic?
    raise PolymorphicSpecificationError.new(_reflection.name, klass)
  end

  clone.of! klass
end

#of!(klass) ⇒ Object



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

def of!(klass)
  self._scope = klass
  self._table = klass.arel_table
  self._polymorphic_klass = klass
  self
end