Skip to content

Commit

Permalink
Fixed problem where errors set in before_validation_on_* filters woul…
Browse files Browse the repository at this point in the history
…d not be respected when checking a documents validity.
  • Loading branch information
peritor committed Oct 15, 2009
1 parent 490c150 commit db34bf9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/couch_potato/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def create_document(document, validate)
if validate
document.run_callbacks :before_validation_on_save
document.run_callbacks :before_validation_on_create
return unless document.valid?
return unless valid_document?(document)
end

document.run_callbacks :before_save
Expand All @@ -94,7 +94,7 @@ def update_document(document, validate)
if validate
document.run_callbacks :before_validation_on_save
document.run_callbacks :before_validation_on_update
return unless document.valid?
return unless valid_document?(document)
end

document.run_callbacks :before_save
Expand All @@ -106,6 +106,15 @@ def update_document(document, validate)
true
end

def valid_document?(document)
errors = document.errors.errors.dup
document.valid?
errors.each do |k, v|
v.each {|message| document.errors.add(k, message)}
end
document.errors.empty?
end

def database
@database
end
Expand Down
40 changes: 39 additions & 1 deletion spec/unit/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class Child
describe CouchPotato::Database, 'save_document' do
it "should set itself on the model for a new object before doing anything else" do
db = CouchPotato::Database.new(stub('couchrest db', :info => nil))
user = stub('user', :new? => true, :valid? => false).as_null_object
db.stub(:valid_document?).and_return false
user = stub('user', :new? => true).as_null_object
user.should_receive(:database=).with(db)
db.save_document user
end
Expand Down Expand Up @@ -129,4 +130,41 @@ class Category
category.dirty?.should == true
end
end

describe "when saving documents with errors set in filters" do
class Vulcan
include CouchPotato::Persistence
before_validation_on_create :set_errors
before_validation_on_update :set_errors

property :name
validates_presence_of :name

def set_errors
errors.add(:validation, "failed")
end
end

it "should keep errors added in before_validation_on_* filters when creating a new object" do
spock = Vulcan.new(:name => 'spock')
CouchPotato.database.save_document(spock)
spock.errors.on(:validation).should == 'failed'
end

it "should keep errors added in before_validation_on_* filters when creating a new object" do
spock = Vulcan.new(:name => 'spock')
CouchPotato.database.save_document(spock, false)
spock.new_record?.should == false
spock.name = "spock's father"
CouchPotato.database.save_document(spock)
spock.errors.on(:validation).should == 'failed'
end

it "should keep errors generated from normal validations together with errors set in normal validations" do
spock = Vulcan.new
CouchPotato.database.save_document(spock)
spock.errors.on(:validation).should == 'failed'
spock.errors.on(:name).should == "can't be empty"
end
end
end

0 comments on commit db34bf9

Please sign in to comment.