From 960313f7a14f02d2cde974f0396e76412bf1630e 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 | 7 +++++-- app/views/administrate/application/_flashes.html.erb | 2 +- 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 +++-- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/controllers/administrate/application_controller.rb b/app/controllers/administrate/application_controller.rb index 1a73c6f7b9..42029e5637 100644 --- a/app/controllers/administrate/application_controller.rb +++ b/app/controllers/administrate/application_controller.rb @@ -70,8 +70,11 @@ 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("
") + end redirect_to action: :index end diff --git a/app/views/administrate/application/_flashes.html.erb b/app/views/administrate/application/_flashes.html.erb index 07d4265222..61de271183 100644 --- a/app/views/administrate/application/_flashes.html.erb +++ b/app/views/administrate/application/_flashes.html.erb @@ -14,7 +14,7 @@ This partial renders flash messages on every page. <% if flash.any? %>
<% flash.each do |key, value| -%> -
<%= value %>
+
<%= value.html_safe %>
<% end -%>
<% end %> diff --git a/spec/example_app/app/models/order.rb b/spec/example_app/app/models/order.rb index 5cf72fef87..cc566832fd 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 has_many :log_entries, as: :logeable validates :address_line_one, 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..35de01b97a 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