Skip to content

Instantly share code, notes, and snippets.

@jseibert
Created May 10, 2012 20:29
Show Gist options
  • Save jseibert/2655684 to your computer and use it in GitHub Desktop.
Save jseibert/2655684 to your computer and use it in GitHub Desktop.
# 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