Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add #reload for reloading model instances

  • Loading branch information...
commit 47e4ff259b9e74cd3543473cf0fc2c7d8e83353f 1 parent e4f4c54
@drusso drusso authored committed
Showing with 46 additions and 0 deletions.
  1. +18 −0 lib/gitmodel/persistable.rb
  2. +28 −0 spec/gitmodel/persistable_spec.rb
View
18 lib/gitmodel/persistable.rb
@@ -132,6 +132,12 @@ def save(options = {})
end
end
+ if result
+ @path = dir
+ @branch = transaction.branch
+ @new_record = false
+ end
+
result
end
end
@@ -141,6 +147,15 @@ def save!(options = {})
save(options) || raise(GitModel::RecordNotSaved)
end
+ # Reloads a record and returns the model instance.
+ #
+ # The record is reloaded from the branch that the record
+ # was last loaded from or last saved to.
+ def reload
+ load(path, branch)
+ self
+ end
+
def delete(options = {})
freeze
self.class.delete(id, options)
@@ -159,6 +174,9 @@ def load(dir, branch)
# todo find a better way to ensure path is safe
dir.gsub!(/\.\./, '')
+ @path = dir
+ @branch = branch
+
raise GitModel::RecordNotFound if GitModel.current_tree(branch).nil?
self.id = File.basename(dir)
View
28 spec/gitmodel/persistable_spec.rb
@@ -118,6 +118,34 @@
end
end
+ describe'#reload' do
+
+ it 'reloads persisted attributes' do
+ o = TestEntity.new(:id => 'foo')
+ o.attributes['A'] = 'A'
+ o.save
+ o.attributes['B'] = 'B'
+
+ o.attributes['A'].should == 'A'
+ o.attributes['B'].should == 'B'
+ o.reload
+ o.attributes['A'].should == 'A'
+ o.attributes['B'].should == nil
+ end
+
+ it 'returns the reloaded instance' do
+ o = TestEntity.create(:id => 'foo')
+ p = o.reload
+ o.should == p
+ end
+
+ it 'raises an exception if the document is not found' do
+ lambda { TestEntity.new.reload }.should raise_error
+ lambda { TestEntity.new(:id => "foo").reload }.should raise_error
+ end
+
+ end
+
describe '.create' do
it 'creates a new instance with the given parameters and calls #save on it' do
Please sign in to comment.
Something went wrong with that request. Please try again.