From 2d06b2865fc3b97a06f0e7cb450c658f075168e0 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Wed, 16 Aug 2017 19:49:36 +0200 Subject: [PATCH] Handle when the block to delete doesn't exist closes #7542 --- app/controllers/blocks_controller.rb | 4 ++-- spec/controllers/blocks_controller_spec.rb | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/blocks_controller.rb b/app/controllers/blocks_controller.rb index 38da40d14..7ad4f2e1c 100644 --- a/app/controllers/blocks_controller.rb +++ b/app/controllers/blocks_controller.rb @@ -12,7 +12,7 @@ class BlocksController < ApplicationController end def destroy - notice = if current_user.blocks.find(params[:id]).delete + notice = if current_user.blocks.find_by(id: params[:id])&.delete {notice: t("blocks.destroy.success")} else {error: t("blocks.destroy.failure")} @@ -20,7 +20,7 @@ class BlocksController < ApplicationController respond_to do |format| format.json { head :no_content } - format.any { redirect_back notice.merge(fallback_location: privacy_settings_path) } + format.any { redirect_back fallback_location: privacy_settings_path, flash: notice } end end diff --git a/spec/controllers/blocks_controller_spec.rb b/spec/controllers/blocks_controller_spec.rb index 31da9a9f6..0a774da3a 100644 --- a/spec/controllers/blocks_controller_spec.rb +++ b/spec/controllers/blocks_controller_spec.rb @@ -33,7 +33,7 @@ describe BlocksController, :type => :controller do it "notifies the user" do delete :destroy, params: {id: @block.id} - expect(flash).not_to be_empty + expect(flash[:notice]).to eq(I18n.t("blocks.destroy.success")) end it "responds with 204 with json" do @@ -51,6 +51,11 @@ describe BlocksController, :type => :controller do delete :destroy, params: {id: @block.id}, format: :json }.to change { alice.blocks.count }.by(-1) end + + it "handles when the block to delete doesn't exist" do + delete :destroy, params: {id: -1} + expect(flash[:error]).to eq(I18n.t("blocks.destroy.failure")) + end end describe "#disconnect_if_contact" do