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

fix: add model-unrelated errors to alerts #2813

Merged
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
3 changes: 3 additions & 0 deletions .erb-lint_rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ Layout/InitialIndentation:

Lint/UselessAssignment:
Enabled: false

Layout/FirstArgumentIndentation:
Enabled: false
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.0
3.3.1
4 changes: 2 additions & 2 deletions app/components/avo/views/resource_edit_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
multipart: true do |form| %>
<%= render Avo::ReferrerParamsComponent.new back_path: back_path %>
<%= content_tag :div, class: 'space-y-12' do %>
<%= content_tag :div, class: "space-y-12" do %>
<% @resource.get_items.each_with_index do |item, index| %>
<%= render Avo::Items::SwitcherComponent.new(
resource: @resource,
Expand All @@ -32,7 +32,7 @@
form: form,
parent_component: self,
actions: @actions
)%>
) %>
<% end %>

<% if Avo.configuration.buttons_on_form_footers %>
Expand Down
16 changes: 14 additions & 2 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,17 @@ def perform_action_and_record_errors(&block)

# Remove duplicated errors
if exception_message.present?
@errors = @errors.reject { |error| exception_message.include? error }.unshift exception_message
@errors = @errors.reject { |error| exception_message.include? error }
end

# Figure out if we have to output the exception_message
# Usually it means that it's not a validation error but something else
if exception_message.present?
exception_is_validation = @errors.select { |error| exception_message.include? error }.present?
end
Comment on lines +234 to +241
Copy link
Contributor

@Paul-Bob Paul-Bob Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this code

@errors = @errors.reject { |error| exception_message.include? error }

runs before this one

exception_is_validation = @errors.select { |error| exception_message.include? error }.present?

will the second one ever be true?


if exception_is_validation || (@errors.blank? && exception_message.present?)
@errors << exception_message
end

@errors.any? ? false : succeeded
Expand Down Expand Up @@ -477,9 +487,11 @@ def update_success_action
end

def update_fail_action
flash.now[:error] = update_fail_message

respond_to do |format|
flash.now[:error] = update_fail_message
format.html { render :edit, status: :unprocessable_entity }
format.turbo_stream { render "update_fail_action" }
end
end

Expand Down
10 changes: 9 additions & 1 deletion app/views/avo/base/create_fail_action.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<%= turbo_stream.replace(frame_id(@resource), template: "avo/base/new") %>
<%= turbo_stream.replace(frame_id(@resource), template: "avo/base/new") %>

<%= turbo_stream.append "alerts" do %>
<%= render Avo::FlashAlertsComponent.new flashes: flash %>
<% end %>

<% if @errors.any? %>
<%= turbo_stream.append("alerts") do %>
<% @errors.each do |message| %>
<%= render Avo::AlertComponent.new :error, message %>
<% end %>
<% end %>
<% end %>

<% if @record.errors.any? %>
<%= turbo_stream.append("alerts") do %>
<% @record.errors.full_messages.each do |message| %>
Expand Down
1 change: 0 additions & 1 deletion app/views/avo/base/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<%= render @component.new(resource: @resource, record: @record, view: @view, actions: @actions) %>

21 changes: 21 additions & 0 deletions app/views/avo/base/update_fail_action.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= turbo_stream.replace(frame_id(@resource), template: "avo/base/edit") %>

<%= turbo_stream.append "alerts" do %>
<%= render Avo::FlashAlertsComponent.new flashes: flash %>
<% end %>

<% if @errors.any? %>
<%= turbo_stream.append("alerts") do %>
<% @errors.each do |message| %>
<%= render Avo::AlertComponent.new :error, message %>
<% end %>
<% end %>
<% end %>

<% if @record.errors.any? %>
<%= turbo_stream.append("alerts") do %>
<% @record.errors.full_messages.each do |message| %>
<%= render Avo::AlertComponent.new :error, message %>
<% end %>
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/filters/course_city_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def react

# Get the first city
cities = cities_for_countries(selected_countries.keys)
first_city = cities.first.first
first_city = cities&.first&.first

# Return the first city selected as a Hash
[[first_city, true]].to_h
Expand Down
2 changes: 1 addition & 1 deletion spec/features/avo/exposed_methods_base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
save

expect(page).to have_text "Course not updated!"
expect(page).to have_text "Validation failed: Name can't be blank"
expect(page).to have_text "Name can't be blank"
end

let(:course_url) { "/admin/resources/courses/#{course.prefix_id}" }
Expand Down
Loading