From c6be3fdf6415476e15e9f00736f00c6a3ab19489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Tapaj=C3=B3s?= Date: Thu, 24 Feb 2011 23:33:05 -0300 Subject: [PATCH 01/10] Lock Gemfile --- Gemfile.lock | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0f7d5b3..8ecf864 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) From f76517d0fa49103547ac76c94dd8e50fcc2c646b Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Tue, 22 Feb 2011 11:01:40 +0100 Subject: [PATCH 02/10] Narrow the rescued exception to avoid catching class evaluation errors that has nothing to to with the association. Example: /Users/weppos/.rvm/gems/ruby-1.8.7-p330/gems/couchrest_model-1.0.0/lib/couchrest/model/associations.rb:29:in `belongs_to': Unable to convert class name into Constant for Document#dossier (RuntimeError) although the real error is /Users/weppos/Projects/working/.../app/models/user.rb:56: undefined method `validates_uniqueness_of' for User:Class (NoMethodError) --- lib/couchrest/model/associations.rb | 4 ++-- spec/couchrest/assocations_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/couchrest/model/associations.rb b/lib/couchrest/model/associations.rb index 889ff5c..ccebcf8 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/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 From 44b56d3d03ab5dfb3f2cd75c47aa8e81e15f3067 Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Mon, 21 Feb 2011 11:41:40 +0100 Subject: [PATCH 03/10] Change Uniqueness validator message to follow ActiveRecord and other ORM behavior. Keeping the same message allows tools like Shoulda to work out of the box. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcos Tapajós --- lib/couchrest/model/validations/locale/en.yml | 2 +- spec/couchrest/validations.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/spec/couchrest/validations.rb b/spec/couchrest/validations.rb index ef01df6..063f62b 100644 --- a/spec/couchrest/validations.rb +++ b/spec/couchrest/validations.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 From f75893b927a65d9b0550ac5a9ad8f9a49c28b28f Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Mon, 21 Feb 2011 11:27:35 +0100 Subject: [PATCH 04/10] Fixed UniquenessValidator error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1) Validations Uniqueness should not validate a non-unique document Failure/Error: @obj.errors[:title].should eql(['is already taken']) expected ["is already taken"] got ["translation missing: en.activemodel.errors.models.with_unique_validation.attributes.title.taken"] (compared using eql?) Diff: @@ -1,2 +1,2 @@ -["is already taken"] +["translation missing: en.activemodel.errors.models.with_unique_validation.attributes.title.taken"] # ./spec/couchrest/validations_spec.rb:28:in `block (3 levels) in ' Signed-off-by: Marcos Tapajós --- lib/couchrest/model/validations/uniqueness.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/couchrest/model/validations/uniqueness.rb b/lib/couchrest/model/validations/uniqueness.rb index 755cdf3..e2fc794 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 From 73c1b3d47b87ecb858517accb431569e597e6f94 Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Mon, 21 Feb 2011 11:25:48 +0100 Subject: [PATCH 05/10] Validations spec are never executed due to an invalid filename pattern. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcos Tapajós --- spec/couchrest/{validations.rb => validations_spec.rb} | 2 -- 1 file changed, 2 deletions(-) rename spec/couchrest/{validations.rb => validations_spec.rb} (99%) diff --git a/spec/couchrest/validations.rb b/spec/couchrest/validations_spec.rb similarity index 99% rename from spec/couchrest/validations.rb rename to spec/couchrest/validations_spec.rb index 063f62b..661e563 100644 --- a/spec/couchrest/validations.rb +++ b/spec/couchrest/validations_spec.rb @@ -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 From 2c2470276524fc31901bacf5a4a0f96ed8d53a0a Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Wed, 9 Feb 2011 14:31:52 +0100 Subject: [PATCH 06/10] Add Base#reload (closes #12) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcos Tapajós --- lib/couchrest/model/persistence.rb | 12 +++++++++++- spec/couchrest/persistence_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb index 372148d..4f0c2bf 100644 --- a/lib/couchrest/model/persistence.rb +++ b/lib/couchrest/model/persistence.rb @@ -76,7 +76,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 diff --git a/spec/couchrest/persistence_spec.rb b/spec/couchrest/persistence_spec.rb index 01a3b86..a8bf3bf 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 From 608869be450176cc0bcd3bc45765db4deef5bd1f Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Wed, 9 Feb 2011 14:25:22 +0100 Subject: [PATCH 07/10] Fixed typo --- lib/couchrest/model/persistence.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb index 4f0c2bf..6a1ea66 100644 --- a/lib/couchrest/model/persistence.rb +++ b/lib/couchrest/model/persistence.rb @@ -65,8 +65,7 @@ module CouchRest # Update the document's attributes and save. For example: # # doc.update_attributes :name => "Fred" - # - # Is the equivilent of doing the following: + # Is the equivalent of doing the following: # # doc.attributes = { :name => "Fred" } # doc.save From 6b66e7f23e160ffb7b62e713220681c171304dbd Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Wed, 9 Feb 2011 14:22:54 +0100 Subject: [PATCH 08/10] Whitespaces. --- lib/couchrest/model/persistence.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb index 6a1ea66..9f67175 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,7 @@ module CouchRest # Update the document's attributes and save. For example: # # doc.update_attributes :name => "Fred" - # Is the equivalent of doing the following: + # Is the equivilent of doing the following: # # doc.attributes = { :name => "Fred" } # doc.save @@ -107,11 +107,11 @@ module CouchRest # a document instance def create_from_database(doc = {}) base = (doc[model_type_key].blank? || doc[model_type_key] == self.to_s) ? self : doc[model_type_key].constantize - base.new(doc, :directly_set_attributes => true) + 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 = {}) @@ -119,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 = {}) @@ -157,7 +157,7 @@ module CouchRest raise Errors::Validations.new(document) end end - + end end From 491d057035cc6376714d051cf91c1ef439d5b6f6 Mon Sep 17 00:00:00 2001 From: SengMing Tan Date: Mon, 21 Feb 2011 20:08:58 +1100 Subject: [PATCH 09/10] fixes typos in README MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcos Tapajós --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d64a990..1694aae 100644 --- a/README.md +++ b/README.md @@ -222,7 +222,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' @@ -261,7 +261,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. From 53b052f631646256480293cf20ad9ebd90fecb21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Tapaj=C3=B3s?= Date: Fri, 25 Feb 2011 00:05:32 -0300 Subject: [PATCH 10/10] Update History --- history.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/history.txt b/history.txt index 423c303..7ce64d8 100644 --- a/history.txt +++ b/history.txt @@ -2,6 +2,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