From 133f77a24a39f8a2fd84645d51b867e9d45784c2 Mon Sep 17 00:00:00 2001 From: Victor Antoniazzi Date: Thu, 10 Aug 2017 12:16:27 -0300 Subject: [PATCH] Show errors when has_many restrict_with_error. * Return full_messages with new line separator --- .../administrate/application_controller.rb | 8 ++++++-- spec/example_app/app/models/order.rb | 2 +- spec/features/orders_index_spec.rb | 11 +++++++++++ spec/models/order_spec.rb | 11 +++++++++++ spec/support/have_flash_matcher.rb | 5 +++-- 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/controllers/administrate/application_controller.rb b/app/controllers/administrate/application_controller.rb index fba98788b1..fdd94b1d3c 100644 --- a/app/controllers/administrate/application_controller.rb +++ b/app/controllers/administrate/application_controller.rb @@ -67,8 +67,12 @@ def update end def destroy - requested_resource.destroy - flash[:notice] = translate_with_resource("destroy.success") + if requested_resource.destroy + flash[:notice] = translate_with_resource("destroy.success") + else + flash[:error] = requested_resource.errors. + full_messages.join("
").html_safe + end redirect_to action: :index end diff --git a/spec/example_app/app/models/order.rb b/spec/example_app/app/models/order.rb index 5833f20c36..ded4bba9d2 100644 --- a/spec/example_app/app/models/order.rb +++ b/spec/example_app/app/models/order.rb @@ -3,7 +3,7 @@ class Order < ActiveRecord::Base validates :customer, presence: true has_many :line_items, dependent: :destroy - has_many :payments, dependent: :destroy + has_many :payments, dependent: :restrict_with_error validates :address_line_one, presence: true validates :address_line_two, presence: true diff --git a/spec/features/orders_index_spec.rb b/spec/features/orders_index_spec.rb index b300330d42..223dcb3029 100644 --- a/spec/features/orders_index_spec.rb +++ b/spec/features/orders_index_spec.rb @@ -55,4 +55,15 @@ t("administrate.controller.destroy.success", resource: "Order") ) end + + scenario "cannot delete because associated payment" do + create(:payment, order: create(:order)) + + visit admin_orders_path + click_on t("administrate.actions.destroy") + + expect(page).to have_flash( + "Cannot delete record because dependent payments exist", type: :error + ) + end end diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 62ada4434c..5baed7ba8a 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -24,6 +24,17 @@ expect(LineItem.all).to be_empty end + it "raise error when try delete associated payment" do + order = create(:order) + create(:payment, order: order) + + order.destroy + + expect(order.errors[:base]).to eq( + ["Cannot delete record because dependent payments exist"] + ) + end + describe "#total_price" do it "returns 0 when there are no line items" do order = build(:order) diff --git a/spec/support/have_flash_matcher.rb b/spec/support/have_flash_matcher.rb index 8717a59be2..2e10dd723a 100644 --- a/spec/support/have_flash_matcher.rb +++ b/spec/support/have_flash_matcher.rb @@ -1,5 +1,6 @@ module Features - def have_flash(text) - have_css(".flash-notice", text: text) + def have_flash(text, options = {}) + options.reverse_merge!(type: :notice) + have_css(".flash-#{options[:type]}", text: text) end end