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.



28
29
30
31
# File 'lib/active_hash/base.rb', line 28

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

Instance Method Details

#not(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_hash/base.rb', line 33

def not(options)
  return @scope 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

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

  ActiveHash::Relation.new(@scope.klass, filtered_records, {})
end