Add Base#reload (closes #12)

Signed-off-by: Marcos Tapajós <tapajos@gmail.com>
This commit is contained in:
Simone Carletti 2011-02-09 14:31:52 +01:00 committed by Marcos Tapajós
parent 73c1b3d47b
commit 2c24702765
2 changed files with 40 additions and 1 deletions

View file

@ -76,7 +76,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

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