Skip to content

Commit

Permalink
Yield on PeopleManagerController#destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-illi committed Feb 23, 2024
1 parent a6f3578 commit 79cdbf6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/controllers/people_managers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ def create
end

def destroy
find_entry.destroy!
ActiveRecord::Base.transaction do
find_entry.tap do |entry|
entry.destroy!
yield entry if block_given?
end
end
redirect_to redirect_to_path
end

Expand Down
52 changes: 52 additions & 0 deletions spec/controllers/people_managers_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas

require 'spec_helper'

shared_examples 'people_managers#destroy' do
controller(described_class) do
def destroy
super { |entry| entry.call_on_yielded }
end
end

let(:entry) do
PeopleManager.create!(
manager: people(:bottom_leader),
managed: people(:bottom_member)
)
end

before { sign_in(people(:root)) }

def params
attr = described_class.assoc == :people_managers ? :managed_id : :manager_id
{
id: entry.id,
person_id: entry.send(attr)
}
end

context '#destroy' do
it 'yields' do
expect_any_instance_of(PeopleManager).to receive(:call_on_yielded)
delete :destroy, params: params

expect { entry.reload }.to raise_error(ActiveRecord::RecordNotFound)
end

it 'does not destroy entry if household#remove raises error' do
expect_any_instance_of(PeopleManager).to receive(:call_on_yielded).and_raise('baaad stuff')

expect do
delete :destroy, params: params
end.to raise_error('baaad stuff')

expect { entry.reload }.not_to raise_error
end
end
end
13 changes: 13 additions & 0 deletions spec/controllers/person/manageds_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas

require 'spec_helper'
require_relative '../people_managers_controller_spec'

describe Person::ManagedsController do
it_behaves_like 'people_managers#destroy'
end
13 changes: 13 additions & 0 deletions spec/controllers/person/managers_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas

require 'spec_helper'
require_relative '../people_managers_controller_spec'

describe Person::ManagersController do
it_behaves_like 'people_managers#destroy'
end

0 comments on commit 79cdbf6

Please sign in to comment.