Module: DynamicDefaultScoping::ClassMethods

Defined in:
lib/dynamic_default_scoping.rb

Instance Method Summary collapse

Instance Method Details

#default_scope(name, options = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dynamic_default_scoping.rb', line 9

def default_scope name, options = nil
  if options.present?
    scope name, options
  else
    name, options = nil, name
  end

  reset_scoped_methods

  scoping = self.default_scoping.dup
  last = scoping.pop

  scope = if last.respond_to?(:call) || options.respond_to?(:call)
    lambda do
      construct_finder_arel \
        options.respond_to?(:call) ? options.call : options,
        last.respond_to?(:call)    ? last.call    : last
    end
  else
    construct_finder_arel options, last
  end

  self.default_scoping = scoping << scope
end

#scoped(options = nil) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/dynamic_default_scoping.rb', line 34

def scoped options = nil
  if options
    scoped.apply_finder_options options
  else
    last = current_scoped_methods
    last ? relation.merge(last) : relation.clone
  end
end