Fixing issues with dirty tracking on nested models and related improvements
This commit is contained in:
parent
8c4505d191
commit
2cc119b3b3
15 changed files with 141 additions and 115 deletions
|
@ -5,25 +5,36 @@
|
|||
|
||||
module CouchRest::Model
|
||||
class CastedArray < Array
|
||||
include CouchRest::Model::CastedBy
|
||||
include CouchRest::Model::Dirty
|
||||
attr_accessor :casted_by
|
||||
attr_accessor :property
|
||||
attr_accessor :casted_by_property
|
||||
|
||||
def initialize(array, property)
|
||||
self.property = property
|
||||
def initialize(array, property, parent = nil)
|
||||
self.casted_by_property = property
|
||||
self.casted_by = parent unless parent.nil?
|
||||
super(array)
|
||||
end
|
||||
|
||||
|
||||
# Adding new entries
|
||||
|
||||
def << obj
|
||||
couchrest_parent_will_change! if use_dirty?
|
||||
super(instantiate_and_cast(obj))
|
||||
end
|
||||
|
||||
|
||||
def push(obj)
|
||||
couchrest_parent_will_change! if use_dirty?
|
||||
super(instantiate_and_cast(obj))
|
||||
end
|
||||
|
||||
def unshift(obj)
|
||||
super(instantiate_and_cast(obj))
|
||||
end
|
||||
|
||||
def []= index, obj
|
||||
value = instantiate_and_cast(obj, false)
|
||||
couchrest_parent_will_change! if use_dirty? && value != self[index]
|
||||
super(index, value)
|
||||
end
|
||||
|
||||
def pop
|
||||
couchrest_parent_will_change! if use_dirty? && self.length > 0
|
||||
super
|
||||
|
@ -34,17 +45,6 @@ module CouchRest::Model
|
|||
super
|
||||
end
|
||||
|
||||
def unshift(obj)
|
||||
couchrest_parent_will_change! if use_dirty?
|
||||
super(instantiate_and_cast(obj))
|
||||
end
|
||||
|
||||
def []= index, obj
|
||||
value = instantiate_and_cast(obj)
|
||||
couchrest_parent_will_change! if use_dirty? && value != self[index]
|
||||
super(index, value)
|
||||
end
|
||||
|
||||
def clear
|
||||
couchrest_parent_will_change! if use_dirty? && self.length > 0
|
||||
super
|
||||
|
@ -52,11 +52,14 @@ module CouchRest::Model
|
|||
|
||||
protected
|
||||
|
||||
def instantiate_and_cast(obj)
|
||||
if self.casted_by && self.property && obj.class != self.property.type_class
|
||||
self.property.cast_value(self.casted_by, obj)
|
||||
def instantiate_and_cast(obj, change = true)
|
||||
property = casted_by_property
|
||||
couchrest_parent_will_change! if change && use_dirty?
|
||||
if casted_by && property && obj.class != property.type_class
|
||||
property.cast_value(casted_by, obj)
|
||||
else
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
obj.casted_by = casted_by if obj.respond_to?(:casted_by)
|
||||
obj.casted_by_property = casted_by_property if obj.respond_to?(:casted_by_property)
|
||||
obj
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue