Casted array should notify changes on deletion

Otherwise there is no direct way to delete particular elements.

Workaround with assigning to another array without those elements
is pretty ugly.

* Should notify on deletion at a particular index (Array#delete_at)
* Should notify on deletion of a particular element (Array#delete)

Signed-off-by: Marcos Tapajós <tapajos@gmail.com>
This commit is contained in:
Kostiantyn Kahanskyi 2011-06-06 11:55:15 +02:00 committed by Marcos Tapajós
parent f244b51fbf
commit 0bb00860d1
2 changed files with 54 additions and 0 deletions

View file

@ -50,6 +50,16 @@ module CouchRest::Model
super super
end end
def delete(obj)
couchrest_parent_will_change! if use_dirty? && self.length > 0
super(obj)
end
def delete_at(index)
couchrest_parent_will_change! if use_dirty? && self.length > 0
super(index)
end
def build(*args) def build(*args)
obj = casted_by_property.build(*args) obj = casted_by_property.build(*args)
self.push(obj) self.push(obj)

View file

@ -257,6 +257,50 @@ describe "Dirty" do
end end
end end
it "should report changes on deletion from an array" do
should_change_array do |array, obj|
array << "keyword"
obj.save!
array.delete_at(0)
end
should_change_array do |array, obj|
array << "keyword"
obj.save!
array.delete("keyword")
end
end
it "should report changes on deletion from an array after reload" do
should_change_array do |array, obj|
array << "keyword"
obj.save!
obj.reload
array.delete_at(0)
end
should_change_array do |array, obj|
array << "keyword"
obj.save!
obj.reload
array.delete("keyword")
end
end
it "should report no changes on deletion from an empty array" do
should_not_change_array do |array, obj|
array.clear
obj.save!
array.delete_at(0)
end
should_not_change_array do |array, obj|
array.clear
obj.save!
array.delete("keyword")
end
end
it "should report changes if an array is pushed" do it "should report changes if an array is pushed" do
should_change_array do |array, obj| should_change_array do |array, obj|
array.push("keyword") array.push("keyword")