From fafbfff474fdfde1ef0306f8cab306ba26d38369 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Tue, 24 Aug 2010 19:27:35 +0200 Subject: [PATCH] Ensuring update_attributes cannot be called in CastedModel as per issue #9 --- lib/couchrest/model/attributes.rb | 7 ------- lib/couchrest/model/persistence.rb | 14 ++++++++++++++ spec/couchrest/casted_model_spec.rb | 5 +++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/couchrest/model/attributes.rb b/lib/couchrest/model/attributes.rb index 40ddbc9..391cdb5 100644 --- a/lib/couchrest/model/attributes.rb +++ b/lib/couchrest/model/attributes.rb @@ -29,13 +29,6 @@ module CouchRest end alias :attributes= :update_attributes_without_saving - # Takes a hash as argument, and applies the values by using writer methods - # for each key. Raises a NoMethodError if the corresponding methods are - # missing. In case of error, no attributes are changed. - def update_attributes(hash) - update_attributes_without_saving hash - save - end private diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb index dea3e45..a48955a 100644 --- a/lib/couchrest/model/persistence.rb +++ b/lib/couchrest/model/persistence.rb @@ -62,6 +62,20 @@ module CouchRest end end + # Update the document's attributes and save. For example: + # + # doc.update_attributes :name => "Fred" + # + # Is the equivilent of doing the following: + # + # doc.attributes = { :name => "Fred" } + # doc.save + # + def update_attributes(hash) + update_attributes_without_saving hash + save + end + protected def perform_validations(options = {}) diff --git a/spec/couchrest/casted_model_spec.rb b/spec/couchrest/casted_model_spec.rb index 32ef3e0..b9f26e1 100644 --- a/spec/couchrest/casted_model_spec.rb +++ b/spec/couchrest/casted_model_spec.rb @@ -179,6 +179,11 @@ describe CouchRest::Model::CastedModel do @question.q.should == "What is your quest?" @question.a.should == "To seek the Holy Grail" end + + it "should raise an error if save or update_attributes called" do + expect { @question.save }.to raise_error + expect { @question.update_attributes(:q => "Fubar?") }.to raise_error + end end describe "saved document with casted models" do