Class: ActiveHash::Base::WhereChain

Inherits:
Object
  • Object
show all
Defined in:
lib/active_hash/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ WhereChain

Returns a new instance of WhereChain.



20
21
22
23
# File 'lib/active_hash/base.rb', line 20

def initialize(scope)
  @scope = scope
  @records = @scope.all
end

Instance Method Details

#not(options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_hash/base.rb', line 25

def not(options)
  return @records if options.blank?

  # use index if searching by id
  if options.key?(:id) || options.key?("id")
    ids = @scope.pluck(:id) - Array.wrap(options.delete(:id) || options.delete("id"))
    candidates = ids.map { |id| @scope.find_by_id(id) }.compact
  end
  return candidates if options.blank?

  (candidates || @records || []).reject do |record|
    match_options?(record, options)
  end
end