-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement choose_sektion_form step, (#624)
- Loading branch information
1 parent
2504ca0
commit a3ce736
Showing
9 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
spec/views/wizards/steps/_choose_sektion.html.haml_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |