mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Setting both values to blank deletes override
This commit is contained in:
@@ -5,8 +5,8 @@ class ModelSet
|
||||
|
||||
attr_accessor :collection
|
||||
|
||||
def initialize(klass, collection, attributes={}, reject_if=nil)
|
||||
@klass, @collection, @reject_if = klass, collection, reject_if
|
||||
def initialize(klass, collection, attributes={}, reject_if=nil, delete_if=nil)
|
||||
@klass, @collection, @reject_if, @delete_if = klass, collection, reject_if, delete_if
|
||||
|
||||
# Set here first, to ensure that we apply collection_attributes to the right collection
|
||||
@collection = attributes[:collection] if attributes[:collection]
|
||||
@@ -36,7 +36,16 @@ class ModelSet
|
||||
end
|
||||
|
||||
def save
|
||||
collection.all?(&:save)
|
||||
collection_to_delete.each &:destroy
|
||||
collection_to_keep.all? &:save
|
||||
end
|
||||
|
||||
def collection_to_delete
|
||||
collection.select { |e| @delete_if.andand.call(e.attributes) }
|
||||
end
|
||||
|
||||
def collection_to_keep
|
||||
collection.reject { |e| @delete_if.andand.call(e.attributes) }
|
||||
end
|
||||
|
||||
def persisted?
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class VariantOverrideSet < ModelSet
|
||||
def initialize(attributes={})
|
||||
super(VariantOverride, VariantOverride.all, attributes)
|
||||
super(VariantOverride, VariantOverride.all, attributes, nil,
|
||||
proc { |attrs| attrs['price'].blank? && attrs['count_on_hand'].blank? } )
|
||||
end
|
||||
end
|
||||
|
||||
@@ -142,6 +142,19 @@ feature %q{
|
||||
vo.price.should == 22.22
|
||||
vo.count_on_hand.should == 8888
|
||||
end
|
||||
|
||||
it "deletes overrides when values are cleared" do
|
||||
fill_in "variant-overrides-#{variant.id}-price", with: ''
|
||||
fill_in "variant-overrides-#{variant.id}-count-on-hand", with: ''
|
||||
page.should have_content "Changes to one override remain unsaved."
|
||||
|
||||
expect do
|
||||
click_button 'Save Changes'
|
||||
page.should have_content "Changes saved."
|
||||
end.to change(VariantOverride, :count).by(-1)
|
||||
|
||||
VariantOverride.where(id: vo.id).should be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
58
spec/models/model_set_spec.rb
Normal file
58
spec/models/model_set_spec.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe ModelSet do
|
||||
describe "updating" do
|
||||
it "creates new models" do
|
||||
attrs = {collection_attributes: {'1' => {name: 'e1', description: 'foo'},
|
||||
'2' => {name: 'e2', description: 'bar'}}}
|
||||
|
||||
ms = ModelSet.new(EnterpriseGroup, EnterpriseGroup.all, attrs)
|
||||
|
||||
expect { ms.save }.to change(EnterpriseGroup, :count).by(2)
|
||||
|
||||
EnterpriseGroup.where(name: ['e1', 'e2']).count.should == 2
|
||||
end
|
||||
|
||||
|
||||
it "updates existing models" do
|
||||
e1 = create(:enterprise_group)
|
||||
e2 = create(:enterprise_group)
|
||||
|
||||
attrs = {collection_attributes: {'1' => {id: e1.id, name: 'e1zz', description: 'foo'},
|
||||
'2' => {id: e2.id, name: 'e2yy', description: 'bar'}}}
|
||||
|
||||
ms = ModelSet.new(EnterpriseGroup, EnterpriseGroup.all, attrs)
|
||||
|
||||
expect { ms.save }.to change(EnterpriseGroup, :count).by(0)
|
||||
|
||||
EnterpriseGroup.where(name: ['e1zz', 'e2yy']).count.should == 2
|
||||
end
|
||||
|
||||
|
||||
it "destroys deleted models" do
|
||||
e1 = create(:enterprise)
|
||||
e2 = create(:enterprise)
|
||||
|
||||
attrs = {collection_attributes: {'1' => {id: e1.id, name: 'deleteme'},
|
||||
'2' => {id: e2.id, name: 'e2'}}}
|
||||
|
||||
ms = ModelSet.new(Enterprise, Enterprise.all, attrs, nil,
|
||||
proc { |attrs| attrs['name'] == 'deleteme' })
|
||||
|
||||
expect { ms.save }.to change(Enterprise, :count).by(-1)
|
||||
|
||||
Enterprise.where(id: e1.id).should be_empty
|
||||
Enterprise.where(id: e2.id).should be_present
|
||||
end
|
||||
|
||||
|
||||
it "ignores deletable new records" do
|
||||
attrs = {collection_attributes: {'1' => {name: 'deleteme'}}}
|
||||
|
||||
ms = ModelSet.new(Enterprise, Enterprise.all, attrs, nil,
|
||||
proc { |attrs| attrs['name'] == 'deleteme' })
|
||||
|
||||
expect { ms.save }.to change(Enterprise, :count).by(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user