class BabySqueel::Table

Attributes

_join[W]
_on[RW]
_table[RW]

Public Class Methods

new(arel_table) click to toggle source
# File lib/baby_squeel/table.rb, line 10
def initialize(arel_table)
  @_table = arel_table
end

Public Instance Methods

[](key) click to toggle source

See Arel::Table#[]

# File lib/baby_squeel/table.rb, line 15
def [](key)
  Nodes::Attribute.new(self, key)
end
_arel(associations = []) click to toggle source

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.

# 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
_join() click to toggle source
# File lib/baby_squeel/table.rb, line 19
def _join
  @_join ||= Arel::Nodes::InnerJoin
end
alias(alias_name) click to toggle source

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

# File lib/baby_squeel/table.rb, line 29
def alias(alias_name)
  clone.alias! alias_name
end
alias?() click to toggle source
# File lib/baby_squeel/table.rb, line 38
def alias?
  _table.kind_of? Arel::Nodes::TableAlias
end
as(alias_name) click to toggle source
# File lib/baby_squeel/table.rb, line 23
def as(alias_name)
  self.alias(alias_name)
end
evaluate() { |self| ... } click to toggle source

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

# 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 = []) click to toggle source

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.

# 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
inner() click to toggle source

Instruct the table to be joined with an INNER JOIN.

# File lib/baby_squeel/table.rb, line 53
def inner
  clone.inner!
end
on(node = nil, &block) click to toggle source

Specify an explicit join.

# File lib/baby_squeel/table.rb, line 63
def on(node = nil, &block)
  clone.on!(node, &block)
end
outer() click to toggle source

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

# File lib/baby_squeel/table.rb, line 43
def outer
  clone.outer!
end

Private Instance Methods

method_missing(*args, &block) click to toggle source
Calls superclass method
# File lib/baby_squeel/table.rb, line 123
def method_missing(*args, &block)
  resolver.resolve!(*args, &block) || super
end
resolver() click to toggle source
# File lib/baby_squeel/table.rb, line 115
def resolver
  @resolver ||= Resolver.new(self, [:attribute])
end
respond_to_missing?(name, *) click to toggle source
Calls superclass method
# File lib/baby_squeel/table.rb, line 119
def respond_to_missing?(name, *)
  resolver.resolves?(name) || super
end