Skip to content

Commit

Permalink
fix: message don't appear on fail destroy on associations (#2016)
Browse files Browse the repository at this point in the history
* fix: message don't appear on fail destroy on associations

* add test
  • Loading branch information
Paul-Bob committed Nov 15, 2023
1 parent 2e90542 commit 869bce2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,10 @@ def destroy_success_action
end

def destroy_fail_action
flash[:error] = destroy_fail_message

respond_to do |format|
format.html { redirect_back fallback_location: params[:referrer] || resources_path(resource: @resource, turbo_frame: params[:turbo_frame], view_type: params[:view_type]), error: destroy_fail_message }
format.turbo_stream { render partial: "avo/partials/flash_alerts" }
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/avo/partials/_flash_alerts.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= turbo_stream.append "alerts" do %>
<%= render Avo::FlashAlertsComponent.new flashes: flash %>
<%= render Avo::FlashAlertsComponent.new flashes: flash.discard %>
<% end %>
36 changes: 36 additions & 0 deletions spec/system/avo/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,42 @@
sleep 0.8
expect(page).to have_text("Record destroyed").twice
end

it "shows the notification when delete fails" do
Comment.class_eval do
def destroy
raise "Record failed to destroy"
end
end

visit url

expect {
find("[data-resource-id='#{comments.first.id}'] [data-control='destroy']").click
sleep 0.2
page.driver.browser.switch_to.alert.accept
sleep 0.2
find("[data-resource-id='#{comments.third.id}'] [data-control='destroy']").click
sleep 0.2
page.driver.browser.switch_to.alert.accept
sleep 0.2
}.to change(Comment, :count).by(0)

expect(page).to have_current_path url

expect(page).to have_text comments.third.tiny_name.to_s
expect(page).to have_text comments.first.tiny_name.to_s
expect(page).to have_text comments.second.tiny_name.to_s

sleep 0.8
expect(page).to have_text("Record failed to destroy").twice

Comment.class_eval do
def destroy
super
end
end
end
end
end

Expand Down

0 comments on commit 869bce2

Please sign in to comment.