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

Returns a new instance of Table.



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

def initialize(arel_table)
  @_table = arel_table
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



108
109
110
111
# File 'lib/baby_squeel/table.rb', line 108

def method_missing(name, *args, &block)
  return super if !args.empty? || block_given?
  resolve(name) || not_found_error!(name)
end

Instance Attribute Details

#_joinObject



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

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

#_onObject

Returns the value of attribute _on.



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

def _on
  @_on
end

#_tableObject

Returns the value of attribute _table.



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

def _table
  @_table
end

Instance Method Details

#[](key) ⇒ Object

See Arel::Table#[]



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

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.



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

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.



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

def alias(alias_name)
  clone.alias! alias_name
end

#alias!(alias_name) ⇒ Object

:nodoc:



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

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.



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

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.



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

def inner
  clone.inner!
end

#inner!Object

:nodoc:



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

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

#on(node) ⇒ Object

Specify an explicit join.



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

def on(node)
  clone.on! node
end

#on!(node) ⇒ Object

:nodoc:



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

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

#outerObject

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



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

def outer
  clone.outer!
end

#outer!Object

:nodoc:



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

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