Module: Ripple::Scoping::ClassMethods

Defined in:
lib/ripple_searchable/scoping.rb

Instance Method Summary collapse

Instance Method Details

#default_scope(value) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/ripple_searchable/scoping.rb', line 42

def default_scope(value)
  self.default_scoping = if default_scoping
    ->{ default_scoping.call.merge(value.to_proc.call) } unless default_scoping.call == value
  else
    value.to_proc
  end
end

#scope(name, value, &block) ⇒ Object

Create a scope that can be accessed from the class level or chained to criteria by the provided name.

Example

class Product
  include Ripple::Document

  scope :active, where(active: true)
  scope :avail, ->(count){ where(quantity: count)}
end

Product.active.where(name: "peter")

sets the selector to: “((active:true)) AND (name:peter)”



32
33
34
35
36
37
38
39
40
# File 'lib/ripple_searchable/scoping.rb', line 32

def scope(name, value, &block)
  name = name.to_sym
  valid_scope_name?(name)
  scopes[name] = {
    scope: strip_default_scope(value),
    extension: Module.new(&block)
  }
  define_scope_method(name)
end

#scope_stackObject



50
51
52
# File 'lib/ripple_searchable/scoping.rb', line 50

def scope_stack
  Thread.current[:"#{self.bucket_name}_scope_stack"] ||= []
end

#with_default_scopeObject



54
55
56
# File 'lib/ripple_searchable/scoping.rb', line 54

def with_default_scope
  default_scoping.try(:call) || without_default_scope
end

#with_scope(criteria) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/ripple_searchable/scoping.rb', line 67

def with_scope(criteria)
  scope_stack.push(criteria)
  begin
    yield criteria
  ensure
    scope_stack.pop
  end
end

#without_default_scopeObject



58
59
60
61
# File 'lib/ripple_searchable/scoping.rb', line 58

def without_default_scope
  Thread.current[:"#{self.bucket_name}_without_default_scope"] = true
  scope_stack.last || Criteria.new(self)
end

#without_default_scope?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ripple_searchable/scoping.rb', line 63

def without_default_scope?
  Thread.current[:"#{self.bucket_name}_without_default_scope"]
end