Created
May 10, 2012 20:29
-
-
Save jseibert/2655684 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Ensure apps are created for the organization atomically by using find_and_modify | |
result = App.collection.find_and_modify({ | |
:query => { | |
:bundle_identifier => params[:id].downcase, | |
:organization_id => current_organization._id }, | |
:update => { | |
"$set" => { | |
:bundle_identifier => params[:id].downcase, | |
:organization_id => current_organization._id } }, | |
:new => true, | |
:upsert => true }) | |
# The model has a bunch of defaults, however Mongoid does not set them on | |
# find_and_modify calls, so we have to be clever to update the defaults while | |
# keeping things atomic | |
if result["builds"].nil? && result["build_opts"].nil? && result["status"].nil? && | |
result["platform"].nil? && result["dc"].nil? && result["idc"].nil? | |
result = App.collection.find_and_modify({ | |
:query => { | |
:bundle_identifier => result["bundle_identifier"], | |
:organization_id => result["organization_id"], | |
:builds => nil, | |
:build_opts => nil, | |
:status => nil, | |
:platform => nil, | |
:dc => nil, | |
:idc => nil }, | |
:update => { | |
"$set" => { | |
:created_at => Time.now.utc, | |
:builds => [], | |
:build_opts => {}, | |
:status => :new, | |
:platform => :ios, | |
:dc => 0, | |
:idc => 0 } }, | |
:fields => { :id => 1 } } | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment