Class: MagicScopes::StateScopesGenerator

Inherits:
MagicScopes::ScopesGenerator::Base show all
Defined in:
lib/magic_scopes/scopes_generators/state.rb

Instance Method Summary collapse

Methods inherited from MagicScopes::ScopesGenerator::Base

instance

Constructor Details

#initialize(model, state) ⇒ StateScopesGenerator

Returns a new instance of StateScopesGenerator.



4
5
6
7
8
# File 'lib/magic_scopes/scopes_generators/state.rb', line 4

def initialize(model, state)
  @model = model
  @state = state
  @key   = "#{model.table_name}.#{attr}"
end

Instance Method Details

#is(name) ⇒ Object



10
11
12
# File 'lib/magic_scopes/scopes_generators/state.rb', line 10

def is(name)
  scope name || @state, -> { where("#{@key}" => @state) }
end

#not(name) ⇒ Object



14
15
16
# File 'lib/magic_scopes/scopes_generators/state.rb', line 14

def not(name)
  scope name || "not_#{@state}", -> { where("#{@key} != ? OR #{@key} IS NULL", @state) }
end

#with(name) ⇒ Object



18
19
20
21
# File 'lib/magic_scopes/scopes_generators/state.rb', line 18

def with(name)
  @model.instance_eval("undef :with_#{attr}") unless name
  scope name || "with_#{@attr}", ->(*vals) { where(vals.empty? ? "#{@key} IS NOT NULL" : ["#{@key} IN (?)", vals]) }
end

#without(name) ⇒ Object



23
24
25
26
# File 'lib/magic_scopes/scopes_generators/state.rb', line 23

def without(name)
  @model.instance_eval("undef :without_#{attr}") unless name
  scope name || "without_#{@attr}", ->(*vals) { where(vals.empty? ? "#{@key} IS NULL" : ["#{@key} NOT IN (?)", vals]) }
end