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.



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)



123
124
125
# File 'lib/baby_squeel/table.rb', line 123

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 Join.



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/baby_squeel/table.rb', line 101

def _arel(associations = [])
  if _on
    _join.new(_table, Arel::Nodes::On.new(_on))
  elsif associations.any?(&:needs_polyamorous?)
    Join.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.



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

def alias(alias_name)
  clone.alias! alias_name
end

#alias!(alias_name) ⇒ Object

:nodoc:



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

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

#alias?Boolean

Returns:

  • (Boolean)


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

def alias?
  _table.kind_of? Arel::Nodes::TableAlias
end

#as(alias_name) ⇒ Object



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

def as(alias_name)
  self.alias(alias_name)
end

#evaluate(&block) ⇒ Object

Evaluates a DSL block. If arity is given, this method ‘yield` itself, rather than `instance_eval`.



74
75
76
77
78
79
80
# File 'lib/baby_squeel/table.rb', line 74

def evaluate(&block)
  if block.arity.zero?
    instance_eval(&block)
  else
    yield(self)
  end
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.



86
87
88
89
90
# File 'lib/baby_squeel/table.rb', line 86

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

#innerObject

Instruct the table to be joined with an INNER JOIN.



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

def inner
  clone.inner!
end

#inner!Object

:nodoc:



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

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

#on(node = nil, &block) ⇒ Object

Specify an explicit join.



63
64
65
# File 'lib/baby_squeel/table.rb', line 63

def on(node = nil, &block)
  clone.on!(node, &block)
end

#on!(node = nil, &block) ⇒ Object

:nodoc:



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

def on!(node = nil, &block) # :nodoc:
  self._on = node || evaluate(&block)
  self
end

#outerObject

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



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

def outer
  clone.outer!
end

#outer!Object

:nodoc:



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

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