From d7cce918f5265eb0da976e530baa82c1132fc3c6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 12 Aug 2013 14:22:11 +1000 Subject: [PATCH] Instead of protecting shipping methods from destruction when referenced by product distributions, protect enterprise fees --- .../admin/enterprise_fees_controller.rb | 17 +++++++++++++- spec/features/admin/enterprise_fees_spec.rb | 23 +++++++++++++++++++ spec/features/admin/shipping_methods_spec.rb | 11 --------- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/app/controllers/admin/enterprise_fees_controller.rb b/app/controllers/admin/enterprise_fees_controller.rb index 890cc5b08f..41450c82c9 100644 --- a/app/controllers/admin/enterprise_fees_controller.rb +++ b/app/controllers/admin/enterprise_fees_controller.rb @@ -2,6 +2,8 @@ module Admin class EnterpriseFeesController < ResourceController before_filter :load_enterprise_fee_set, :only => :index before_filter :load_data + before_filter :do_not_destroy_referenced_fees, :only => :destroy + def index respond_to do |format| @@ -21,6 +23,20 @@ module Admin private + + def do_not_destroy_referenced_fees + product_distribution = ProductDistribution.where(:enterprise_fee_id => @object).first + if product_distribution + p = product_distribution.product + flash[:error] = "That enterprise fee cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}." + + respond_with(@object) do |format| + format.html { redirect_to collection_url } + format.js { render text: flash[:error], status: 403 } + end + end + end + def load_enterprise_fee_set @enterprise_fee_set = EnterpriseFeeSet.new :collection => collection end @@ -32,6 +48,5 @@ module Admin def collection super.order('enterprise_id', 'fee_type', 'name') end - end end diff --git a/spec/features/admin/enterprise_fees_spec.rb b/spec/features/admin/enterprise_fees_spec.rb index 2f3c9e64fd..e2af5bb65e 100644 --- a/spec/features/admin/enterprise_fees_spec.rb +++ b/spec/features/admin/enterprise_fees_spec.rb @@ -99,4 +99,27 @@ feature %q{ page.should_not have_selector "input[value='#{fee.name}']" end + scenario "deleting a shipping method referenced by a product distribution" do + # Given an enterprise fee referenced by a product distribution + fee = create(:enterprise_fee) + p = create(:product) + d = create(:distributor_enterprise) + create(:product_distribution, product: p, distributor: d, enterprise_fee: fee) + + # When I go to the enterprise fees page + login_to_admin_section + click_link 'Configuration' + click_link 'Enterprise Fees' + + # And I click delete + find("a.delete-resource").click + + # Then I should see an error + page.should have_content "That enterprise fee cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}." + + # And my enterprise fee should not have been deleted + visit admin_enterprise_fees_path + page.should have_selector "input[value='#{fee.name}']" + EnterpriseFee.find(fee.id).should_not be_nil + end end diff --git a/spec/features/admin/shipping_methods_spec.rb b/spec/features/admin/shipping_methods_spec.rb index 9f4f1f9490..fc36554af3 100644 --- a/spec/features/admin/shipping_methods_spec.rb +++ b/spec/features/admin/shipping_methods_spec.rb @@ -26,15 +26,4 @@ feature 'shipping methods' do page.should have_content "That shipping method cannot be deleted as it is referenced by an order: #{o.number}." Spree::ShippingMethod.find(@sm.id).should_not be_nil end - - scenario "deleting a shipping method referenced by a product distribution" do - p = create(:product) - d = create(:distributor_enterprise) - create(:product_distribution, product: p, distributor: d, shipping_method: @sm) - - visit_delete spree.admin_shipping_method_path(@sm) - - page.should have_content "That shipping method cannot be deleted as it is referenced by a product distribution: #{p.id} - #{p.name}." - Spree::ShippingMethod.find(@sm.id).should_not be_nil - end end