forked from mongodb/mongoid
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding upsert (before|after|around) callback
- Loading branch information
Showing
7 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# encoding: utf-8 | ||
module Mongoid | ||
module Persistence | ||
|
||
# Contains common logic for upsert operations. | ||
module Upsertion | ||
|
||
# Wrap all the common upsert logic for root docments. | ||
# | ||
# @example Execute common upsert logic. | ||
# prepare do |doc| | ||
# collection.find({ :_id => 1 }).upsert({ name: "test" }, [ :upsert ]) | ||
# end | ||
# | ||
# @param [ Proc ] block The block to call. | ||
# | ||
# @return [ true, false ] If the save passed or not. | ||
# | ||
# @since 3.0.0 | ||
def prepare(&block) | ||
return false if validating? && document.invalid?(:update) | ||
result = document.run_callbacks(:upsert) do | ||
yield(document); true | ||
end | ||
document.post_persist unless result == false | ||
result | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters