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.



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

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)



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

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

Instance Attribute Details

#_joinObject



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

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

#_onObject

Returns the value of attribute _on.



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

def _on
  @_on
end

#_tableObject

Returns the value of attribute _table.



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

def _table
  @_table
end

Instance Method Details

#[](key) ⇒ Object

See Arel::Table#[]



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

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.



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

def alias(alias_name)
  clone.alias! alias_name
end

#alias!(alias_name) ⇒ Object

:nodoc:



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

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

#find_alias(association, 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.



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

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

  finder = JoinDependency::Finder.new(builder.to_join_dependency)
  finder.find_alias(association._reflection)
end

#innerObject

Instruct the table to be joined with an INNER JOIN.



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

def inner
  clone.inner!
end

#inner!Object

:nodoc:



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

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

#on(node) ⇒ Object

Specify an explicit join.



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

def on(node)
  clone.on! node
end

#on!(node) ⇒ Object

:nodoc:



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

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

#outerObject

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



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

def outer
  clone.outer!
end

#outer!Object

:nodoc:



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

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