Skip to content

Commit

Permalink
Add Switch Stammsektion Wizard (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
amaierhofer committed Jul 24, 2024
1 parent 51d26eb commit 9b926ad
Show file tree
Hide file tree
Showing 30 changed files with 793 additions and 96 deletions.
26 changes: 26 additions & 0 deletions app/abilities/memberships/constraints.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

# Copyright (c) 2012-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.

module Memberships
module Constraints
def for_self_if_active_member_or_backoffice
active_member? && (for_self? || backoffice?)
end

def backoffice?
user_context.user.backoffice?
end

def for_self?
subject.person == user_context.user
end

def active_member?
People::SacMembership.new(subject.person).active?
end
end
end
18 changes: 2 additions & 16 deletions app/abilities/memberships/join_zusatzsektion_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@

module Memberships
class JoinZusatzsektionAbility < AbilityDsl::Base
include Memberships::Constraints

on(Wizards::Memberships::JoinZusatzsektion) do
permission(:any).may(:create).for_self_if_active_member_or_backoffice
end

def for_self_if_active_member_or_backoffice
active_member? && (for_self? || backoffice?)
end

def backoffice?
user_context.user.backoffice?
end

def for_self?
subject.person == user_context.user
end

def active_member?
People::SacMembership.new(subject.person).active?
end
end
end
18 changes: 2 additions & 16 deletions app/abilities/memberships/leave_zusatzsektion_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@

module Memberships
class LeaveZusatzsektionAbility < AbilityDsl::Base
include Memberships::Constraints

on(Wizards::Memberships::LeaveZusatzsektion) do
permission(:any).may(:create).for_self_if_active_member_or_backoffice
end

def for_self_if_active_member_or_backoffice
active_member? && (for_self? || backoffice?)
end

def backoffice?
user_context.user.backoffice?
end

def for_self?
subject.person == user_context.user
end

def active_member?
People::SacMembership.new(subject.person).active?
end
end
end
16 changes: 16 additions & 0 deletions app/abilities/memberships/switch_stammsektion_ability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# Copyright (c) 2012-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.

module Memberships
class SwitchStammsektionAbility < AbilityDsl::Base
include Memberships::Constraints

on(Wizards::Memberships::SwitchStammsektion) do
permission(:any).may(:create).for_self_if_active_member_or_backoffice
end
end
end
18 changes: 2 additions & 16 deletions app/abilities/memberships/terminate_sac_membership_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@

module Memberships
class TerminateSacMembershipAbility < AbilityDsl::Base
include Memberships::Constraints

on(Wizards::Memberships::TerminateSacMembershipWizard) do
permission(:any).may(:create).for_self_if_active_member_or_backoffice
end

def for_self_if_active_member_or_backoffice
active_member? && (for_self? || backoffice?)
end

def backoffice?
user_context.user.backoffice?
end

def for_self?
subject.person == user_context.user
end

def active_member?
People::SacMembership.new(subject.person).active?
end
end
end
14 changes: 13 additions & 1 deletion app/components/sac_cas/steps_component/content_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def fields_for(&)
end + @form.fields_for(partial_name, model, &) + bottom_toolbar
end

def form_error_messages
@form.error_messages
end

def model
@form.object.step_at(index)
end
Expand All @@ -29,13 +33,21 @@ def attr?(key)
def bottom_toolbar
content_tag(:div, class: "btn-toolbar allign-with-form") do
buttons = [next_button]
buttons << back_link if index.positive?
buttons << if index.positive?
back_link
else
cancel_link
end
safe_join(buttons)
end
end

private

def cancel_link
link_to(t("global.button.cancel"), person_path(@form.object.person), class: "link cancel mt-2 pt-1")
end

# Adjust to comply with existing api
def past?
return super unless wizard?
Expand Down
52 changes: 52 additions & 0 deletions app/controllers/memberships/switch_stammsektions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

# Copyright (c) 2012-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.

module Memberships
class SwitchStammsektionsController < Wizards::BaseController
before_action :wizard, :person, :group, :authorize

helper_method :group, :person
alias_method :entry, :wizard

private

def authorize
authorize!(:create, wizard)
end

def wizard
@wizard ||= model_class.new(
person: person,
current_step: params[:step].to_i,
backoffice: current_user.backoffice?,
**model_params.to_unsafe_h
)
end

def model_class
Wizards::Memberships::SwitchStammsektion
end

def success_message
roles_count = wizard.switch_operation.affected_people.count
t(".success", count: roles_count, group_name: wizard.choose_sektion.group.to_s, switch_date: wizard.choose_date.switch_on_text)
end

# NOTE: format: :html is required otherwise it is redirect as turbo_stream
def redirect_target
person_path(person, format: :html)
end

def person
@person ||= Person.find(params[:person_id])
end

def group
@group ||= Group.find(params[:group_id])
end
end
end
47 changes: 18 additions & 29 deletions app/helpers/dropdown/people/memberships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ module Dropdown
class People::Memberships < Base
attr_reader :person, :template

WIZARDS = [
[Wizards::Memberships::JoinZusatzsektion, :group_person_join_zusatzsektion_path],
[Wizards::Memberships::SwitchStammsektion, :group_person_switch_stammsektion_path],
[Wizards::Memberships::TerminateSacMembershipWizard, :group_person_terminate_sac_membership_path]
].freeze

delegate :t, :current_ability, :current_user, :group_person_join_zusatzsektion_path,
:group_person_terminate_sac_membership_path,
to: :template
:group_person_terminate_sac_membership_path, :group_person_switch_stammsektion_path, to: :template

def initialize(template, person, group)
@template = template
Expand All @@ -28,37 +33,21 @@ def to_s
private

def init_items
add_join_zusatzsektion_item if join_zusatzsektion?
add_sac_membership_termination if terminate_sac_membership?
end

def add_join_zusatzsektion_item
link = group_person_join_zusatzsektion_path(group_id: @group.id, person_id: @person.id)
add_item(translate(:join_zusatzsektion_link), link, method: :get)
end

def join_zusatzsektion?
current_ability.can?(
:create,
Wizards::Memberships::JoinZusatzsektion.new(
person: @person,
backoffice: current_user.backoffice?
)
)
WIZARDS.each do |wizard_class, path|
add_wizard(wizard_class, path) if current_ability.can?(:create, build(wizard_class))
end
end

def add_sac_membership_termination
link = group_person_terminate_sac_membership_path(group_id: @group.id, person_id: @person.id)
add_item(translate(:sac_membership_termination_link), link, method: :get)
def add_wizard(wizard_class, path)
wizard_name = wizard_class.to_s.demodulize.to_s.underscore
target_url = send(path, group_id: @group.id, person_id: @person.id)
add_item(translate("#{wizard_name}_link"), target_url, method: :get)
end

def terminate_sac_membership?
current_ability.can?(
:create,
Wizards::Memberships::TerminateSacMembershipWizard.new(
person: @person,
backoffice: current_user.backoffice?
)
def build(wizard_class)
wizard_class.new(
person: @person,
backoffice: current_user.backoffice?
)
end

Expand Down
17 changes: 17 additions & 0 deletions app/helpers/sheet/memberships/switch_stammsektion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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.

module Sheet::Memberships
class SwitchStammsektion < Sheet::Base
self.parent_sheet = Sheet::Person

def initialize(*args)
super
@title = I18n.t(".title", scope: self.class.to_s.underscore) # rubocop:disable Rails/HelperInstanceVariable
end
end
end
22 changes: 22 additions & 0 deletions app/mailers/memberships/switch_stammsektion_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.

module Memberships
class SwitchStammsektionMailer < ApplicationMailer
CONFIRMATION = "memberships_switch_stammsektion_confirmation"

def confirmation(person, sektion, switch_on_text)
values = [
%W[person-name #{person}],
%W[group-name #{sektion}],
%W[switch-date #{switch_on_text}]
].to_h
custom_headers = {cc: Group::Geschaeftsstelle.first.email}
custom_content_mail([person], CONFIRMATION, values, custom_headers)
end
end
end
13 changes: 13 additions & 0 deletions app/models/memberships/switch_stammsektion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ def initialize(...)

validate :assert_join_date

def save
super.tap do |success|
update_primary_groups
end
end

private

def update_primary_groups
affected_people.each do |person|
person.reload # Unsure why this reload is necessary
person.update!(primary_group: Groups::Primary.new(person).identify)
end
end

def prepare_roles(person)
old_role = existing_membership(person)

Expand Down
Loading

0 comments on commit 9b926ad

Please sign in to comment.