Module: Toqua::Scoping

Extended by:
ActiveSupport::Concern
Included in:
KeysetPagination, Pagination, Search
Defined in:
lib/toqua/scoping.rb

Instance Method Summary collapse

Instance Method Details

#__run_scope(relation, scope, opts) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/toqua/scoping.rb', line 13

def __run_scope(relation, scope, opts)
  if_condition = if opts[:if]
                   opts[:if].respond_to?(:call) ? !!instance_exec(&opts[:if]) : send(opts[:if])
                 elsif opts[:unless]
                   opts[:unless].respond_to?(:call) ? !instance_exec(&opts[:unless]) : !send(opts[:unless])
                 else
                   true
                 end

  action_condition = if opts[:only]
                       [opts[:only]].flatten.include?(action_name.to_sym)
                     else
                       true
                     end

  if if_condition && action_condition
    self.instance_exec(relation, &scope)
  else
    relation
  end
end

#apply_scopes(start) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/toqua/scoping.rb', line 5

def apply_scopes(start)
  s = start
  self.class.__scopes.each do |scope, opts|
    s = __run_scope(s, scope, opts)
  end
  s
end