From 907081160f9b7cf50a94f0a87aae0f09fb4e3a62 Mon Sep 17 00:00:00 2001 From: "Michael J. Giarlo" Date: Mon, 23 Jan 2017 15:52:10 -0800 Subject: [PATCH] Redirect to My Collections path after deleting collection Fixes #2858. Based on #3014 and psu-stewardship/scholarsphere#401, thanks to @hortongn and @awead. --- .../sufia/collections_controller_behavior.rb | 14 ++++++++ .../collections_controller_spec.rb | 35 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/app/controllers/concerns/sufia/collections_controller_behavior.rb b/app/controllers/concerns/sufia/collections_controller_behavior.rb index f70587ae37..34881d0bb4 100644 --- a/app/controllers/concerns/sufia/collections_controller_behavior.rb +++ b/app/controllers/concerns/sufia/collections_controller_behavior.rb @@ -14,6 +14,20 @@ module CollectionsControllerBehavior protected + def after_destroy(id) + respond_to do |wants| + wants.html { redirect_to sufia.dashboard_collections_path, notice: "Collection #{id} was successfully deleted" } + wants.json { render_json_response(response_type: :success, message: "Collection #{id} was successfully deleted") } + end + end + + def after_destroy_error(id) + respond_to do |wants| + wants.html { redirect_to sufia.dashboard_collections_path, notice: "Collection #{id} could not be deleted" } + wants.json { render_json_response(response_type: :unprocessable_entity, message: "Collection #{id} could not be deleted") } + end + end + def add_breadcrumb_for_controller add_breadcrumb I18n.t('sufia.dashboard.my.collections'), sufia.dashboard_collections_path end diff --git a/spec/controllers/collections_controller_spec.rb b/spec/controllers/collections_controller_spec.rb index 6b1a8e2936..b82c7baa0f 100644 --- a/spec/controllers/collections_controller_spec.rb +++ b/spec/controllers/collections_controller_spec.rb @@ -211,6 +211,41 @@ end end + describe "#delete" do + before { sign_in user } + context "when it succeeds" do + it "redirects to My Collections" do + delete :destroy, params: { id: collection } + expect(response).to redirect_to(Sufia::Engine.routes.url_helpers.dashboard_collections_path) + expect(flash[:notice]).to eq "Collection #{collection.id} was successfully deleted" + end + + it "returns json" do + delete :destroy, params: { format: :json, id: collection } + json = JSON.parse(response.body) + json_description = json['description'] + expect(json_description).to eq "Collection #{collection.id} was successfully deleted" + end + end + context "when an error occurs" do + before do + allow_any_instance_of(Collection).to receive(:destroy).and_return(nil) + end + it "redirects to My Collections" do + delete :destroy, params: { id: collection } + expect(response).to redirect_to(Sufia::Engine.routes.url_helpers.dashboard_collections_path) + expect(flash[:notice]).to eq "Collection #{collection.id} could not be deleted" + end + + it "returns json" do + delete :destroy, params: { format: :json, id: collection } + json = JSON.parse(response.body) + json_description = json['description'] + expect(json_description).to eq "Collection #{collection.id} could not be deleted" + end + end + end + describe "#edit" do before { sign_in user }