Class: Refined::ScopeChain

Inherits:
Object
  • Object
show all
Defined in:
lib/refined/scope_chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, criteria = {}) ⇒ ScopeChain

Returns a new instance of ScopeChain.



5
6
7
8
# File 'lib/refined/scope_chain.rb', line 5

def initialize(name, criteria={})
  self.constant_name = name
  self.criteria = criteria
end

Instance Attribute Details

#constant_nameObject

Returns the value of attribute constant_name.



3
4
5
# File 'lib/refined/scope_chain.rb', line 3

def constant_name
  @constant_name
end

#criteriaObject

Returns the value of attribute criteria.



2
3
4
# File 'lib/refined/scope_chain.rb', line 2

def criteria
  @criteria
end

Instance Method Details

#chain!Object



20
21
22
23
24
25
26
# File 'lib/refined/scope_chain.rb', line 20

def chain!
  return constant_name.scoped unless criteria.present?
  criteria.inject(constant_name) do |model, (method, arg)|
    construct(method, arg)
    model.send(method, arg)
  end
end

#construct(name, value) ⇒ Object



14
15
16
17
18
# File 'lib/refined/scope_chain.rb', line 14

def construct(name, value)
  constant_name.send :define_singleton_method, name do |arg|
    where(name => value)
  end unless constant_name.respond_to?(name)
end