class BabySqueel::Table
Attributes
Public Class Methods
# File lib/baby_squeel/table.rb, line 10 def initialize(arel_table) @_table = arel_table end
Public Instance Methods
See Arel::Table#[]
# File lib/baby_squeel/table.rb, line 15 def [](key) Nodes::Attribute.new(self, key) end
This method will be invoked by BabySqueel::Nodes::unwrap
. When called, there are three possible outcomes:
-
Join
explicitly using an on clause. Just return Arel. -
Implicit join without using an outer join. In this case, we'll just give a hash to Active Record, and join the normal way.
-
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
# File lib/baby_squeel/table.rb, line 19 def _join @_join ||= Arel::Nodes::InnerJoin end
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
# File lib/baby_squeel/table.rb, line 38 def alias? _table.kind_of? Arel::Nodes::TableAlias end
# File lib/baby_squeel/table.rb, line 23 def as(alias_name) self.alias(alias_name) end
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
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
Instruct the table to be joined with an INNER JOIN.
# File lib/baby_squeel/table.rb, line 53 def inner clone.inner! end
Specify an explicit join.
# File lib/baby_squeel/table.rb, line 63 def on(node = nil, &block) clone.on!(node, &block) end
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
# File lib/baby_squeel/table.rb, line 123 def method_missing(*args, &block) resolver.resolve!(*args, &block) || super end
# File lib/baby_squeel/table.rb, line 115 def resolver @resolver ||= Resolver.new(self, [:attribute]) end
# File lib/baby_squeel/table.rb, line 119 def respond_to_missing?(name, *) resolver.resolves?(name) || super end