diff --git a/app/admin/geographies.rb b/app/admin/geographies.rb index 24bdaadbb..bd1c14f43 100644 --- a/app/admin/geographies.rb +++ b/app/admin/geographies.rb @@ -88,7 +88,7 @@ def destroy {alert: 'Could not delete selected Geography'} end - redirect_to admin_geographies_path(scope: current_scope.name), results + redirect_to admin_geographies_path, results end end end diff --git a/app/admin/legislations.rb b/app/admin/legislations.rb index 9907fd8c1..a89792d21 100644 --- a/app/admin/legislations.rb +++ b/app/admin/legislations.rb @@ -149,7 +149,7 @@ def destroy {alert: 'Could not delete selected Legislations'} end - redirect_to admin_legislations_path(scope: current_scope.name), results + redirect_to admin_legislations_path, results end end end diff --git a/app/admin/litigations.rb b/app/admin/litigations.rb index da1a9b9b5..06129eaf3 100644 --- a/app/admin/litigations.rb +++ b/app/admin/litigations.rb @@ -108,5 +108,17 @@ def scoped_collection super.includes(:geography, :created_by, :updated_by) end + + def destroy + destroy_command = ::Command::Destroy::Litigation.new(resource.object) + + results = if destroy_command.call + {notice: 'Successfully deleted selected Litigation'} + else + {alert: 'Could not delete selected Litigation'} + end + + redirect_to admin_litigations_path, results + end end end diff --git a/app/commands/command/destroy/geography.rb b/app/commands/command/destroy/geography.rb index da1999b68..2ce18bab8 100644 --- a/app/commands/command/destroy/geography.rb +++ b/app/commands/command/destroy/geography.rb @@ -13,8 +13,6 @@ def call r.litigations = [] r.legislations = [] - - r.save! end end end diff --git a/app/commands/command/destroy/legislation.rb b/app/commands/command/destroy/legislation.rb index 2ac527c27..34b08b8b7 100644 --- a/app/commands/command/destroy/legislation.rb +++ b/app/commands/command/destroy/legislation.rb @@ -15,7 +15,6 @@ def call r.litigations = [] r.targets = [] - r.save! end end end diff --git a/app/commands/command/destroy/litigation.rb b/app/commands/command/destroy/litigation.rb new file mode 100644 index 000000000..f60d5e5ad --- /dev/null +++ b/app/commands/command/destroy/litigation.rb @@ -0,0 +1,24 @@ +module Command + module Destroy + class Litigation + def initialize(resource) + @resource = resource + end + + def call + ActiveRecord::Base.transaction do + @resource.tap do |r| + r.discard + + r.litigation_sides.discard_all + r.events.discard_all + r.documents.discard_all + + r.legislations = [] + r.external_legislations = [] + end + end + end + end + end +end diff --git a/app/models/litigation.rb b/app/models/litigation.rb index 6d9f208d9..d4a447ce6 100644 --- a/app/models/litigation.rb +++ b/app/models/litigation.rb @@ -16,12 +16,14 @@ # visibility_status :string default("draft") # created_by_id :bigint # updated_by_id :bigint +# discarded_at :datetime # class Litigation < ApplicationRecord include UserTrackable include Taggable include VisibilityStatus + include Discardable extend FriendlyId friendly_id :title, use: :slugged, routes: :default diff --git a/app/models/litigation_side.rb b/app/models/litigation_side.rb index 66e80154c..2d5fa1517 100644 --- a/app/models/litigation_side.rb +++ b/app/models/litigation_side.rb @@ -14,6 +14,8 @@ # class LitigationSide < ApplicationRecord + include Discardable + SIDE_TYPES = %w[a b c].freeze PARTY_TYPES = %w[ government diff --git a/db/migrate/20190917165449_add_discarded_at_to_litigations.rb b/db/migrate/20190917165449_add_discarded_at_to_litigations.rb new file mode 100644 index 000000000..ccce0e3ae --- /dev/null +++ b/db/migrate/20190917165449_add_discarded_at_to_litigations.rb @@ -0,0 +1,6 @@ +class AddDiscardedAtToLitigations < ActiveRecord::Migration[5.2] + def change + add_column :litigations, :discarded_at, :datetime + add_index :litigations, :discarded_at + end +end diff --git a/db/migrate/20190917181907_add_discarded_at_to_litigation_sides.rb b/db/migrate/20190917181907_add_discarded_at_to_litigation_sides.rb new file mode 100644 index 000000000..aac4a4830 --- /dev/null +++ b/db/migrate/20190917181907_add_discarded_at_to_litigation_sides.rb @@ -0,0 +1,6 @@ +class AddDiscardedAtToLitigationSides < ActiveRecord::Migration[5.2] + def change + add_column :litigation_sides, :discarded_at, :datetime + add_index :litigation_sides, :discarded_at + end +end diff --git a/db/schema.rb b/db/schema.rb index 2689a54dd..1b4c823ee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_09_16_190258) do +ActiveRecord::Schema.define(version: 2019_09_17_181907) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -227,7 +227,9 @@ t.datetime "updated_at", null: false t.string "connected_entity_type" t.bigint "connected_entity_id" + t.datetime "discarded_at" t.index ["connected_entity_type", "connected_entity_id"], name: "index_litigation_sides_connected_entity" + t.index ["discarded_at"], name: "index_litigation_sides_on_discarded_at" t.index ["litigation_id"], name: "index_litigation_sides_on_litigation_id" end @@ -245,7 +247,9 @@ t.string "visibility_status", default: "draft" t.bigint "created_by_id" t.bigint "updated_by_id" + t.datetime "discarded_at" t.index ["created_by_id"], name: "index_litigations_on_created_by_id" + t.index ["discarded_at"], name: "index_litigations_on_discarded_at" t.index ["document_type"], name: "index_litigations_on_document_type" t.index ["geography_id"], name: "index_litigations_on_geography_id" t.index ["jurisdiction_id"], name: "index_litigations_on_jurisdiction_id" diff --git a/spec/controllers/admin/geographies_controller_spec.rb b/spec/controllers/admin/geographies_controller_spec.rb index 4faf8af13..23a25f318 100644 --- a/spec/controllers/admin/geographies_controller_spec.rb +++ b/spec/controllers/admin/geographies_controller_spec.rb @@ -145,7 +145,7 @@ end it 'redirects to index & renders alert message' do - expect(subject).to redirect_to(admin_geographies_path(scope: 'All')) + expect(subject).to redirect_to(admin_geographies_path) expect(flash[:alert]).to match('Could not delete selected Geography') end end diff --git a/spec/controllers/admin/legislations_controller_spec.rb b/spec/controllers/admin/legislations_controller_spec.rb index e4b0997c9..52b42adfe 100644 --- a/spec/controllers/admin/legislations_controller_spec.rb +++ b/spec/controllers/admin/legislations_controller_spec.rb @@ -125,8 +125,9 @@ end describe 'DELETE destroy' do + let!(:legislation_to_delete) { create(:legislation, discarded_at: nil) } + context 'with valid params' do - let!(:legislation_to_delete) { create(:legislation, discarded_at: nil) } let!(:document) { create(:document, documentable: legislation_to_delete) } let!(:event) { create(:legislation_event, eventable: legislation_to_delete) } @@ -159,6 +160,18 @@ expect(flash[:alert]).to match('Could not delete selected Legislations') end end + + context 'when geography does not exist' do + subject { delete :destroy, params: {id: legislation_to_delete.id} } + + before do + legislation_to_delete.geography.legislations = [] + end + + it 'soft-delete even if geography is nil' do + expect { subject }.to change { Legislation.count }.by(-1) + end + end end describe 'Batch Actions' do diff --git a/spec/controllers/admin/litigations_controller_spec.rb b/spec/controllers/admin/litigations_controller_spec.rb index 7a51221ee..a96ec11ee 100644 --- a/spec/controllers/admin/litigations_controller_spec.rb +++ b/spec/controllers/admin/litigations_controller_spec.rb @@ -130,6 +130,86 @@ end end + describe 'DELETE destroy' do + let!(:litigation) { create(:litigation, discarded_at: nil) } + subject { delete :destroy, params: {id: litigation.id} } + + context 'with valid params' do + let!(:litigation_side) { create(:litigation_side, litigation: litigation) } + let!(:event) { create(:litigation_event, eventable: litigation) } + let!(:document) { create(:document, documentable: litigation) } + + let!(:legislation) { create(:legislation, litigations: [litigation]) } + let!(:external_legislation) do + create(:external_legislation, litigations: [litigation]) + end + + before do + expect { subject }.to change { Litigation.count }.by(-1) + end + + it 'discards litigation object' do + expect(Litigation.find_by_id(litigation.id)).to be_nil + end + + it 'set discarded_at date to litigation object' do + expect(litigation.reload.discarded_at).to_not be_nil + end + + it 'shows discarded litigations in all_discarded scope' do + expect(Litigation.all_discarded.find(litigation.id)).to_not be_nil + end + + it 'discard all litigation sides' do + expect(LitigationSide.find_by_id(litigation_side.id)).to be_nil + end + + it 'discard all events' do + expect(Event.find_by_id(event.id)).to be_nil + end + + it 'discard all documents' do + expect(Document.find_by_id(document.id)).to be_nil + end + + it 'removes discarded litigation from legislation' do + expect(legislation.reload.litigations).to be_empty + end + + it 'removes discarded litigation from external_legislations' do + expect(external_legislation.reload.litigations).to be_empty + end + + it 'shows proper notice' do + expect(flash[:notice]).to match('Successfully deleted selected Litigation') + end + end + + context 'with invalid params' do + let(:command) { double } + + before do + expect(::Command::Destroy::Litigation).to receive(:new).and_return(command) + expect(command).to receive(:call).and_return(nil) + end + + it 'redirects to index & renders alert message' do + expect(subject).to redirect_to(admin_litigations_path) + expect(flash[:alert]).to match('Could not delete selected Litigation') + end + end + + context 'when geography does not exist' do + before do + litigation.geography.litigations = [] + end + + it 'soft-delete even if geography is nil' do + expect { subject }.to change { Litigation.count }.by(-1) + end + end + end + def last_litigation_created Litigation.order(:created_at).last end diff --git a/spec/factories/litigations.rb b/spec/factories/litigations.rb index 52aecd37a..be4e12b61 100644 --- a/spec/factories/litigations.rb +++ b/spec/factories/litigations.rb @@ -16,6 +16,7 @@ # visibility_status :string default("draft") # created_by_id :bigint # updated_by_id :bigint +# discarded_at :datetime # FactoryBot.define do diff --git a/spec/models/litigation_spec.rb b/spec/models/litigation_spec.rb index 9e0cc8450..517f6769a 100644 --- a/spec/models/litigation_spec.rb +++ b/spec/models/litigation_spec.rb @@ -16,6 +16,7 @@ # visibility_status :string default("draft") # created_by_id :bigint # updated_by_id :bigint +# discarded_at :datetime # require 'rails_helper'