4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/simply_stored/couch/finders.rb', line 4
def find(*args)
what = args.shift
options = args.last.is_a?(Hash) ? args.last : {}
if options && order = options.delete(:order)
options[:descending] = true if order == :desc
end
with_deleted = options.delete(:with_deleted)
case what
when :all
if with_deleted || !soft_deleting_enabled?
CouchPotato.database.view(all_documents(*args))
else
CouchPotato.database.view(all_documents_without_deleted(options.update(:include_docs => true)))
end
when :first
if with_deleted || !soft_deleting_enabled?
CouchPotato.database.view(all_documents(:limit => 1, :include_docs => true)).first
else
CouchPotato.database.view(all_documents_without_deleted(:limit => 1, :include_docs => true)).first
end
else
raise SimplyStored::Error, "Can't load record without an id" if what.nil?
document = CouchPotato.database.load_document(what)
if document.nil? or !document.is_a?(self) or (document.deleted? && !with_deleted)
raise(SimplyStored::RecordNotFound, "#{self.name} could not be found with #{what.inspect}")
end
document
end
end
|