Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/target soft delete #65

Merged
merged 5 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions app/admin/geographies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,10 @@
form partial: 'form'

controller do
include DiscardableController

def apply_filtering(chain)
super(chain).distinct
end

def destroy
destroy_command = ::Command::Destroy::Geography.new(resource.object)

message = if destroy_command.call
{notice: 'Successfully deleted selected Geography'}
else
{alert: 'Could not delete selected Geography'}
end

redirect_to admin_geographies_path, message
end
end
end
14 changes: 2 additions & 12 deletions app/admin/legislations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,10 @@
end

controller do
include DiscardableController

def scoped_collection
super.includes(:geography, :frameworks, :document_types, :created_by, :updated_by)
end

def destroy
destroy_command = ::Command::Destroy::Legislation.new(resource.object)

message = if destroy_command.call
{notice: 'Successfully deleted selected Legislation'}
else
{alert: 'Could not delete selected Legislations'}
end

redirect_to admin_legislations_path, message
end
end
end
14 changes: 2 additions & 12 deletions app/admin/litigations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,10 @@
form partial: 'form'

controller do
include DiscardableController

def scoped_collection
super.includes(:geography, :created_by, :updated_by)
end

def destroy
destroy_command = ::Command::Destroy::Litigation.new(resource.object)

message = if destroy_command.call
{notice: 'Successfully deleted selected Litigation'}
else
{alert: 'Could not delete selected Litigation'}
end

redirect_to admin_litigations_path, message
end
end
end
29 changes: 29 additions & 0 deletions app/admin/target_scopes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
ActiveAdmin.register TargetScope do
menu parent: 'Laws', priority: 4
config.batch_actions = false

decorate_with TargetScopeDecorator

permit_params :name

filter :name_contains, label: 'Name'

index do
column :name, :name_link

actions
end

show do
attributes_table do
row :name
end
end

form html: {'data-controller' => 'check-modified'} do |f|
f.semantic_errors(*f.object.errors.keys)

f.inputs do
f.input :name
end

f.actions
end

controller do
include DiscardableController
end
end
4 changes: 4 additions & 0 deletions app/admin/targets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@
column(:target_scope) { |t| t.target_scope.name }
column :visibility_status
end

controller do
include DiscardableController
end
end
19 changes: 19 additions & 0 deletions app/commands/command/destroy/target.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Command
module Destroy
class Target
def initialize(resource)
@resource = resource
end

def call
ActiveRecord::Base.transaction do
@resource.tap do |r|
r.discard

r.legislations = []
end
end
end
end
end
end
19 changes: 19 additions & 0 deletions app/commands/command/destroy/target_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Command
module Destroy
class TargetScope
def initialize(resource)
@resource = resource
end

def call
ActiveRecord::Base.transaction do
@resource.tap do |r|
r.discard

r.targets = []
end
end
end
end
end
end
16 changes: 16 additions & 0 deletions app/controllers/concerns/discardable_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module DiscardableController
# Override default Active Admin destroy method using Discardable Model.
# It sets discarded_at for entity and for its dependent resources.
# Redirect to resource list with proper message
def destroy
entity_name = ActiveSupport::Inflector.singularize(controller_name).camelize
entity_destroy_class = "::Command::Destroy::#{entity_name}".constantize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we catch possible NameErrors here and re-throw ex with more context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kowal done

destroy_command = entity_destroy_class.new(resource.object)

success_message = {notice: "Successfully deleted selected #{entity_name}"}
alert_message = {alert: "Could not delete selected #{entity_name}"}
message = destroy_command.call ? success_message : alert_message

redirect_to send("admin_#{controller_name}_path"), message
end
end
7 changes: 7 additions & 0 deletions app/decorators/target_scope_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class TargetScopeDecorator < Draper::Decorator
delegate_all

def name_link
h.link_to model.name, h.admin_target_scope_path(model)
end
end
2 changes: 1 addition & 1 deletion app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#

class Company < ApplicationRecord
include Discardable
include DiscardableModel
include VisibilityStatus
extend FriendlyId
friendly_id :name, use: :slugged, routes: :default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
# More details: https://github.com/jhawthorn/discard#default-scope
#
module Discardable
module DiscardableModel
extend ActiveSupport::Concern

included do
Expand Down
2 changes: 1 addition & 1 deletion app/models/cp/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
module CP
class Assessment < ApplicationRecord
include HasEmissions
include Discardable
include DiscardableModel

belongs_to :company

Expand Down
2 changes: 1 addition & 1 deletion app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#

class Document < ApplicationRecord
include Discardable
include DiscardableModel

self.inheritance_column = nil

Expand Down
2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#

class Event < ApplicationRecord
include Discardable
include DiscardableModel

belongs_to :eventable, polymorphic: true

Expand Down
2 changes: 1 addition & 1 deletion app/models/geography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Geography < ApplicationRecord
include UserTrackable
include Taggable
include VisibilityStatus
include Discardable
include DiscardableModel
extend FriendlyId

friendly_id :name, use: :slugged, routes: :default
Expand Down
2 changes: 1 addition & 1 deletion app/models/legislation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Legislation < ApplicationRecord
include UserTrackable
include Taggable
include VisibilityStatus
include Discardable
include DiscardableModel
extend FriendlyId

friendly_id :title, use: :slugged, routes: :default
Expand Down
2 changes: 1 addition & 1 deletion app/models/litigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Litigation < ApplicationRecord
include UserTrackable
include Taggable
include VisibilityStatus
include Discardable
include DiscardableModel
extend FriendlyId

friendly_id :title, use: :slugged, routes: :default
Expand Down
2 changes: 1 addition & 1 deletion app/models/litigation_side.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#

class LitigationSide < ApplicationRecord
include Discardable
include DiscardableModel

SIDE_TYPES = %w[a b c].freeze
PARTY_TYPES = %w[
Expand Down
2 changes: 1 addition & 1 deletion app/models/mq/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

module MQ
class Assessment < ApplicationRecord
include Discardable
include DiscardableModel
LEVELS = %w[0 1 2 3 4 4STAR].freeze

belongs_to :company, inverse_of: :mq_assessments
Expand Down
1 change: 1 addition & 0 deletions app/models/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class Target < ApplicationRecord
include UserTrackable
include VisibilityStatus
include DiscardableModel

TYPES = %w[
base_year_target
Expand Down
2 changes: 2 additions & 0 deletions app/models/target_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#

class TargetScope < ApplicationRecord
include DiscardableModel

has_many :targets

validates_presence_of :name
Expand Down
4 changes: 2 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ development:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
host: localhost

# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
Expand All @@ -59,7 +59,7 @@ development:
test:
<<: *default
database: laws_and_pathways_test

host: localhost
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20190919193714_add_discarded_at_to_targets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDiscardedAtToTargets < ActiveRecord::Migration[5.2]
def change
add_column :targets, :discarded_at, :datetime
add_index :targets, :discarded_at
end
end
6 changes: 6 additions & 0 deletions db/migrate/20190919193834_add_discarded_at_to_target_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDiscardedAtToTargetScope < ActiveRecord::Migration[5.2]
def change
add_column :target_scopes, :discarded_at, :datetime
add_index :target_scopes, :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_19_175646) do
ActiveRecord::Schema.define(version: 2019_09_19_193834) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -307,6 +307,8 @@
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "discarded_at"
t.index ["discarded_at"], name: "index_target_scopes_on_discarded_at"
end

create_table "targets", force: :cascade do |t|
Expand All @@ -324,7 +326,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_targets_on_created_by_id"
t.index ["discarded_at"], name: "index_targets_on_discarded_at"
t.index ["geography_id"], name: "index_targets_on_geography_id"
t.index ["sector_id"], name: "index_targets_on_sector_id"
t.index ["target_scope_id"], name: "index_targets_on_target_scope_id"
Expand Down
42 changes: 42 additions & 0 deletions spec/controllers/admin/target_scopes_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'rails_helper'

RSpec.describe Admin::TargetScopesController, type: :controller do
let(:admin) { create(:admin_user) }
before { sign_in admin }

describe 'DELETE destroy' do
let!(:target_scope) { create(:target_scope, discarded_at: nil) }

context 'with valid params' do
subject { delete :destroy, params: {id: target_scope.id} }

before do
expect { subject }.to change { TargetScope.count }.by(-1)
end

it 'set discarded_at date to target scope object' do
expect(target_scope.reload.discarded_at).to_not be_nil
end

it 'shows proper notice' do
expect(flash[:notice]).to match('Successfully deleted selected TargetScope')
end
end

context 'with invalid params' do
let(:command) { double }

subject { delete :destroy, params: {id: target_scope.id} }

before do
expect(::Command::Destroy::TargetScope).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_target_scopes_path)
expect(flash[:alert]).to match('Could not delete selected TargetScope')
end
end
end
end
Loading