Module: ActiveRecord::Associations::ScopeReflection

Defined in:
lib/scope_reflection.rb

Instance Method Summary collapse

Instance Method Details

#initialize(owner, reflection) ⇒ Object



4
5
6
7
# File 'lib/scope_reflection.rb', line 4

def initialize(owner, reflection)
  scope_reflection(reflection)
  super
end

#scope_reflection(reflection) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/scope_reflection.rb', line 9

def scope_reflection(reflection)

  source_reflection_class = 
    if reflection.is_a? ActiveRecord::Reflection::ThroughReflection 
      reflection.source_reflection.klass 
    else 
      reflection.klass
    end
    
  scope_names = [reflection.options[:scope]].flatten.compact
  final_scope = scope_names.inject(source_reflection_class) do |accumulated_scope, scope|
    if accumulated_scope.respond_to?(scope)
      accumulated_scope.send(scope)
    else
      raise ActiveRecord::UndefinedScopeReflectionError.new(self, scope)
    end
  end

  current_scoped_methods = final_scope.send(:current_scoped_methods) || {}
  reflection.options.merge!(current_scoped_methods[:find] || {}) 
end