-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
598f914
Added discarded_at to migration file for target and target_scope
agwozdowski bac9f40
TargetScope views and unit tests
agwozdowski 8b50ab6
Added DiscardableController. Moved DiscardableModel to concerns
agwozdowski b76ae8a
Added InvalidDestroyCommandError when destroy command not exist
agwozdowski c34a302
Revert database.yml file
agwozdowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
db/migrate/20190919193834_add_discarded_at_to_target_scope.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we catch possible
NameError
s here and re-throw ex with more context?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kowal done