diff --git a/lib/mongo_mapper.rb b/lib/mongo_mapper.rb old mode 100644 new mode 100755 index aa764aa91..94586dd12 --- a/lib/mongo_mapper.rb +++ b/lib/mongo_mapper.rb @@ -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' diff --git a/lib/mongo_mapper/connection.rb b/lib/mongo_mapper/connection.rb index a2d967288..a9576dfb3 100755 --- a/lib/mongo_mapper/connection.rb +++ b/lib/mongo_mapper/connection.rb @@ -10,7 +10,7 @@ module Connection # @api public def connection - @@connection ||= Mongo::Connection.new + @@connection ||= Mongo::MongoClient.new end # @api public @@ -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'] diff --git a/lib/mongo_mapper/plugins/modifiers.rb b/lib/mongo_mapper/plugins/modifiers.rb old mode 100644 new mode 100755 index 55080c3c8..a83b69e15 --- a/lib/mongo_mapper/plugins/modifiers.rb +++ b/lib/mongo_mapper/plugins/modifiers.rb @@ -79,6 +79,7 @@ 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 } @@ -86,6 +87,13 @@ def criteria_and_keys_from_args(args) [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) diff --git a/lib/mongo_mapper/plugins/querying.rb b/lib/mongo_mapper/plugins/querying.rb old mode 100644 new mode 100755 index 5fdacbeff..e70e39efe --- a/lib/mongo_mapper/plugins/querying.rb +++ b/lib/mongo_mapper/plugins/querying.rb @@ -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 diff --git a/lib/mongo_mapper/utils.rb b/lib/mongo_mapper/utils.rb new file mode 100755 index 000000000..48bfcc6f4 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/test/functional/test_modifiers.rb b/test/functional/test_modifiers.rb old mode 100644 new mode 100755 index f9d8c6bb3..881055fb5 --- a/test/functional/test_modifiers.rb +++ b/test/functional/test_modifiers.rb @@ -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 @@ -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 diff --git a/test/functional/test_safe.rb b/test/functional/test_safe.rb old mode 100644 new mode 100755 index d49f7b89e..228b081e3 --- a/test/functional/test_safe.rb +++ b/test/functional/test_safe.rb @@ -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 @@ -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