class BabySqueel::Association
Attributes
_polymorphic_klass[RW]
Specifies the model that the polymorphic association should join with
_reflection[R]
An Active Record association reflection
Public Class Methods
new(parent, reflection)
click to toggle source
Calls superclass method
# File lib/baby_squeel/association.rb, line 12 def initialize(parent, reflection) @parent = parent @_reflection = reflection # In the case of a polymorphic reflection these # attributes will be set after calling #of unless @_reflection.polymorphic? super @_reflection.klass end end
Public Instance Methods
!=(other)
click to toggle source
# File lib/baby_squeel/association.rb, line 27 def !=(other) Nodes.wrap build_where_clause(other).invert.ast end
==(other)
click to toggle source
# File lib/baby_squeel/association.rb, line 23 def ==(other) Nodes.wrap build_where_clause(other).ast end
_arel(associations = [])
click to toggle source
Intelligently constructs Arel nodes. There are three outcomes:
-
The user explicitly constructed their join using on. See BabySqueel::Table#_arel.
Post.joining { author.on(author_id == author.id) }
-
The user aliased an implicitly joined association. ActiveRecord's join dependency gives us no way of handling this, so we have to throw an error.
Post.joining { author.as('some_alias') }
-
The user implicitly joined this association, so we pass this association up the tree until it hits the top-level
BabySqueel::Table
. Once it gets there, Arel join nodes will be constructed.Post.joining { author }
Calls superclass method
# File lib/baby_squeel/association.rb, line 85 def _arel(associations = []) if _on super elsif alias? raise AssociationAliasingError.new(_reflection.name, _table.right) elsif _reflection.polymorphic? && _polymorphic_klass.nil? raise PolymorphicNotSpecifiedError.new(_reflection.name) else @parent._arel([self, *associations]) end end
add_to_tree(hash)
click to toggle source
See Join#add_to_tree
.
# File lib/baby_squeel/association.rb, line 51 def add_to_tree(hash) polyamorous = Polyamorous::Join.new( _reflection.name, _join, _polymorphic_klass ) hash[polyamorous] ||= {} end
find_alias(associations = [])
click to toggle source
See BabySqueel::Table#find_alias
.
# File lib/baby_squeel/association.rb, line 62 def find_alias(associations = []) @parent.find_alias([self, *associations]) end
needs_polyamorous?()
click to toggle source
# File lib/baby_squeel/association.rb, line 46 def needs_polyamorous? _join == Arel::Nodes::OuterJoin || _reflection.polymorphic? end
of(klass)
click to toggle source
# File lib/baby_squeel/association.rb, line 31 def of(klass) unless _reflection.polymorphic? raise PolymorphicSpecificationError.new(_reflection.name, klass) end clone.of! klass end
of!(klass)
click to toggle source
# File lib/baby_squeel/association.rb, line 39 def of!(klass) self._scope = klass self._table = klass.arel_table self._polymorphic_klass = klass self end
Private Instance Methods
build_where_clause(other)
click to toggle source
# File lib/baby_squeel/association.rb, line 99 def build_where_clause(other) if valid_where_clause?(other) relation = @parent._scope.all factory = relation.send(:where_clause_factory) factory.build({ _reflection.name => other }, []) else raise AssociationComparisonError.new(_reflection.name, other) end end
valid_where_clause?(other)
click to toggle source
# File lib/baby_squeel/association.rb, line 109 def valid_where_clause?(other) if other.respond_to? :all? other.all? { |o| valid_where_clause? o } else other.nil? || other.respond_to?(:model_name) end end