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

Add response with error message on failure destroy action #2920

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
1 change: 1 addition & 0 deletions backend/app/controllers/spree/admin/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def destroy
invoke_callbacks(:destroy, :fails)
respond_with(@object) do |format|
format.html { redirect_to location_after_destroy }
format.js { render status: :unprocessable_entity, plain: @object.errors.full_messages.join(', ') }
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions backend/spec/controllers/spree/admin/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def model_class
model do
acts_as_list
validates :name, presence: true
before_destroy :check_destroy_constraints

def check_destroy_constraints
return unless name == 'undestroyable'
errors.add :base, "You can't destroy undestroyable things!"
throw(:abort)
end
end
end

Expand Down Expand Up @@ -146,6 +153,21 @@ def model_class
it 'destroys the resource' do
expect { subject }.to change { Widget.count }.from(1).to(0)
end

context 'failure' do
let(:widget) { Widget.create!(name: 'undestroyable') }
let(:params) { { id: widget.id } }

context 'js format' do
subject { delete :destroy, params: params, format: 'js' }

it 'responds with error message' do
subject
expect(response).to be_unprocessable
expect(response.body).to eq assigns(:widget).errors.full_messages.join(', ')
kennyadsl marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end

describe '#update_positions' do
Expand Down