Skip to content

Commit

Permalink
Added safe plugin to Document. Allows you to turn on :safe option for…
Browse files Browse the repository at this point in the history
… all saves using a class method.

class User
  include MongoMapper::Document
  safe
end
  • Loading branch information
jnunemaker committed May 24, 2010
1 parent ce3371a commit 9b41f9d
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/mongo_mapper/document.rb
Expand Up @@ -24,6 +24,7 @@ def self.included(model)
plugin Plugins::Persistence
plugin Plugins::Protected
plugin Plugins::Rails
plugin Plugins::Safe # needs to be after querying (save_to_collection)
plugin Plugins::Sci
plugin Plugins::Serialization
plugin Plugins::Timestamps
Expand Down
1 change: 1 addition & 0 deletions lib/mongo_mapper/plugins.rb
Expand Up @@ -31,6 +31,7 @@ def plugin(mod)
autoload :Protected, 'mongo_mapper/plugins/protected'
autoload :Querying, 'mongo_mapper/plugins/querying'
autoload :Rails, 'mongo_mapper/plugins/rails'
autoload :Safe, 'mongo_mapper/plugins/safe'
autoload :Sci, 'mongo_mapper/plugins/sci'
autoload :Serialization, 'mongo_mapper/plugins/serialization'
autoload :Timestamps, 'mongo_mapper/plugins/timestamps'
Expand Down
5 changes: 2 additions & 3 deletions lib/mongo_mapper/plugins/querying.rb
Expand Up @@ -214,6 +214,7 @@ def delete
private
def create_or_update(options={})
result = new? ? create(options) : update(options)
@new = false
result != false
end

Expand All @@ -226,9 +227,7 @@ def update(options={})
end

def save_to_collection(options={})
safe = options[:safe] || false
@new = false
collection.save(to_mongo, :safe => safe)
collection.save(to_mongo, :safe => options[:safe])
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/mongo_mapper/plugins/safe.rb
@@ -0,0 +1,27 @@
module MongoMapper
module Plugins
module Safe
module ClassMethods
def inherited(subclass)
super
subclass.safe if safe?
end

def safe
@safe = true
end

def safe?
@safe == true
end
end

module InstanceMethods
def save_to_collection(options={})
options[:safe] = self.class.safe? unless options.key?(:safe)
super
end
end
end
end
end
80 changes: 80 additions & 0 deletions test/functional/test_safe.rb
@@ -0,0 +1,80 @@
require 'test_helper'

class SafeTest < Test::Unit::TestCase
context "A Document" do
should "default safe to off" do
Doc().should_not be_safe
end

should "allow turning safe on" do
Doc() { safe }.should be_safe
end

context "inherited with safe setting on" do
should "set subclass safe setting on" do
inherited = Class.new(Doc() { safe })
inherited.should be_safe
end
end

context "inherited with safe setting off" do
should "leave subclass safe setting off" do
inherited = Class.new(Doc())
inherited.should_not be_safe
end
end
end

context "A safe document" do
setup do
@klass = Doc() do
safe
end
drop_indexes(@klass)
end

teardown do
drop_indexes(@klass)
end

context "#save" do
setup do
@klass.ensure_index :email, :unique => true
end

context "using safe setting from class" do
should "work fine when all is well" do
assert_nothing_raised do
@klass.new(:email => 'john@doe.com').save
end
end

should "raise error when operation fails" do
assert_raises(Mongo::OperationFailure) do
2.times do
@klass.new(:email => 'john@doe.com').save
end
end
end
end

context "overriding safe setting" do
should "raise error if safe is true" do
assert_raises(Mongo::OperationFailure) do
2.times do
@klass.new(:email => 'john@doe.com').save(:safe => true)
end
end
end

should "not raise error if safe is false" do
assert_nothing_raised do
2.times do
@klass.new(:email => 'john@doe.com').save(:safe => false)
end
end
end
end
end
end
end

0 comments on commit 9b41f9d

Please sign in to comment.