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

Validate course number #318

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
10 changes: 10 additions & 0 deletions app/models/pbs/event/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Pbs::Event::Course
APPROVALS = %w(requires_approval_abteilung requires_approval_region
requires_approval_kantonalverband requires_approval_bund).freeze

COURSE_NUMBER_REGEX = /\APBS CH(?: [A-Z]{2})? [0-9]{3}-[0-9]{2}\z/.freeze
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following what's specified in https://pilot.scouts.ch (page 12 in the current version)


included do
include Pbs::Event::RestrictedRole

Expand Down Expand Up @@ -42,6 +44,14 @@ module Pbs::Event::Course
validates :bsv_days, numericality: { greater_than_or_equal_to: 0, allow_blank: true }
validate :assert_bsv_days_precision

validates :number, format: { with: COURSE_NUMBER_REGEX,
message: :must_adhere_to_format,
if: :validate_number? }

def validate_number?
(new_record? || number_changed? || kind_id_changed?) && kind.validate_course_number
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to update existing records as long as course number and event kind are left untouched

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(new_record? || number_changed? || kind_id_changed?) && kind.validate_course_number
return false unless kind.validate_course_number?
new_record? ||
number_changed? ||
kind_id_changed?

Find ich persönlich leserlicher

end

### CALLBACKS
after_initialize :become_campy
before_save :set_requires_approval
Expand Down
2 changes: 1 addition & 1 deletion app/views/event/kinds/_fields_pbs.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
-# or later. See the COPYING file at the top-level directory or at
-# https://github.com/hitobito/hitobito_pbs.

= f.labeled_input_fields :documents_text, :campy, :can_have_confirmations, :confirmation_name
= f.labeled_input_fields :documents_text, :campy, :can_have_confirmations, :confirmation_name, :validate_course_number
5 changes: 5 additions & 0 deletions config/locales/models.pbs.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,7 @@ de:
campy: Kurs als Lager anmelden
can_have_confirmations: Export Kursbestätigungen aktivieren
confirmation_name: Name der Kursart im Export-Tool
validate_course_number: Kursnummer validieren

event/participation:
additional_information: Wie möchtest du dich im Anlass ernähren? Was sind deine Essgewohnheiten?
Expand Down Expand Up @@ -1362,6 +1363,10 @@ de:
attributes:
base:
cannot_remove_parent_id: 'Das übergeordnete Lager kann nicht mehr verlassen werden, da es nicht mehr im Status "Erstellt" ist'
event/course:
attributes:
number:
must_adhere_to_format: muss dem Format "PBS CH KV XXX-JJ" entsprechen
person:
attributes:
prefers_digital_correspondence:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Copyright (c) 2023, Pfadibewegung Schweiz. This file is part of
# hitobito_cvp and licensed under the Affero General Public License version 3
# hitobito_pbs 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.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2024, Pfadibewegung Schweiz. This file is part of
# hitobito_pbs 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.

class AddValidateCourseNumberToEventKinds < ActiveRecord::Migration[6.1]
def change
add_column :event_kinds, :validate_course_number, :boolean, default: false, null: false
simfeld marked this conversation as resolved.
Show resolved Hide resolved
end
end
2 changes: 1 addition & 1 deletion lib/hitobito_pbs/wagon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Wagon < Rails::Engine
:prefers_digital_correspondence]
GroupsController.permitted_attrs += [:hostname]
Event::KindsController.permitted_attrs += [:documents_text, :campy, :can_have_confirmations,
:confirmation_name]
:confirmation_name, :validate_course_number]
QualificationKindsController.permitted_attrs += [:manual]
ServiceTokensController.permitted_attrs += [:group_health, :census_evaluations]

Expand Down
7 changes: 5 additions & 2 deletions spec/fixtures/event/kinds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_jubla.

lpk: {}
lpk:
validate_course_number: false

bkws: {}
bkws:
validate_course_number: false

fut:
campy: true
validate_course_number: false
101 changes: 101 additions & 0 deletions spec/models/event/course_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,105 @@ def person(name, role)
expect(event).to_not be_attr_used(:globally_visible)
end
end

context 'validations' do
let(:kind_with_validation) { Fabricate(:event_kind, validate_course_number: true) }
let(:kind_without_validation) { event_kinds(:lpk) }
VALID_NUMBERS = ['PBS CH 947-00', 'PBS CH XX 007-99', 'PBS CH AB 123-45'].freeze
INVALID_NUMBERS = ['PBS CH 000-FP', 'PBS XY XX 475-31', 'CH AB 123-45', 'PBS CH 123-456',
'PBS CH AB 12-345', 'PBSCHAB123-45'].freeze

context 'course number' do

subject { Fabricate(:course, groups: [groups(:be)], kind: kind_with_validation, number: 'PBS CH BE 123-24') }

VALID_NUMBERS.each do |number|
it "'#{number}' is valid" do
subject.number = number
expect(subject.valid?).to be_truthy
end
end

INVALID_NUMBERS.each do |number|
it "'#{number}' is invalid" do
subject.number = number
expect(subject.valid?).to be_falsey
expect(subject.errors[:number]).to be_present
end
end
end

context 'when updating an existing event' do
subject { event }

context 'with event kind having validation enabled' do
before do
subject.kind = kind_with_validation
# fixture has an invalid course number, so we need to skip validations
subject.save(validate: false)
end

it 'is invalid for a number not matching the expected format' do
subject.number = INVALID_NUMBERS.first
expect(subject.valid?).to be_falsey
expect(subject.errors[:number]).to be_present
end

it 'is valid for a number matching the expected format' do
subject.number = VALID_NUMBERS.first
expect(subject.valid?).to be_truthy
end

it 'is valid when switching to an event kind with validation disabled' do
subject.kind = kind_without_validation
expect(subject.valid?).to be_truthy
end
end

context 'with event kind having validation disabled' do
before do
subject.kind = kind_without_validation
subject.save
end

it 'is valid for a number not matching the expected format' do
subject.number = INVALID_NUMBERS.first
expect(subject.valid?).to be_truthy
end

it 'is invalid when switching to an event kind with validation enabled' do
subject.kind = kind_with_validation
expect(subject.valid?).to be_falsey
expect(subject.errors[:number]).to be_present
end
end
end

context 'when creating a new event' do
subject { Event::Course.new(groups: [groups(:be)],
name: Faker::Lorem.word,
dates: [Event::Date.new(start_at: Time.zone.now)]) }

it 'is valid for an event kind with validation enabled and a valid number' do
subject.kind = kind_with_validation
subject.number = VALID_NUMBERS.first
subject.validate!
puts subject.errors.full_messages
expect(subject.valid?).to be_truthy
end

it 'is invalid for an event kind with validation enabled and an invalid number' do
subject.kind = kind_with_validation
subject.number = INVALID_NUMBERS.first
expect(subject.valid?).to be_falsey
expect(subject.errors[:number]).to be_present
end

it 'is valid for an event kind with validation disabled and an invalid number' do
subject.kind = kind_without_validation
subject.number = INVALID_NUMBERS.first
expect(subject.valid?).to be_truthy
end
end
end
end
Loading