-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added safe plugin to Document. Allows you to turn on :safe option for…
… all saves using a class method. class User include MongoMapper::Document safe end
- Loading branch information
1 parent
ce3371a
commit 9b41f9d
Showing
5 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |