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:
parent
f244b51fbf
commit
0bb00860d1
|
@ -50,6 +50,16 @@ module CouchRest::Model
|
|||
super
|
||||
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)
|
||||
obj = casted_by_property.build(*args)
|
||||
self.push(obj)
|
||||
|
|
|
@ -257,6 +257,50 @@ describe "Dirty" do
|
|||
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
|
||||
should_change_array do |array, obj|
|
||||
array.push("keyword")
|
||||
|
|
Loading…
Reference in a new issue