diff --git a/app/controllers/spree/admin/shipping_methods_controller_decorator.rb b/app/controllers/spree/admin/shipping_methods_controller_decorator.rb new file mode 100644 index 0000000000..6e8309da5e --- /dev/null +++ b/app/controllers/spree/admin/shipping_methods_controller_decorator.rb @@ -0,0 +1,15 @@ +module Spree + module Admin + ShippingMethodsController.class_eval do + before_filter :do_not_destroy_referenced_shipping_methods, :only => :destroy + + def do_not_destroy_referenced_shipping_methods + order = Order.where(:shipping_method_id => @object).first + if order + flash[:error] = "That shipping method cannot be deleted as it is referenced by an order: #{order.number}." + redirect_to collection_url + end + end + end + end +end diff --git a/spec/requests/admin/shipping_methods_spec.rb b/spec/requests/admin/shipping_methods_spec.rb new file mode 100644 index 0000000000..b0355516c8 --- /dev/null +++ b/spec/requests/admin/shipping_methods_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +feature 'shipping methods' do + include AuthenticationWorkflow + include WebHelper + + scenario "deleting a shipping method referenced by an order" do + login_to_admin_section + + sm = create(:shipping_method) + o = create(:order, 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 an order: #{o.number}." + Spree::ShippingMethod.find(sm.id).should_not be_nil + end + + scenario "deleting a shipping method referenced by a product distribution" + scenario "deleting a shipping method referenced by a line item" +end