Skip to content

Commit

Permalink
changed the mongoid validation error to hold the document model and n…
Browse files Browse the repository at this point in the history
…ot the errors. updated the specs and some minor whitespace removal
  • Loading branch information
joshk authored and durran committed Jun 17, 2010
1 parent 0a7c2ea commit 69a560c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
3 changes: 1 addition & 2 deletions lib/mongoid/associations/embeds_many.rb
Expand Up @@ -82,8 +82,7 @@ def create(attrs = {}, type = nil)
# The newly created Document.
def create!(attrs = {}, type = nil)
document = create(attrs, type)
errors = document.errors
raise Errors::Validations.new(errors) unless errors.empty?
raise Errors::Validations.new(document) unless document.errors.empty?
document
end

Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/errors.rb
Expand Up @@ -61,10 +61,10 @@ def initialize(version)
#
# <tt>Validations.new(person.errors)</tt>
class Validations < MongoidError
attr_reader :errors
def initialize(errors)
@errors = errors
super("Validation Failed: #{@errors.full_messages.join(", ")}")
attr_reader :document
def initialize(document)
@document = document
super("Validation Failed: #{@document.errors.full_messages.join(", ")}")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/persistence.rb
Expand Up @@ -209,7 +209,7 @@ def destroy_all(conditions = {})

# Raise an error if validation failed.
def fail_validate!(document)
raise Errors::Validations.new(document.errors)
raise Errors::Validations.new(document)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions spec/unit/mongoid/associations/embeds_many_spec.rb
Expand Up @@ -215,7 +215,8 @@
Mongoid::Associations::Options.new(:name => :addresses)
)
@errors = mock(:full_messages => ["test"], :empty? => false)
@address = mock(:parentize => true, :write_attributes => true, :errors => @errors, :_index= => true)
@address = mock(:parentize => true, :write_attributes => true, :_index= => true)
@address.expects(:errors).twice.returns(@errors)
Address.expects(:instantiate).returns(@address)
end

Expand Down Expand Up @@ -492,13 +493,13 @@ def extension
Mongoid::Associations::Options.new(:name => :addresses)
)
end

it "should update existing documents" do
@association.nested_build({ "0" => { :street => "Yet Another" } })
@association.size.should == 2
@association[0].street.should == "Yet Another"
end

it "should create new documents" do
@association.nested_build({ "2" => { :street => "Yet Another" } })
@association.size.should == 3
Expand Down
9 changes: 7 additions & 2 deletions spec/unit/mongoid/errors_spec.rb
Expand Up @@ -73,13 +73,18 @@
context "default" do

before do
@errors = stub(:full_messages => [ "Error 1", "Error 2" ])
@error = Mongoid::Errors::Validations.new(@errors)
@errors = stub(:full_messages => [ "Error 1", "Error 2" ], :empty? => false)
@document = stub(:errors => @errors)
@error = Mongoid::Errors::Validations.new(@document)
end

it "contains the errors' full messages" do
@error.message.should == "Validation Failed: Error 1, Error 2"
end

it "allows access to the invalid document" do
@error.document.should == @document
end
end
end
end
Expand Down

0 comments on commit 69a560c

Please sign in to comment.