Add Base#reload (closes #12)
Signed-off-by: Marcos Tapajós <tapajos@gmail.com>
This commit is contained in:
parent
73c1b3d47b
commit
2c24702765
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue