Class: BabySqueel::Table

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

Direct Known Subclasses

Relation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arel_table) ⇒ Table



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

def initialize(arel_table)
  @_table = arel_table
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object (private)



105
106
107
# File 'lib/baby_squeel/table.rb', line 105

def method_missing(*args, &block)
  resolver.resolve!(*args, &block) || super
end

Instance Attribute Details

#_joinObject



19
20
21
# File 'lib/baby_squeel/table.rb', line 19

def _join
  @_join ||= Arel::Nodes::InnerJoin
end

#_onObject

Returns the value of attribute _on.



7
8
9
# File 'lib/baby_squeel/table.rb', line 7

def _on
  @_on
end

#_tableObject

Returns the value of attribute _table.



7
8
9
# File 'lib/baby_squeel/table.rb', line 7

def _table
  @_table
end

Instance Method Details

#[](key) ⇒ Object

See Arel::Table#[]



15
16
17
# File 'lib/baby_squeel/table.rb', line 15

def [](key)
  Nodes::Attribute.new(self, key)
end

#_arel(associations = []) ⇒ Object

This method will be invoked by BabySqueel::Nodes::unwrap. When called, there are three possible outcomes:

  1. Join explicitly using an on clause. Just return Arel.

  2. Implicit join without using an outer join. In this case, we’ll just give a hash to Active Record, and join the normal way.

  3. Implicit join using an outer join. In this case, we need to use Polyamorous to build the join. We’ll return a JoinExpression.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/baby_squeel/table.rb', line 83

def _arel(associations = [])
  if _on
    _join.new(_table, Arel::Nodes::On.new(_on))
  elsif associations.any?(&:needs_polyamorous?)
    JoinExpression.new(associations)
  elsif associations.any?
    associations.reverse.inject({}) do |names, assoc|
      { assoc._reflection.name => names }
    end
  end
end

#alias(alias_name) ⇒ Object

Alias a table. This is only possible when joining an association explicitly.



25
26
27
# File 'lib/baby_squeel/table.rb', line 25

def alias(alias_name)
  clone.alias! alias_name
end

#alias!(alias_name) ⇒ Object

:nodoc:



29
30
31
32
# File 'lib/baby_squeel/table.rb', line 29

def alias!(alias_name) # :nodoc:
  self._table = _table.alias(alias_name)
  self
end

#find_alias(associations = []) ⇒ Object

When referencing a joined table, the tables that attributes reference can change (due to aliasing). This method allows BabySqueel::Nodes::Attribute instances to find what their alias will be.



68
69
70
71
72
# File 'lib/baby_squeel/table.rb', line 68

def find_alias(associations = [])
  builder = JoinDependency::Builder.new(_scope.all)
  builder.ensure_associated _arel(associations)
  builder.find_alias(associations)
end

#innerObject

Instruct the table to be joined with an INNER JOIN.



45
46
47
# File 'lib/baby_squeel/table.rb', line 45

def inner
  clone.inner!
end

#inner!Object

:nodoc:



49
50
51
52
# File 'lib/baby_squeel/table.rb', line 49

def inner! # :nodoc:
  self._join = Arel::Nodes::InnerJoin
  self
end

#on(node) ⇒ Object

Specify an explicit join.



55
56
57
# File 'lib/baby_squeel/table.rb', line 55

def on(node)
  clone.on! node
end

#on!(node) ⇒ Object

:nodoc:



59
60
61
62
# File 'lib/baby_squeel/table.rb', line 59

def on!(node) # :nodoc:
  self._on = node
  self
end

#outerObject

Instruct the table to be joined with a LEFT OUTER JOIN.



35
36
37
# File 'lib/baby_squeel/table.rb', line 35

def outer
  clone.outer!
end

#outer!Object

:nodoc:



39
40
41
42
# File 'lib/baby_squeel/table.rb', line 39

def outer! # :nodoc:
  self._join = Arel::Nodes::OuterJoin
  self
end