now using ActiveModel::Dirty. only writes to database if model.changed?

This commit is contained in:
Andrew Williams 2011-03-01 01:30:41 +10:30
parent 53b052f631
commit 4dbf694e51
14 changed files with 342 additions and 18 deletions

View file

@ -12,7 +12,9 @@ module CouchRest
_run_save_callbacks do
set_unique_id if new? && self.respond_to?(:set_unique_id)
result = database.save_doc(self)
(result["ok"] == true) ? self : false
ret = (result["ok"] == true) ? self : false
@changed_attributes.clear if ret && @changed_attributes
ret
end
end
end
@ -28,10 +30,13 @@ module CouchRest
def update(options = {})
raise "Calling #{self.class.name}#update on document that has not been created!" if self.new?
return false unless perform_validations(options)
return true unless self.changed?
_run_update_callbacks do
_run_save_callbacks do
result = database.save_doc(self)
result["ok"] == true
ret = result["ok"] == true
@changed_attributes.clear if ret && @changed_attributes
ret
end
end
end
@ -140,12 +145,18 @@ module CouchRest
# should use the class name as part of the unique id.
def unique_id method = nil, &block
if method
define_method :get_unique_id do
self.send(method)
end
define_method :set_unique_id do
self['_id'] ||= self.send(method)
self['_id'] ||= get_unique_id
end
elsif block
define_method :get_unique_id do
block.call(self)
end
define_method :set_unique_id do
uniqid = block.call(self)
uniqid = get_unique_id
raise ArgumentError, "unique_id block must not return nil" if uniqid.nil?
self['_id'] ||= uniqid
end