Skip to content

Commit

Permalink
Refactor execute_or_raise
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jul 1, 2012
1 parent c9e0ef6 commit 4be5daf
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/mongoid/criteria.rb
Expand Up @@ -162,9 +162,7 @@ def documents=(docs)
# @since 2.0.0
def execute_or_raise(ids, multi)
result = multiple_from_map_or_db(ids)
if (result.size < ids.size) && Mongoid.raise_not_found_error
raise Errors::DocumentNotFound.new(klass, ids, ids - result.map(&:_id))
end
check_for_missing_documents!(result, ids)
multi ? result : result.first
end

Expand Down Expand Up @@ -529,6 +527,27 @@ def where(expression)

private

# Are documents in the query missing, and are we configured to raise an
# error?
#
# @api private
#
# @example Check for missing documents.
# criteria.check_for_missing_documents!([], [ 1 ])
#
# @param [ Array<Document> ] result The result.
# @param [ Array<Object> ] ids The ids.
#
# @raise [ Errors::DocumentNotFound ] If none are found and raising an
# error.
#
# @since 3.0.0
def check_for_missing_documents!(result, ids)
if (result.size < ids.size) && Mongoid.raise_not_found_error
raise Errors::DocumentNotFound.new(klass, ids, ids - result.map(&:_id))
end
end

# Create a document given the provided method and attributes from the
# existing selector.
#
Expand Down

0 comments on commit 4be5daf

Please sign in to comment.