Class: Squeel::Nodes::Join

Inherits:
Node
  • 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



17
18
19
# File 'lib/squeel/nodes/join.rb', line 17

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:



61
62
63
64
65
66
67
68
# File 'lib/squeel/nodes/join.rb', line 61

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.



9
10
11
# File 'lib/squeel/nodes/join.rb', line 9

def _join
  @_join
end

Instance Method Details

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

Compare with other objects

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/squeel/nodes/join.rb', line 45

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

#hashObject

Implemented for equality testing



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

def hash
  [_name, _type, _klass].hash
end

#innerJoin

Set the join type to an inner join

Returns:

  • (Join)

    The join, with an updated join type.



23
24
25
26
# File 'lib/squeel/nodes/join.rb', line 23

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.



30
31
32
33
# File 'lib/squeel/nodes/join.rb', line 30

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

#polymorphic?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/squeel/nodes/join.rb', line 35

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



82
83
84
# File 'lib/squeel/nodes/join.rb', line 82

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



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

def ~
  KeyPath.new [self], true
end