Skip to content

Commit

Permalink
Merge pull request #3602 from softr8/resources_controller_rescue_inva…
Browse files Browse the repository at this point in the history
…lid_objects

Rescuing from ActiveRecord::RecordInvalid in ResourcesController
  • Loading branch information
aldesantis authored May 6, 2020
2 parents b539d47 + 687c86b commit 3a2cee4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions backend/app/controllers/spree/admin/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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, with: :resource_not_found
rescue_from ActiveRecord::RecordInvalid, with: :resource_invalid

respond_to :html

Expand Down Expand Up @@ -299,4 +300,19 @@ def render_after_create_error
def render_after_update_error
render action: 'edit'
end

def resource_invalid(exception)
invoke_callbacks(action, :fails)
respond_with(@object) do |format|
format.html do
flash.now[:error] = exception.message
if @object.new_record?
render_after_create_error
else
render_after_update_error
end
end
format.js { render layout: false }
end
end
end
11 changes: 11 additions & 0 deletions backend/spec/controllers/spree/admin/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ def check_destroy_constraints
expect(flash[:error]).to eq assigns(:widget).errors.full_messages.join(', ')
end
end

context 'resource invalid' do
before do
allow_any_instance_of(Widget).to receive(:update).and_raise(ActiveRecord::RecordInvalid)
end

it 'returns to edit page with error' do
put :update, params: params
expect(flash[:error]).to eq('Record invalid')
end
end
end

describe '#destroy' do
Expand Down

0 comments on commit 3a2cee4

Please sign in to comment.