Skip to content

Commit

Permalink
Merge pull request #62 from Vizzuality/feature/litigation-soft-delete
Browse files Browse the repository at this point in the history
Feature/litigation soft delete
  • Loading branch information
tsubik committed Sep 19, 2019
2 parents 8149788 + f0c7517 commit ef4a0eb
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/admin/geographies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/admin/legislations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions app/admin/litigations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions app/commands/command/destroy/geography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ def call

r.litigations = []
r.legislations = []

r.save!
end
end
end
Expand Down
1 change: 0 additions & 1 deletion app/commands/command/destroy/legislation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def call

r.litigations = []
r.targets = []
r.save!
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions app/commands/command/destroy/litigation.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/models/litigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/litigation_side.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#

class LitigationSide < ApplicationRecord
include Discardable

SIDE_TYPES = %w[a b c].freeze
PARTY_TYPES = %w[
government
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20190917165449_add_discarded_at_to_litigations.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/geographies_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion spec/controllers/admin/legislations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down Expand Up @@ -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
Expand Down
80 changes: 80 additions & 0 deletions spec/controllers/admin/litigations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/factories/litigations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# visibility_status :string default("draft")
# created_by_id :bigint
# updated_by_id :bigint
# discarded_at :datetime
#

FactoryBot.define do
Expand Down
1 change: 1 addition & 0 deletions spec/models/litigation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# visibility_status :string default("draft")
# created_by_id :bigint
# updated_by_id :bigint
# discarded_at :datetime
#

require 'rails_helper'
Expand Down

0 comments on commit ef4a0eb

Please sign in to comment.