some optimisations

This commit is contained in:
Andrew Williams 2011-03-01 22:06:42 +10:30
parent 4e8e5ee0d1
commit ce2e2fc9a6
4 changed files with 33 additions and 19 deletions

View file

@ -18,9 +18,7 @@ module CouchRest
def couchrest_attribute_will_change!(attr)
return if attr.nil?
self.send("#{attr}_will_change!")
if pkey = casted_by_attribute
@casted_by.couchrest_attribute_will_change!(pkey)
end
couchrest_parent_will_change!
end
def couchrest_parent_will_change!
@ -31,9 +29,9 @@ module CouchRest
# return the attribute name this object is referenced by in the parent
def casted_by_attribute
return nil unless @casted_by
return @casted_by_attribute if @casted_by_attribute_set
attr = @casted_by.attributes
attr.keys.detect { |k| attr[k] == self }
@casted_by_attribute = attr.keys.detect { |k| attr[k] == self }
end
end

View file

@ -53,16 +53,22 @@ module CouchRest
end
def []=(key,value)
old_id = get_unique_id if self.respond_to?(:get_unique_id)
has_changes = self.changed?
if !has_changes && self.respond_to?(:get_unique_id)
check_id_change = true
old_id = get_unique_id
end
super(key, value)
ret = super(key, value)
if self.respond_to?(:get_unique_id)
if check_id_change
# if we have set an attribute that results in the _id changing (unique_id),
# force changed? to return true so that the record can be saved
new_id = get_unique_id
changed_attributes["_id"] = new_id if old_id != new_id
end
ret
end
# Takes a hash as argument, and applies the values by using writer methods
@ -181,8 +187,8 @@ module CouchRest
property(:created_at, Time, :read_only => true, :protected => true, :auto_validation => false)
set_callback :save, :before do |object|
write_attribute_dirty('updated_at', Time.now)
write_attribute_dirty('created_at', Time.now) if object.new?
write_attribute('updated_at', Time.now)
write_attribute('created_at', Time.now) if object.new?
end
EOS
end