Commit ec58639a authored by Zsolt Benke's avatar Zsolt Benke
Browse files

Added destroy method to ClientsController

parent fe1acf42
Pipeline #21564 passed with stage
in 34 minutes and 26 seconds
...@@ -3,7 +3,8 @@ class ClientsController < ApplicationController ...@@ -3,7 +3,8 @@ class ClientsController < ApplicationController
include NestedAttributes include NestedAttributes
include ContactAttributes include ContactAttributes
before_action :set_client, only: [:show, :edit, :update, :set_terminated] rescue_from Client::NotDestroyableError, with: :user_not_authorized
before_action :set_client, only: [:show, :edit, :update, :destroy, :set_terminated]
before_action :set_social_worker_collection, only: [:new, :create, :edit, :update] before_action :set_social_worker_collection, only: [:new, :create, :edit, :update]
before_action :set_assignments, only: [:show, :edit] before_action :set_assignments, only: [:show, :edit]
...@@ -64,6 +65,11 @@ class ClientsController < ApplicationController ...@@ -64,6 +65,11 @@ class ClientsController < ApplicationController
end end
end end
def destroy
@client.destroy!
redirect_to clients_path, make_notice
end
def set_terminated def set_terminated
if @client.update(acceptance: :resigned, resigned_by: current_user) if @client.update(acceptance: :resigned, resigned_by: current_user)
redirect_back(fallback_location: edit_client_path(@client), redirect_back(fallback_location: edit_client_path(@client),
......
...@@ -2,10 +2,49 @@ require 'test_helper' ...@@ -2,10 +2,49 @@ require 'test_helper'
class CliensControllerTest < ActionDispatch::IntegrationTest class CliensControllerTest < ActionDispatch::IntegrationTest
setup do setup do
@superadmin = create :user, :with_clients, @superadmin = create :user, :with_clients
:with_department, role: 'superadmin'
@social_worker = create :user, :with_clients,
:with_department, role: 'social_worker'
@department_manager = create :department_manager @department_manager = create :department_manager
@social_worker = create :social_worker
end
test 'superadmin, department_manager, social_worker can destroy inactive clients' do
[@superadmin, @department_manager, @social_worker].each do |user|
client = create :client
login_as user
assert_difference 'Client.count', -1 do
delete client_path client
end
assert_redirected_to clients_path
end
end
test 'no user can destroy clients with assignment associated' do
[@superadmin, @department_manager, @social_worker].each do |user|
login_as user
client = create :client
assignment = create :assignment, client: client
assert_no_difference 'Client.count' do
delete client_path client
end
assert_redirected_to root_path
assert_nil client.deleted_at
end
end
test 'no user can destroy clients with assignment associated even when its deleted' do
[@superadmin, @department_manager, @social_worker].each do |user|
login_as user
client = create :client
assignment = create :assignment, client: client
assignment.destroy
assert_no_difference 'Client.count' do
delete client_path client
end
assert_redirected_to root_path
assert_nil client.deleted_at
end
end end
end end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment