Class: ActiveRecord::Associations::AssociationScope

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/associations/association_scope.rb

Overview

:nodoc:

Direct Known Subclasses

DisableJoinsAssociationScope

Defined Under Namespace

Classes: ReflectionProxy

Constant Summary collapse

INSTANCE =
create

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_transformation) ⇒ AssociationScope

Returns a new instance of AssociationScope.



15
16
17
# File 'lib/active_record/associations/association_scope.rb', line 15

def initialize(value_transformation)
  @value_transformation = value_transformation
end

Class Method Details

.create(&block) ⇒ Object



10
11
12
13
# File 'lib/active_record/associations/association_scope.rb', line 10

def self.create(&block)
  block ||= lambda { |val| val }
  new(block)
end

.get_bind_values(owner, chain) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_record/associations/association_scope.rb', line 34

def self.get_bind_values(owner, chain)
  binds = []
  last_reflection = chain.last

  binds << last_reflection.join_id_for(owner)
  if last_reflection.type
    binds << owner.class.polymorphic_name
  end

  chain.each_cons(2).each do |reflection, next_reflection|
    if reflection.type
      binds << next_reflection.klass.polymorphic_name
    end
  end
  binds
end

.scope(association) ⇒ Object



6
7
8
# File 'lib/active_record/associations/association_scope.rb', line 6

def self.scope(association)
  INSTANCE.scope(association)
end

Instance Method Details

#scope(association) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record/associations/association_scope.rb', line 21

def scope(association)
  klass = association.klass
  reflection = association.reflection
  scope = klass.unscoped
  owner = association.owner
  chain = get_chain(reflection, association, scope.alias_tracker)

  scope.extending! reflection.extensions
  scope = add_constraints(scope, owner, chain)
  scope.limit!(1) unless reflection.collection?
  scope
end