Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show errors when has_many restrict_with_error. #961

Merged
merged 1 commit into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("<br/>")
end
redirect_to action: :index
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/administrate/application/_flashes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This partial renders flash messages on every page.
<% if flash.any? %>
<div class="flashes">
<% flash.each do |key, value| -%>
<div class="flash flash-<%= key %>"><%= value %></div>
<div class="flash flash-<%= key %>"><%= value.html_safe %></div>
<% end -%>
</div>
<% end %>
2 changes: 1 addition & 1 deletion spec/example_app/app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions spec/features/orders_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions spec/models/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions spec/support/have_flash_matcher.rb
Original file line number Diff line number Diff line change
@@ -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