lib/gitmodel/persistable.rb in gitmodel-0.0.6 vs lib/gitmodel/persistable.rb in gitmodel-0.0.7
- old
+ new
@@ -52,10 +52,27 @@
def id=(string)
# TODO ensure is valid as a filename
@id = string
end
+ # Get the location of the record relative to the repository's root.
+ #
+ # It is determined by appending the name of the directory containing
+ # the record with the record's +id+.
+ def path
+ @path ||= File.join(self.class.db_subdir, self.id)
+ end
+
+ # Get the branch that the record was last loaded from or was last
+ # saved on.
+ #
+ # The branch specified in the +GitModel+ config is used by default.
+ # Typically, the branch is 'master'.
+ def branch
+ @branch ||= GitModel.default_branch
+ end
+
def attributes
@attributes
end
def attributes=(new_attributes, guard_protected_attributes = true)
@@ -113,19 +130,34 @@
blobs.each do |name, data|
t.index.add(File.join(dir, name), data)
end
end
+ if result
+ @path = dir
+ @branch = transaction.branch
+ @new_record = false
+ end
+
result
end
end
# Same as #save but raises an exception on error
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)
end
@@ -139,9 +171,12 @@
def load(dir, branch)
_run_find_callbacks do
# remove dangerous ".."
# 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)
@new_record = false