Show a nice error message instead of 500 error when deleting a referenced order cycle

This commit is contained in:
Rohan Mitchell
2015-03-16 12:42:35 +11:00
parent 95c09315f5
commit a6f0d8f69a
2 changed files with 27 additions and 0 deletions

View File

@@ -7,6 +7,8 @@ module Admin
before_filter :load_data_for_index, :only => :index
before_filter :require_coordinator, only: :new
around_filter :protect_invalid_destroy, only: :destroy
def show
respond_to do |format|
@@ -106,5 +108,14 @@ module Admin
render :set_coordinator
end
end
def protect_invalid_destroy
begin
yield
rescue ActiveRecord::InvalidForeignKey
redirect_to main_app.admin_order_cycles_url
flash[:error] = "That order cycle has been selected by a customer and cannot be deleted. To prevent customers from accessing it, please close it instead."
end
end
end
end

View File

@@ -56,5 +56,21 @@ module Admin
end
end
end
describe "destroy" do
let!(:distributor) { create(:distributor_enterprise, owner: distributor_owner) }
describe "when an order cycle becomes non-deletable, and we attempt to delete it" do
let!(:oc) { create(:simple_order_cycle, coordinator: distributor) }
let!(:order) { create(:order, order_cycle: oc) }
before { spree_get :destroy, id: oc.id }
it "displays an error message" do
expect(response).to redirect_to admin_order_cycles_path
expect(flash[:error]).to eq "That order cycle has been selected by a customer and cannot be deleted. To prevent customers from accessing it, please close it instead."
end
end
end
end
end