Module: NullObjectModels::Extension::ClassMethods

Defined in:
lib/null_object_models/extension.rb

Instance Method Summary collapse

Instance Method Details

#find_or_null(id_or_ids, klass = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/null_object_models/extension.rb', line 6

def find_or_null(id_or_ids, klass = nil)
  klass ||= null_model

  if id_or_ids.is_a?(Array)
    found_collection = where(id: id_or_ids)

    id_or_ids.map do |id|
      found_collection.detect { |found| found.id == id } || klass.new(id, self)
    end
  else
    find_by_id(id_or_ids) || klass.new(id_or_ids, self)
  end
end

#null_modelObject



20
21
22
23
24
# File 'lib/null_object_models/extension.rb', line 20

def null_model
  ('Null' + self.name).constantize
rescue
  NullObjectModels::Default
end