Skip to content

Commit 444a436

Browse files
committed
Yield on PeopleManagerController#create
1 parent cc9c982 commit 444a436

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

app/controllers/people_managers_controller.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ def new
2121

2222
def create
2323
assign_attributes
24-
if entry.save
25-
redirect_to redirect_to_path
26-
else
27-
render :new
24+
ActiveRecord::Base.transaction do
25+
if entry.save
26+
yield entry if block_given?
27+
redirect_to redirect_to_path
28+
else
29+
render :new
30+
end
2831
end
2932
end
3033

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
4+
# hitobito_sac_cas and licensed under the Affero General Public License version 3
5+
# or later. See the COPYING file at the top-level directory or at
6+
# https://github.com/hitobito/hitobito
7+
8+
require 'spec_helper'
9+
10+
shared_examples 'people_managers#create' do
11+
controller(described_class) do
12+
def create
13+
super { |entry| entry.call_on_yielded }
14+
end
15+
end
16+
17+
before { sign_in(people(:root)) }
18+
19+
context '#create' do
20+
let(:params) do
21+
attr = described_class.assoc == :people_managers ? :manager_id : :managed_id
22+
{ person_id: people(:bottom_leader).id, people_manager: { attr => people(:bottom_member).id } }
23+
end
24+
25+
it 'yields' do
26+
expect_any_instance_of(PeopleManager).to receive(:call_on_yielded)
27+
28+
expect { post :create, params: params }.to change { PeopleManager.count }.by(1)
29+
end
30+
31+
it 'does not create entry if yielded block raises error' do
32+
expect_any_instance_of(PeopleManager).to receive(:call_on_yielded).and_raise('baaad stuff')
33+
34+
expect { post :create, params: params }.
35+
to raise_error('baaad stuff').
36+
and not_change { PeopleManager.count }
37+
end
38+
end
39+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
4+
# hitobito_sac_cas and licensed under the Affero General Public License version 3
5+
# or later. See the COPYING file at the top-level directory or at
6+
# https://github.com/hitobito/hitobito_sac_cas
7+
8+
require 'spec_helper'
9+
require_relative '../people_managers_controller_spec'
10+
11+
describe Person::ManagedsController do
12+
it_behaves_like 'people_managers#create'
13+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
4+
# hitobito_sac_cas and licensed under the Affero General Public License version 3
5+
# or later. See the COPYING file at the top-level directory or at
6+
# https://github.com/hitobito/hitobito_sac_cas
7+
8+
require 'spec_helper'
9+
require_relative '../people_managers_controller_spec'
10+
11+
describe Person::ManagersController do
12+
it_behaves_like 'people_managers#create'
13+
end

0 commit comments

Comments
 (0)