Skip to content

Commit

Permalink
Implement choose_sektion_form step, (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
amaierhofer committed Jun 19, 2024
1 parent 2504ca0 commit a3ce736
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 3 deletions.
10 changes: 9 additions & 1 deletion app/components/sac_cas/steps_component/content_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@
module SacCas::StepsComponent::ContentComponent
extend ActiveSupport::Concern

def fields_for(&block)
@form.fields_for(@partial.split('/').last, model, &block)
end

def model
@form.object.step_at(index)
end

def attr?(key)
return false if key == :email && @partial =~ /main_person/

super(key)
super
end
end
14 changes: 14 additions & 0 deletions app/controllers/wizards_previews_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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.

class WizardsPreviewsController < ApplicationController
include ViewComponent::PreviewActions
skip_authorization_check
skip_forgery_protection

def current_user; end
end
36 changes: 36 additions & 0 deletions app/models/wizards/steps/choose_sektion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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 Wizards
module Steps
class ChooseSektion < Step
attribute :group_id, :integer
validates :group_id, presence: true
validate :assert_group_type, if: :group

GROUP_TYPES = [Group::Sektion.sti_name, Group::Ortsgruppe.sti_name].freeze

def groups
Group
.where(type: GROUP_TYPES)
.select(:id, :name)
end

def group
@group ||= Group.find(group_id) if group_id.present?
end

private

def assert_group_type
if GROUP_TYPES.exclude?(group.type)
errors.add(:group_id, :invalid)
end
end
end
end
end
7 changes: 7 additions & 0 deletions app/views/wizards/steps/_choose_sektion.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-# 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.
= c.fields_for do |form|
= form.labeled_collection_select :group_id, c.model.groups, :id, :name, {prompt: true}, data: { action: 'change->autosubmit#save' }
6 changes: 5 additions & 1 deletion config/locales/wagon.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ de:
self_registration/sektion/supplements:
adult_consent: Einverständniserklärung der Erziehungsberechtigten

wizards/steps/choose_sektion:
group_id: Sektion wählen

errors:
messages:
must_be_older_than_18: Person muss 18 Jahre oder älter sein.
Expand Down Expand Up @@ -74,7 +77,8 @@ de:
attributes:
person:
must_be_sac_member: muss Sac Mitglied sein
must_not_be_join_section_member: ist bereits Mitglied der Sektion oder
must_not_be_join_section_member:
ist bereits Mitglied der Sektion oder
hat ein offenes Beitrittsgesuch
must_be_family_main_person: muss Hauptperson der Familie sein

Expand Down
7 changes: 6 additions & 1 deletion lib/hitobito_sac_cas/wagon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Wagon < Rails::Engine
#{config.root}/app/jobs
]

if Rails.env.development? && config.respond_to?(:view_component)
config.view_component.preview_paths << "#{config.root}/spec/components/previews"
config.view_component.preview_controller = 'WizardsPreviewsController'
end

config.to_prepare do # rubocop:disable Metrics/BlockLength
JobManager.wagon_jobs += [
Export::BackupMitgliederScheduleJob,
Expand Down Expand Up @@ -191,7 +196,7 @@ class Wagon < Rails::Engine
end

initializer 'sac_cas.append_doorkeeper_scope' do |_app|
Doorkeeper.configuration.scopes.add "user_groups"
Doorkeeper.configuration.scopes.add 'user_groups'
end

private
Expand Down
48 changes: 48 additions & 0 deletions spec/components/previews/wizards_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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.

class WizardsPreview < ViewComponent::Preview

def choose_sektion_step(wizards_preview_wizard: {})
wizard = build_wizard(Wizards::Steps::ChooseSektion, wizards_preview_wizard)
render_wrapped(wizard)
end

def choose_sektion_step_with_alert(wizards_preview_wizard: {})
wizard = build_wizard(Wizards::Steps::ChooseSektion, wizards_preview_wizard)
render_wrapped(wizard) do |view_ctx, step|
view_ctx.content_tag(:p, step.group&.name, class: 'alert alert-info') if step.group
end
end

private

def render_wrapped(wizard)
render WrappingComponent.new do
view_ctx = WizardsPreviewsController.new.view_context
view_ctx.standard_form(wizard, url: '', authenticity_token: '', method: :get,
data: { controller: 'autosubmit' }) do |f|
step_component = StepsComponent.new(partials: wizard.partials, step: 0, form: f)
content = yield(view_ctx, wizard.step_at(0)) if block_given?
safe_join([content, view_ctx.render(step_component)].compact)
end
end
end

def build_wizard(step_class, params)
self.class.const_set('Wizard', Class.new(Wizards::Base) { self.steps = [step_class] })
Wizard.new(current_step: 0, current_ability: :current_ability,
**params).tap { |w| w.valid? if params.present? }
end

class WrappingComponent < ViewComponent::Base
haml_template <<~HAML
= content
HAML
end

end
29 changes: 29 additions & 0 deletions spec/models/wizards/steps/choose_sektion_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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'

describe Wizards::Steps::ChooseSektion do
let(:wizard) { Wizards::Base.new(current_step: 0) }
subject(:step) { described_class.new(wizard) }

describe 'validations' do
let(:error) { steps.errors[:group] }

it 'validates presence of group id' do
step.group_id = nil
expect(step).not_to be_valid
expect(step.errors[:group_id]).to eq ['muss ausgefüllt werden']
end

it 'validates type of group id' do
step.group_id = Group::SacCas.first.id
expect(step).not_to be_valid
expect(step.errors[:group_id]).to eq ['ist nicht gültig']
end
end
end
49 changes: 49 additions & 0 deletions spec/views/wizards/steps/_choose_sektion.html.haml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

require 'spec_helper'

describe 'wizards/steps/_choose_sektion.html.haml' do
let(:wizard) { Wizards::Base.new(current_step: 0) }
let(:params) { {} }
let(:step) { Wizards::Steps::ChooseSektion.new(wizard, **params) }
let(:component) do
StepsComponent::ContentComponent.new(
partial: :choose_sektion,
partial_iteration: double(:iter, index: 0),
step: step,
form: form
)
end

let(:form) { StandardFormBuilder.new(:wizard, wizard, view, { builder: StandardFormBuilder }) }

let(:dom) do
render
Capybara::Node::Simple.new(@rendered)
end

before do
allow(Wizards::Base).to receive(:steps).and_return([step.class])
allow(view).to receive_messages(f: form, c: component)
end

it 'renders field with group options' do
expect(dom).to have_select 'Sektion wählen',
options: [
'Bitte wählen',
'SAC Blüemlisalp Ausserberg',
'SAC Blüemlisalp',
'SAC Matterhorn'
]
end

it 'autosubmits field' do
field = dom.find_field 'Sektion wählen'
expect(field.native['data-action']).to eq 'change->autosubmit#save'
end
end

0 comments on commit a3ce736

Please sign in to comment.