Class: NinjaModel::Associations::AssociationScope

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association) ⇒ AssociationScope

Returns a new instance of AssociationScope.



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

def initialize(association)
  @association = association
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



5
6
7
# File 'lib/ninja_model/associations/association_scope.rb', line 5

def association
  @association
end

Instance Method Details

#add_constraints(scope) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ninja_model/associations/association_scope.rb', line 26

def add_constraints(scope)
  if reflection.source_macro == :belongs_to
    key = reflection.association_primary_key
    foreign_key = reflection.foreign_key
  else
    key = reflection.foreign_key
    foreign_key = reflection.active_record_primary_key
  end

  scope = scope.where(key => owner[foreign_key])
end

#scopeObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ninja_model/associations/association_scope.rb', line 14

def scope
  scope = klass.unscoped
  scope = scope.extending(*Array.wrap(options[:extend]))
  scope = scope.apply_finder_options(
    options.slice(
      :order, :limit, :joins, :group, :having, :offset
    )
  )

  add_constraints(scope)
end