Skip to content

Commit

Permalink
Merge pull request #3798 from nebulab/elia/fix-admin-resource-control…
Browse files Browse the repository at this point in the history
…ler-not-found-errors

Ensure #resource_not_found mentions the right model
  • Loading branch information
kennyadsl authored Oct 15, 2020
2 parents 67bbedd + fc0bb21 commit 4efed6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backend/app/controllers/spree/admin/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseController

helper_method :new_object_url, :edit_object_url, :object_url, :collection_url
before_action :load_resource, except: :update_positions
rescue_from ActiveRecord::RecordNotFound do
resource_not_found
rescue_from ActiveRecord::RecordNotFound do |exception|
resource_not_found(flash_class: exception.model.constantize)
end
rescue_from ActiveRecord::RecordInvalid, with: :resource_invalid

Expand Down
17 changes: 17 additions & 0 deletions backend/spec/controllers/spree/admin/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,21 @@ def check_destroy_constraints
end
end
end

describe 'rescue_from ActveRecord::RecordNotFound' do
let(:widget) { Widget.create!(name: 'a widget') }

subject do
get :edit, params: { id: widget.to_param }
end

it 'shows an error message with a reference to the model that was not found and redirect to the collection' do
allow(controller).to receive(:edit) { Spree::Product.find(123) }

subject

expect(response).to redirect_to('/admin/widgets')
expect(flash[:error]).to eql('Product is not found')
end
end
end

0 comments on commit 4efed6b

Please sign in to comment.