Class: Squeel::Nodes::Join

Inherits:
Object
  • Object
show all
Defined in:
lib/squeel/nodes/join.rb

Overview

A node representing a joined association

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = Arel::InnerJoin, klass = nil) ⇒ Join

Create a new Join node

Parameters:

  • name (Symbol)

    The association name

  • type (Arel::InnerJoin, Arel::OuterJoin) (defaults to: Arel::InnerJoin)

    The ARel join class

  • klass (Class, String, Symbol) (defaults to: nil)

    The polymorphic belongs_to class or class name



15
16
17
# File 'lib/squeel/nodes/join.rb', line 15

def initialize(name, type = Arel::InnerJoin, klass = nil)
  @_join = Polyamorous::Join.new(name, type, klass)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#node_nameKeyPath #node_name(klass) ⇒ KeyPath

Ensures that a Join can be used as the base of a new KeyPath

Overloads:

  • #node_nameKeyPath

    Creates a new KeyPath with this Join as the base and the method_name as the endpoint

    Returns:

  • #node_name(klass) ⇒ KeyPath

    Creates a new KeyPath with this Join as the base and a polymorphic belongs_to join as the endpoint

    Parameters:

    • klass (Class)

      The polymorphic class for the join

    Returns:



54
55
56
57
58
59
60
61
# File 'lib/squeel/nodes/join.rb', line 54

def method_missing(method_id, *args)
  super if method_id == :to_ary
  if (args.size == 1) && (Class === args[0])
    KeyPath.new(self, Join.new(method_id, Arel::InnerJoin, args[0]))
  else
    KeyPath.new(self, method_id)
  end
end

Instance Attribute Details

#_joinObject (readonly)

Returns the value of attribute _join.



7
8
9
# File 'lib/squeel/nodes/join.rb', line 7

def _join
  @_join
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Compare with other objects

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/squeel/nodes/join.rb', line 38

def eql?(other)
  self.class.eql?(other.class) &&
  self._name.eql?(other._name) &&
  self._type.eql?(other._type) &&
  self._klass.eql?(other._klass)
end

#innerJoin

Set the join type to an inner join

Returns:

  • (Join)

    The join, with an updated join type.



21
22
23
24
# File 'lib/squeel/nodes/join.rb', line 21

def inner
  self._type = Arel::InnerJoin
  self
end

#outerJoin

Set the join type to an outer join

Returns:

  • (Join)

    The join, with an updated join type.



28
29
30
31
# File 'lib/squeel/nodes/join.rb', line 28

def outer
  self._type = Arel::OuterJoin
  self
end

#polymorphic?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/squeel/nodes/join.rb', line 33

def polymorphic?
  _klass
end

#to_symNilClass

expand_hash_conditions_for_aggregates assumes our hash keys can be converted to symbols, so this has to be implemented, but it doesn’t really have to do anything useful.

Returns:

  • (NilClass)

    Just to avoid bombing out on expand_hash_conditions_for_aggregates



75
76
77
# File 'lib/squeel/nodes/join.rb', line 75

def to_sym
  nil
end

#~KeyPath

Return a KeyPath containing only this Join, but flagged as absolute. This helps Joins behave more like a KeyPath, as anyone using the Squeel DSL is likely to think of them as such.

Returns:

  • (KeyPath)

    An absolute KeyPath, containing only this Join



67
68
69
# File 'lib/squeel/nodes/join.rb', line 67

def ~
  KeyPath.new [], self, true
end