Skip to content

Commit

Permalink
Upgrade straggler connection references.
Browse files Browse the repository at this point in the history
Upgrade safe semantics to be consistent with the new MongoClient safe semantics - legacy usage (:safe => true) is transparently upgraded to the new system.
BIG CHANGE: In keeping with the driver defaults, safety is now ON by default (:w => 1) rather than off by default. Use `safe false` to turn off safety in a model.
Fixed tests.
  • Loading branch information
cheald committed Dec 7, 2012
1 parent 875e9a3 commit 68373a5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/mongo_mapper.rb 100644 → 100755
Expand Up @@ -20,6 +20,7 @@ module MongoMapper
autoload :Plugins, 'mongo_mapper/plugins'
autoload :Translation, 'mongo_mapper/translation'
autoload :Version, 'mongo_mapper/version'
autoload :Utils, 'mongo_mapper/utils'

module Middleware
autoload :IdentityMap, 'mongo_mapper/middleware/identity_map'
Expand Down
8 changes: 4 additions & 4 deletions lib/mongo_mapper/connection.rb
Expand Up @@ -10,7 +10,7 @@ module Connection

# @api public
def connection
@@connection ||= Mongo::Connection.new
@@connection ||= Mongo::MongoClient.new
end

# @api public
Expand Down Expand Up @@ -75,12 +75,12 @@ def connect(environment, options={})

MongoMapper.connection = if env['hosts']
if env['hosts'].first.is_a?(String)
Mongo::ReplSetConnection.new( env['hosts'], options )
Mongo::MongoReplicaSetClient.new( env['hosts'], options )
else
Mongo::ReplSetConnection.new( *env['hosts'].push(options) )
Mongo::MongoReplicaSetClient.new( *env['hosts'].push(options) )
end
else
Mongo::Connection.new(env['host'], env['port'], options)
Mongo::MongoClient.new(env['host'], env['port'], options)
end

MongoMapper.database = env['database']
Expand Down
8 changes: 8 additions & 0 deletions lib/mongo_mapper/plugins/modifiers.rb 100644 → 100755
Expand Up @@ -79,13 +79,21 @@ def criteria_and_keys_from_args(args)
criteria = args[0]
updates = args[1]
options = args[2]
upgrade_legacy_safe_usage(options)
else
criteria, (updates, options) = args.partition { |a| !a.is_a?(Hash) }
criteria = { :id => criteria }
end

[criteria_hash(criteria).to_hash, updates, options]
end

def upgrade_legacy_safe_usage(options)
if options and options.key?(:safe)
options.delete :safe
options.merge! Utils.get_safe_options(options)
end
end
end

def unset(*args)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/querying.rb 100644 → 100755
Expand Up @@ -158,7 +158,7 @@ def update(options={})

def save_to_collection(options={})
@_new = false
collection.save(to_mongo, :safe => options[:safe])
collection.save(to_mongo, Utils.get_safe_options(options))
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/mongo_mapper/utils.rb
@@ -0,0 +1,11 @@
module MongoMapper
module Utils
def self.get_safe_options(options)
safe = options[:safe]
safe = {:w => 1} if safe == true or safe.nil?
safe = {:w => 0} if safe == false
safe = {:w => safe} if safe.is_a? Fixnum
safe
end
end
end
6 changes: 3 additions & 3 deletions test/functional/test_modifiers.rb 100644 → 100755
Expand Up @@ -76,9 +76,9 @@ def assert_keys_removed(page, *keys)
Mongo::Collection.any_instance.expects(:update).with(
{:title => "Better Be Safe than Sorry"},
{'$unset' => {:tags => 1}},
{:safe => true, :multi => true}
{:w => 1, :multi => true}
)
@page_class.unset({:title => "Better Be Safe than Sorry"}, :tags, {:safe => true})
@page_class.unset({:title => "Better Be Safe than Sorry"}, :tags, {:w => 1})
end

should "be able to pass both safe and upsert options" do
Expand Down Expand Up @@ -215,7 +215,7 @@ def assert_keys_removed(page, *keys)
Mongo::Collection.any_instance.expects(:update).with(
{:title => "Better Be Safe than Sorry"},
{'$set' => {:title => "I like safety."}},
{:safe => true, :multi => true}
{:w => 1, :multi => true}
)
@page_class.set({:title => "Better Be Safe than Sorry"}, {:title => "I like safety."}, {:safe => true})
end
Expand Down
6 changes: 3 additions & 3 deletions test/functional/test_safe.rb 100644 → 100755
Expand Up @@ -46,9 +46,9 @@ class SafeTest < Test::Unit::TestCase
end

context "using safe setting from class" do
should "pass :safe => true option to save" do
should "pass :w => 1 option to save" do
instance = @klass.new(:email => 'john@doe.com')
Mongo::Collection.any_instance.expects(:save).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:safe => true})
Mongo::Collection.any_instance.expects(:save).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:w => 1})
instance.save!
end

Expand Down Expand Up @@ -103,7 +103,7 @@ class SafeTest < Test::Unit::TestCase
context "using safe setting from class" do
should "pass :safe => options_hash to save" do
instance = @klass.new(:email => 'john@doe.com')
Mongo::Collection.any_instance.expects(:save).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:safe => {:j => true}})
Mongo::Collection.any_instance.expects(:save).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:j => true})
instance.save!
end

Expand Down

0 comments on commit 68373a5

Please sign in to comment.