Module: ScopedAssociations::ActiveRecord4::HasMany

Defined in:
lib/scoped_associations/activerecord4/has_many.rb

Defined Under Namespace

Modules: ReflectionExtension Classes: ScopedHasManyAssociation

Instance Method Summary collapse

Instance Method Details

#build(attributes = {}) ⇒ Object



9
10
11
12
13
# File 'lib/scoped_associations/activerecord4/has_many.rb', line 9

def build(attributes = {})
  reflection = ActiveRecord::VERSION::MINOR == 0 ? super() : super(attributes)
  extend_reflection(reflection)
  reflection
end

#foreign_scope(model) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/scoped_associations/activerecord4/has_many.rb', line 30

def foreign_scope(model)
  if options[:as]
    "#{options[:as]}_scope"
  else
    model.name.demodulize.underscore + "_scope"
  end
end

#initialize(model, name, scope, options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/scoped_associations/activerecord4/has_many.rb', line 15

def initialize(model, name, scope, options)
  super
  if scoped?
    search_scope = foreign_scope(model)
    proc_scope = proc { where(search_scope => name) }
    if @scope.nil?
      @scope = proc { instance_eval(&proc_scope) }
    else
      old_scope = @scope
      @scope = proc { instance_eval(&proc_scope)
                        .instance_eval(&old_scope) }
    end
  end
end

#valid_optionsObject



5
6
7
# File 'lib/scoped_associations/activerecord4/has_many.rb', line 5

def valid_options
  super + [:scoped]
end