From 31823f2dbdcb241f94d0f0bbdd08e94b5032c492 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 12 Dec 2014 11:44:35 +1100 Subject: [PATCH] Setting both values to blank deletes override --- app/models/model_set.rb | 15 ++++- app/models/variant_override_set.rb | 3 +- spec/features/admin/variant_overrides_spec.rb | 13 +++++ spec/models/model_set_spec.rb | 58 +++++++++++++++++++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 spec/models/model_set_spec.rb diff --git a/app/models/model_set.rb b/app/models/model_set.rb index 774b94beb8..c9e1456f1a 100644 --- a/app/models/model_set.rb +++ b/app/models/model_set.rb @@ -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? diff --git a/app/models/variant_override_set.rb b/app/models/variant_override_set.rb index bdd0301dcb..fc02173862 100644 --- a/app/models/variant_override_set.rb +++ b/app/models/variant_override_set.rb @@ -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 diff --git a/spec/features/admin/variant_overrides_spec.rb b/spec/features/admin/variant_overrides_spec.rb index 3cacbd7490..3ca116a5a7 100644 --- a/spec/features/admin/variant_overrides_spec.rb +++ b/spec/features/admin/variant_overrides_spec.rb @@ -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 diff --git a/spec/models/model_set_spec.rb b/spec/models/model_set_spec.rb new file mode 100644 index 0000000000..24f452412f --- /dev/null +++ b/spec/models/model_set_spec.rb @@ -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