Merge branch 'master' of github.com:couchrest/couchrest_model into adv_design

This commit is contained in:
Sam Lown 2011-02-27 20:19:09 +01:00
commit f58482553c
10 changed files with 75 additions and 41 deletions

View file

@ -12,21 +12,21 @@ GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
abstract (1.0.0) abstract (1.0.0)
actionpack (3.0.3) actionpack (3.0.4)
activemodel (= 3.0.3) activemodel (= 3.0.4)
activesupport (= 3.0.3) activesupport (= 3.0.4)
builder (~> 2.1.2) builder (~> 2.1.2)
erubis (~> 2.6.6) erubis (~> 2.6.6)
i18n (~> 0.4) i18n (~> 0.4)
rack (~> 1.2.1) rack (~> 1.2.1)
rack-mount (~> 0.6.13) rack-mount (~> 0.6.13)
rack-test (~> 0.5.6) rack-test (~> 0.5.7)
tzinfo (~> 0.3.23) tzinfo (~> 0.3.23)
activemodel (3.0.3) activemodel (3.0.4)
activesupport (= 3.0.3) activesupport (= 3.0.4)
builder (~> 2.1.2) builder (~> 2.1.2)
i18n (~> 0.4) i18n (~> 0.4)
activesupport (3.0.3) activesupport (3.0.4)
builder (2.1.2) builder (2.1.2)
couchrest (1.0.1) couchrest (1.0.1)
json (>= 1.4.6) json (>= 1.4.6)
@ -36,16 +36,16 @@ GEM
erubis (2.6.6) erubis (2.6.6)
abstract (>= 1.0.0) abstract (>= 1.0.0)
i18n (0.5.0) i18n (0.5.0)
json (1.4.6) json (1.5.1)
mime-types (1.16) mime-types (1.16)
rack (1.2.1) rack (1.2.1)
rack-mount (0.6.13) rack-mount (0.6.13)
rack (>= 1.0.0) rack (>= 1.0.0)
rack-test (0.5.7) rack-test (0.5.7)
rack (>= 1.0) rack (>= 1.0)
railties (3.0.3) railties (3.0.4)
actionpack (= 3.0.3) actionpack (= 3.0.4)
activesupport (= 3.0.3) activesupport (= 3.0.4)
rake (>= 0.8.7) rake (>= 0.8.7)
thor (~> 0.14.4) thor (~> 0.14.4)
rake (0.8.7) rake (0.8.7)
@ -60,17 +60,12 @@ GEM
diff-lcs (~> 1.1.2) diff-lcs (~> 1.1.2)
rspec-mocks (2.3.0) rspec-mocks (2.3.0)
thor (0.14.6) thor (0.14.6)
tzinfo (0.3.23) tzinfo (0.3.24)
PLATFORMS PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
activemodel (~> 3.0.0)
couchrest (~> 1.0.1)
couchrest_model! couchrest_model!
mime-types (~> 1.15)
rack-test (>= 0.5.7) rack-test (>= 0.5.7)
railties (~> 3.0.0)
rspec (>= 2.0.0) rspec (>= 2.0.0)
tzinfo (~> 0.3.22)

View file

@ -221,7 +221,7 @@ you'd like to use. For example:
property :toys, [CatToy] property :toys, [CatToy]
end 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.class == CatToy
@cat.toys.first.name == 'mouse' @cat.toys.first.name == 'mouse'
@ -387,7 +387,7 @@ Two types at the moment:
collection_of :tags 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. 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.

View file

@ -6,6 +6,9 @@
* Minor enhancements: * Minor enhancements:
* A yield parameter in an anonymous casted model property block is no longer required (@samlown) * 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 == CouchRest Model 1.0.0

View file

@ -25,8 +25,8 @@ module CouchRest
begin begin
opts[:class] = opts[:class_name].constantize opts[:class] = opts[:class_name].constantize
rescue rescue NameError
raise "Unable to convert class name into Constant for #{self.name}##{attrib}" raise NameError, "Unable to convert class name into Constant for #{self.name}##{attrib}"
end end
prop = property(opts[:foreign_key], opts) prop = property(opts[:foreign_key], opts)

View file

@ -3,7 +3,7 @@ module CouchRest
module Persistence module Persistence
extend ActiveSupport::Concern 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 # false if the document is not valid. If all goes well, the document will
# be returned. # be returned.
def create(options = {}) def create(options = {})
@ -16,13 +16,13 @@ module CouchRest
end end
end end
end end
# Creates the document in the db. Raises an exception # Creates the document in the db. Raises an exception
# if the document is not created properly. # if the document is not created properly.
def create! def create!
self.class.fail_validate!(self) unless self.create self.class.fail_validate!(self) unless self.create
end end
# Trigger the callbacks (before, after, around) # Trigger the callbacks (before, after, around)
# only if the document isn't new # only if the document isn't new
def update(options = {}) def update(options = {})
@ -35,12 +35,12 @@ module CouchRest
end end
end end
end end
# Trigger the callbacks (before, after, around) and save the document # Trigger the callbacks (before, after, around) and save the document
def save(options = {}) def save(options = {})
self.new? ? create(options) : update(options) self.new? ? create(options) : update(options)
end end
# Saves the document to the db using save. Raises an exception # Saves the document to the db using save. Raises an exception
# if the document is not saved properly. # if the document is not saved properly.
def save! def save!
@ -65,7 +65,6 @@ module CouchRest
# Update the document's attributes and save. For example: # Update the document's attributes and save. For example:
# #
# doc.update_attributes :name => "Fred" # doc.update_attributes :name => "Fred"
#
# Is the equivilent of doing the following: # Is the equivilent of doing the following:
# #
# doc.attributes = { :name => "Fred" } # doc.attributes = { :name => "Fred" }
@ -76,7 +75,17 @@ module CouchRest
save save
end 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 = {}) def perform_validations(options = {})
perform_validation = case options perform_validation = case options
@ -101,8 +110,8 @@ module CouchRest
base.new(doc, :directly_set_attributes => true) base.new(doc, :directly_set_attributes => true)
end end
# Defines an instance and save it directly to the database # Defines an instance and save it directly to the database
# #
# ==== Returns # ==== Returns
# returns the reloaded document # returns the reloaded document
def create(attributes = {}) def create(attributes = {})
@ -110,9 +119,9 @@ module CouchRest
instance.create instance.create
instance instance
end end
# Defines an instance and save it directly to the database # Defines an instance and save it directly to the database
# #
# ==== Returns # ==== Returns
# returns the reloaded document or raises an exception # returns the reloaded document or raises an exception
def create!(attributes = {}) def create!(attributes = {})
@ -148,7 +157,7 @@ module CouchRest
raise Errors::Validations.new(document) raise Errors::Validations.new(document)
end end
end end
end end
end end

View file

@ -1,5 +1,5 @@
en: en:
errors: errors:
messages: messages:
taken: "is already taken" taken: "has already been taken"

View file

@ -32,7 +32,7 @@ module CouchRest
end end
if docs.length > 0 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
end end

View file

@ -44,11 +44,11 @@ describe "Assocations" do
end end
it "should raise error if class name does not exist" do it "should raise error if class name does not exist" do
lambda { lambda do
class TestBadAssoc < CouchRest::Model::Base class TestBadAssoc < CouchRest::Model::Base
belongs_to :test_bad_item belongs_to :test_bad_item
end end
}.should raise_error end.should raise_error(NameError, /TestBadAssoc#test_bad_item/)
end end
it "should allow override of foreign key" do it "should allow override of foreign key" do

View file

@ -412,4 +412,33 @@ describe "Model Persistence" do
end 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 end

View file

@ -25,7 +25,7 @@ describe "Validations" do
it "should not validate a non-unique document" do it "should not validate a non-unique document" do
@obj = WithUniqueValidation.create(:title => 'title 1') @obj = WithUniqueValidation.create(:title => 'title 1')
@obj.should_not be_valid @obj.should_not be_valid
@obj.errors[:title].should eql(['is already taken']) @obj.errors[:title].should == ["has already been taken"]
end end
it "should save already created document" do it "should save already created document" do
@ -57,7 +57,6 @@ describe "Validations" do
@obj.class.should_receive('view').and_return({'rows' => [ ]}) @obj.class.should_receive('view').and_return({'rows' => [ ]})
@obj.valid? @obj.valid?
end end
end end
context "with a proxy parameter" do context "with a proxy parameter" do
@ -76,7 +75,6 @@ describe "Validations" do
proxy.should_receive('view').and_return({'rows' => [ ]}) proxy.should_receive('view').and_return({'rows' => [ ]})
@obj.valid? @obj.valid?
end end
end end