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/sac 287 add family member #48

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion app/controllers/people_managers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ def new

def create
assign_attributes
if entry.save

success = ActiveRecord::Base.transaction do
if entry.save
yield entry if block_given?
true
else
false
end
end

if success
redirect_to redirect_to_path
else
render :new
Expand Down
2 changes: 1 addition & 1 deletion app/models/people_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ def create_paper_trail_versions_for_destroy_event
end

def assert_manager_is_not_managed
errors.add(:base, :manager_and_managed_the_same) if manager&.id == managed&.id
errors.add(:manager_id, :manager_and_managed_the_same) if manager&.id == managed&.id
end
end
1 change: 1 addition & 0 deletions app/views/person/manageds/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

= render layout: 'people_managers/section' do
= standard_form(entry, url: person_manageds_path(person), html: {class: 'p-2'}) do |f|
= f.error_messages
- if create_managed? && params[:create]
= render "new_person_fields", f: f
- else
Expand Down
5 changes: 5 additions & 0 deletions config/locales/models.youth.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ de:
attributes:
manager_id:
taken: ist bereits gesetzt.
manager_and_managed_the_same: kann sich nicht selber verwalten.
models:
people_manager:
one: Verwalter*in
other: Verwalter*innen

activemodel:
errors:
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/person/manageds_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
require_relative 'people_managers_shared_examples'

describe Person::ManagedsController do
it_behaves_like 'people_managers#create'
it_behaves_like 'people_managers#destroy'
end
3 changes: 2 additions & 1 deletion spec/controllers/person/managers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# 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
# https://github.com/hitobito/hitobito_youth

require 'spec_helper'
require_relative 'people_managers_shared_examples'

describe Person::ManagersController do
it_behaves_like 'people_managers#create'
it_behaves_like 'people_managers#destroy'
end
33 changes: 32 additions & 1 deletion spec/controllers/person/people_managers_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,41 @@
# 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
# https://github.com/hitobito/hitobito_youth

require 'spec_helper'

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

before { sign_in(people(:root)) }

context '#create' do
let(:params) do
attr = described_class.assoc == :people_managers ? :manager_id : :managed_id
{ person_id: people(:bottom_leader).id, people_manager: { attr => people(:bottom_member).id } }
end

it 'yields' do
expect_any_instance_of(PeopleManager).to receive(:call_on_yielded)

expect { post :create, params: params }.to change { PeopleManager.count }.by(1)
end

it 'does not create entry if yielded block raises error' do
expect_any_instance_of(PeopleManager).to receive(:call_on_yielded).and_raise(ActiveRecord::Rollback)

expect { post :create, params: params }.
to raise_error('baaad stuff').
and not_change { PeopleManager.count }
end
end
end

shared_examples 'people_managers#destroy' do
controller(described_class) do
def destroy
Expand Down
12 changes: 11 additions & 1 deletion spec/features/people_managers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def find_person(name, retries = 0)
end
end

it 'shows validation errors in form' do
it 'shows manager validation errors in form' do
within_turbo_frame do
person.people_managers.create!(manager: people(:bottom_member))
click_dropdown('Verwalter*in zuweisen')
Expand All @@ -77,6 +77,16 @@ def find_person(name, retries = 0)
end
end

it 'shows managed validation errors in form' do
within_turbo_frame do
person.people_manageds.create!(managed: people(:bottom_member))
click_dropdown('Kind zuweisen')
find_person 'Bottom Member'
click_on 'Speichern'
expect(page).to have_css('.alert-danger', text: 'Verwalter*in ist bereits gesetzt.')
end
end

context 'without write permission on other persons' do
let(:person) { current_user }
let(:current_user) { Fabricate(Group::TopGroup::LocalSecretary.sti_name, group: top_group).person }
Expand Down
Loading