Class: AutoScopes::AssociationsMethods

Inherits:
Struct
  • Object
show all
Defined in:
lib/auto_scopes/associations_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(associations_chains, path = associations_chains.path) ⇒ AssociationsMethods

Returns a new instance of AssociationsMethods.



9
10
11
12
# File 'lib/auto_scopes/associations_methods.rb', line 9

def initialize(associations_chains, path = associations_chains.path)
	self.associations_chains = associations_chains
	self.path                = path.clone
end

Instance Attribute Details

#associations_chainsObject

Returns the value of attribute associations_chains

Returns:

  • (Object)

    the current value of associations_chains



2
3
4
# File 'lib/auto_scopes/associations_methods.rb', line 2

def associations_chains
  @associations_chains
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/auto_scopes/associations_methods.rb', line 3

def path
  @path
end

Instance Method Details

#create_associationObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/auto_scopes/associations_methods.rb', line 25

def create_association
  	association_klass, field = get_last_association_from_path do |klass, foreign_key|
  		path.pop
			[klass, foreign_key]
  	end

  	joins_path = organize_joins

  	field ||= 'id'

    klass.define_singleton_method("#{config.scope_association_prefix}_#{associations_chains.path.last}") do |id|
      joins(joins_path).where(association_klass.table_name => { field => id })
    end
end

#create_merge(methods) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/auto_scopes/associations_methods.rb', line 14

def create_merge(methods)
	association_klass = get_last_association_from_path
	joins_path        = organize_joins

  	methods.each do |method|
   	klass.define_singleton_method(method) do |*args|
       joins(joins_path).merge association_klass.send(method, *args)
     end
  	end
end