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



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

def initialize(name, type = Arel::InnerJoin, klass = nil)
  @_name, @_type = name, type
  @_klass = convert_to_class(klass) if 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:



70
71
72
73
74
75
76
77
# File 'lib/squeel/nodes/join.rb', line 70

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

#_klassClass, NilClass

Returns:

  • (Class)

    The polymorphic belongs_to join class

  • (NilClass)

    If the join is not a polymorphic belongs_to join



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

def _klass
  @_klass
end

#_nameSymbol (readonly)

Returns The join’s association name.

Returns:

  • (Symbol)

    The join’s association name



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

def _name
  @_name
end

#_typeArel::InnerJoin, Arel::OuterJoin (readonly)

Returns The ARel join type.

Returns:

  • (Arel::InnerJoin, Arel::OuterJoin)

    The ARel join type



11
12
13
# File 'lib/squeel/nodes/join.rb', line 11

def _type
  @_type
end

Instance Method Details

#convert_to_class(value) ⇒ Class (private)

Convert the given value into a class.

Parameters:

  • value (Class, String, Symbol)

    The value to be converted

Returns:

  • (Class)

    The class after conversion



100
101
102
103
104
105
106
107
108
109
# File 'lib/squeel/nodes/join.rb', line 100

def convert_to_class(value)
  case value
  when String, Symbol
    Kernel.const_get(value)
  when Class
    value
  else
    raise ArgumentError, "#{value} cannot be converted to a Class"
  end
end

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

Compare with other objects

Returns:

  • (Boolean)


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

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

#innerJoin

Set the join type to an inner join

Returns:

  • (Join)

    The join, with an updated join type.



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

def inner
  @_type = Arel::InnerJoin
  self
end

#outerJoin

Set the join type to an outer join

Returns:

  • (Join)

    The join, with an updated join type.



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

def outer
  @_type = Arel::OuterJoin
  self
end

#polymorphic?NilClass, Class

Returns a true value (the class itself) if a polymorphic belongs_to class has been set

Returns:

  • (NilClass, Class)

    The class, if present.



49
50
51
# File 'lib/squeel/nodes/join.rb', line 49

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



91
92
93
# File 'lib/squeel/nodes/join.rb', line 91

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



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

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