Skip to content

Commit

Permalink
Redirect to My Collections path after deleting collection
Browse files Browse the repository at this point in the history
Fixes #2858.

Based on #3014 and psu-libraries/scholarsphere#401, thanks to @hortongn and @awead.
  • Loading branch information
mjgiarlo committed Jan 24, 2017
1 parent 9f4a554 commit 9070811
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/controllers/concerns/sufia/collections_controller_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions spec/controllers/collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit 9070811

Please sign in to comment.