diff --git a/Gemfile.lock b/Gemfile.lock index 234bcf3..a0e9846 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,21 +12,21 @@ GEM remote: http://rubygems.org/ specs: abstract (1.0.0) - actionpack (3.0.3) - activemodel (= 3.0.3) - activesupport (= 3.0.3) + actionpack (3.0.4) + activemodel (= 3.0.4) + activesupport (= 3.0.4) builder (~> 2.1.2) erubis (~> 2.6.6) i18n (~> 0.4) rack (~> 1.2.1) rack-mount (~> 0.6.13) - rack-test (~> 0.5.6) + rack-test (~> 0.5.7) tzinfo (~> 0.3.23) - activemodel (3.0.3) - activesupport (= 3.0.3) + activemodel (3.0.4) + activesupport (= 3.0.4) builder (~> 2.1.2) i18n (~> 0.4) - activesupport (3.0.3) + activesupport (3.0.4) builder (2.1.2) couchrest (1.0.1) json (>= 1.4.6) @@ -36,16 +36,16 @@ GEM erubis (2.6.6) abstract (>= 1.0.0) i18n (0.5.0) - json (1.4.6) + json (1.5.1) mime-types (1.16) rack (1.2.1) rack-mount (0.6.13) rack (>= 1.0.0) rack-test (0.5.7) rack (>= 1.0) - railties (3.0.3) - actionpack (= 3.0.3) - activesupport (= 3.0.3) + railties (3.0.4) + actionpack (= 3.0.4) + activesupport (= 3.0.4) rake (>= 0.8.7) thor (~> 0.14.4) rake (0.8.7) @@ -60,17 +60,12 @@ GEM diff-lcs (~> 1.1.2) rspec-mocks (2.3.0) thor (0.14.6) - tzinfo (0.3.23) + tzinfo (0.3.24) PLATFORMS ruby DEPENDENCIES - activemodel (~> 3.0.0) - couchrest (~> 1.0.1) couchrest_model! - mime-types (~> 1.15) rack-test (>= 0.5.7) - railties (~> 3.0.0) rspec (>= 2.0.0) - tzinfo (~> 0.3.22) diff --git a/README.md b/README.md index 4e3729e..36985de 100644 --- a/README.md +++ b/README.md @@ -221,7 +221,7 @@ you'd like to use. For example: property :toys, [CatToy] end - @cat = Cat.new(:name => 'Felix', :toys => [{:name => 'mouse', :purchases => 1.month.ago}]) + @cat = Cat.new(:name => 'Felix', :toys => [{:name => 'mouse', :purchased => 1.month.ago}]) @cat.toys.first.class == CatToy @cat.toys.first.name == 'mouse' @@ -387,7 +387,7 @@ Two types at the moment: collection_of :tags -This is a somewhat controvesial feature of CouchRest Model that some document database purists may fringe at. CouchDB does not yet povide many features to support relationships between documents but the fact of that matter is that its a very useful paradigm for modelling data systems. +This is a somewhat controvesial feature of CouchRest Model that some document database purists may cringe at. CouchDB does not yet povide many features to support relationships between documents but the fact of that matter is that its a very useful paradigm for modelling data systems. In the near future we hope to add support for a `has_many` relationship that takes of the _Linked Documents_ feature that arrived in CouchDB 0.11. diff --git a/history.txt b/history.txt index 824b672..6d91715 100644 --- a/history.txt +++ b/history.txt @@ -6,6 +6,9 @@ * Minor enhancements: * A yield parameter in an anonymous casted model property block is no longer required (@samlown) + * Narrow the rescued exception to avoid catching class evaluation errors that has nothing to to with the association (thanks Simone Carletti) + * Fix validate uniqueness test that was never executed (thanks Simone Carletti) + * Adds a #reload method to reload document attributes (thanks Simone Carletti) == CouchRest Model 1.0.0 diff --git a/lib/couchrest/model/associations.rb b/lib/couchrest/model/associations.rb index ac02fda..e810c94 100644 --- a/lib/couchrest/model/associations.rb +++ b/lib/couchrest/model/associations.rb @@ -25,8 +25,8 @@ module CouchRest begin opts[:class] = opts[:class_name].constantize - rescue - raise "Unable to convert class name into Constant for #{self.name}##{attrib}" + rescue NameError + raise NameError, "Unable to convert class name into Constant for #{self.name}##{attrib}" end prop = property(opts[:foreign_key], opts) diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb index fbc35ba..bc6494c 100644 --- a/lib/couchrest/model/persistence.rb +++ b/lib/couchrest/model/persistence.rb @@ -3,7 +3,7 @@ module CouchRest module Persistence extend ActiveSupport::Concern - # Create the document. Validation is enabled by default and will return + # Create the document. Validation is enabled by default and will return # false if the document is not valid. If all goes well, the document will # be returned. def create(options = {}) @@ -16,13 +16,13 @@ module CouchRest end end end - + # Creates the document in the db. Raises an exception # if the document is not created properly. def create! self.class.fail_validate!(self) unless self.create end - + # Trigger the callbacks (before, after, around) # only if the document isn't new def update(options = {}) @@ -35,12 +35,12 @@ module CouchRest end end end - + # Trigger the callbacks (before, after, around) and save the document def save(options = {}) self.new? ? create(options) : update(options) end - + # Saves the document to the db using save. Raises an exception # if the document is not saved properly. def save! @@ -65,7 +65,6 @@ module CouchRest # 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" } @@ -76,7 +75,17 @@ module CouchRest save end - protected + # Reloads the attributes of this object from the database. + # It doesn't override custom instance variables. + # + # Returns self. + def reload + merge!(self.class.get(id)) + self + end + + + protected def perform_validations(options = {}) perform_validation = case options @@ -101,8 +110,8 @@ module CouchRest base.new(doc, :directly_set_attributes => true) end - # Defines an instance and save it directly to the database - # + # Defines an instance and save it directly to the database + # # ==== Returns # returns the reloaded document def create(attributes = {}) @@ -110,9 +119,9 @@ module CouchRest instance.create instance end - - # Defines an instance and save it directly to the database - # + + # Defines an instance and save it directly to the database + # # ==== Returns # returns the reloaded document or raises an exception def create!(attributes = {}) @@ -148,7 +157,7 @@ module CouchRest raise Errors::Validations.new(document) end end - + end end diff --git a/lib/couchrest/model/validations/locale/en.yml b/lib/couchrest/model/validations/locale/en.yml index 942261f..016350b 100644 --- a/lib/couchrest/model/validations/locale/en.yml +++ b/lib/couchrest/model/validations/locale/en.yml @@ -1,5 +1,5 @@ en: errors: messages: - taken: "is already taken" + taken: "has already been taken" diff --git a/lib/couchrest/model/validations/uniqueness.rb b/lib/couchrest/model/validations/uniqueness.rb index 76777c7..19335f2 100644 --- a/lib/couchrest/model/validations/uniqueness.rb +++ b/lib/couchrest/model/validations/uniqueness.rb @@ -32,7 +32,7 @@ module CouchRest end if docs.length > 0 - document.errors.add(attribute, :taken, :default => options[:message], :value => value) + document.errors.add(attribute, :taken, options.merge(:value => value)) end end diff --git a/spec/couchrest/assocations_spec.rb b/spec/couchrest/assocations_spec.rb index fe16970..36e0e5d 100644 --- a/spec/couchrest/assocations_spec.rb +++ b/spec/couchrest/assocations_spec.rb @@ -44,11 +44,11 @@ describe "Assocations" do end it "should raise error if class name does not exist" do - lambda { + lambda do class TestBadAssoc < CouchRest::Model::Base belongs_to :test_bad_item end - }.should raise_error + end.should raise_error(NameError, /TestBadAssoc#test_bad_item/) end it "should allow override of foreign key" do diff --git a/spec/couchrest/persistence_spec.rb b/spec/couchrest/persistence_spec.rb index 8e360d9..a502890 100644 --- a/spec/couchrest/persistence_spec.rb +++ b/spec/couchrest/persistence_spec.rb @@ -412,4 +412,33 @@ describe "Model Persistence" do end + describe "#reload" do + it "reloads defined attributes" do + i = Article.create!(:title => "Reload when changed") + i.title.should == "Reload when changed" + + i.title = "..." + i.title.should == "..." + + i.reload + i.title.should == "Reload when changed" + end + + it "reloads defined attributes set to nil" do + i = Article.create!(:title => "Reload when nil") + i.title.should == "Reload when nil" + + i.title = nil + i.title.should be_nil + + i.reload + i.title.should == "Reload when nil" + end + + it "returns self" do + i = Article.create!(:title => "Reload return self") + i.reload.should be(i) + end + end + end diff --git a/spec/couchrest/validations.rb b/spec/couchrest/validations_spec.rb similarity index 97% rename from spec/couchrest/validations.rb rename to spec/couchrest/validations_spec.rb index ef01df6..661e563 100644 --- a/spec/couchrest/validations.rb +++ b/spec/couchrest/validations_spec.rb @@ -25,7 +25,7 @@ describe "Validations" do it "should not validate a non-unique document" do @obj = WithUniqueValidation.create(:title => 'title 1') @obj.should_not be_valid - @obj.errors[:title].should eql(['is already taken']) + @obj.errors[:title].should == ["has already been taken"] end it "should save already created document" do @@ -57,7 +57,6 @@ describe "Validations" do @obj.class.should_receive('view').and_return({'rows' => [ ]}) @obj.valid? end - end context "with a proxy parameter" do @@ -76,7 +75,6 @@ describe "Validations" do proxy.should_receive('view').and_return({'rows' => [ ]}) @obj.valid? end - end