From 61cda9729b62411d427fc90fb0be65bce6462502 Mon Sep 17 00:00:00 2001 From: Michael Schaer Date: Sat, 6 Mar 2021 22:50:11 +0100 Subject: [PATCH 1/3] Merging bsv_days with training_days --- .../event/attendances_controller.rb | 8 ++--- .../tabular/events/advanced_bsv_list.rb | 1 - .../pbs/export/tabular/events/bsv_row.rb | 16 ++++----- .../tabular/people/participation_row.rb | 4 +-- .../tabular/people/participations_full.rb | 2 +- app/models/event/camp.rb | 1 - app/models/pbs/event/course.rb | 5 +-- app/models/pbs/event/participation.rb | 17 --------- app/views/event/attendances/_list.html.haml | 8 ++--- app/views/events/_fields_pbs.html.haml | 4 --- db/migrate/20210306220612_remove_bsv_days.rb | 20 +++++++++++ .../event/attendances_controller_spec.rb | 36 +++++++++---------- .../event/lists_controller_spec.rb | 5 ++- .../export/tabular/events/bsv_row_spec.rb | 15 ++------ .../people/participations_full_spec.rb | 9 ++--- spec/fixtures/events.yml | 1 - spec/models/event/camp_spec.rb | 1 - spec/models/event/participation_spec.rb | 14 ++++---- 18 files changed, 70 insertions(+), 97 deletions(-) create mode 100644 db/migrate/20210306220612_remove_bsv_days.rb diff --git a/app/controllers/event/attendances_controller.rb b/app/controllers/event/attendances_controller.rb index 1ac3782d5..ed8876da8 100644 --- a/app/controllers/event/attendances_controller.rb +++ b/app/controllers/event/attendances_controller.rb @@ -18,11 +18,11 @@ def index end def update - bsv_days = params[:bsv_days] - if bsv_days + training_days = params[:training_days] + if training_days event.participations.each do |p| - next unless bsv_days.key?(p.id.to_s) - p.update(bsv_days: bsv_days[p.id.to_s]) + next unless training_days.key?(p.id.to_s) + p.update(training_days: training_days[p.id.to_s]) end end redirect_to attendances_group_event_path(group, event), diff --git a/app/domain/export/tabular/events/advanced_bsv_list.rb b/app/domain/export/tabular/events/advanced_bsv_list.rb index e960e1daa..12ea4c651 100644 --- a/app/domain/export/tabular/events/advanced_bsv_list.rb +++ b/app/domain/export/tabular/events/advanced_bsv_list.rb @@ -30,7 +30,6 @@ def add_main_labels(labels) def add_counts(labels) labels[:training_days] = 'Ausbildungstage' - labels[:bsv_days] = 'BSV Tage' labels[:bsv_eligible_participations_count] = 'Berechtigte Teilnehmende (17-30)' labels[:bsv_eligible_attendance_summary] = 'Berechtigte Teilnehmende (17-30) x Tage' labels[:bsv_eligible_attendances] = 'Berechtigte Tage' diff --git a/app/domain/pbs/export/tabular/events/bsv_row.rb b/app/domain/pbs/export/tabular/events/bsv_row.rb index b8d8ad9e4..8a6b42bd4 100644 --- a/app/domain/pbs/export/tabular/events/bsv_row.rb +++ b/app/domain/pbs/export/tabular/events/bsv_row.rb @@ -32,10 +32,6 @@ def training_days Kernel.format('%g', entry.training_days) if entry.training_days end - def bsv_days - Kernel.format('%g', entry.bsv_days) if entry.bsv_days - end - def kurs_kind entry.kind.to_s end @@ -68,19 +64,19 @@ def all_participants_count end def all_participants_attendances - active_participations.map(&:bsv_days).compact.sum + active_participations.map(&:training_days).compact.sum end def bsv_eligible_attendances - bsv_eligible_participations.map(&:bsv_days).compact.sum + bsv_eligible_participations.map(&:training_days).compact.sum end def all_participants_attendance_summary - format_attendances(attendance_groups_by_bsv_days_for(active_participations)) + format_attendances(attendance_groups_by_training_days_for(active_participations)) end def bsv_eligible_attendance_summary - format_attendances(attendance_groups_by_bsv_days_for(bsv_eligible_participations)) + format_attendances(attendance_groups_by_training_days_for(bsv_eligible_participations)) end private @@ -106,10 +102,10 @@ def active_participations @active_participations ||= entry.participations.where(active: true) end - def attendance_groups_by_bsv_days_for(participations) + def attendance_groups_by_training_days_for(participations) Hash[ participations. - group_by { |participation| participation.bsv_days || 0 }. + group_by { |participation| participation.training_days || 0 }. transform_values { |participation_group| participation_group.count }. sort_by { |days, count| days.to_f } ] diff --git a/app/domain/pbs/export/tabular/people/participation_row.rb b/app/domain/pbs/export/tabular/people/participation_row.rb index 34152028e..86207784d 100644 --- a/app/domain/pbs/export/tabular/people/participation_row.rb +++ b/app/domain/pbs/export/tabular/people/participation_row.rb @@ -11,8 +11,8 @@ module Tabular module People module ParticipationRow - def bsv_days - participation.bsv_days || participation.event.bsv_days + def training_days + participation.training_days || participation.event.training_days end end diff --git a/app/domain/pbs/export/tabular/people/participations_full.rb b/app/domain/pbs/export/tabular/people/participations_full.rb index d51e647f7..c1831d978 100644 --- a/app/domain/pbs/export/tabular/people/participations_full.rb +++ b/app/domain/pbs/export/tabular/people/participations_full.rb @@ -18,7 +18,7 @@ module ParticipationsFull def build_attribute_labels_with_pbs build_attribute_labels_without_pbs.tap do |labels| - labels[:bsv_days] = ::Event::Participation.human_attribute_name(:bsv_days) + labels[:training_days] = ::Event::Participation.human_attribute_name(:training_days) end end end diff --git a/app/models/event/camp.rb b/app/models/event/camp.rb index 85d8ab261..140210567 100644 --- a/app/models/event/camp.rb +++ b/app/models/event/camp.rb @@ -90,7 +90,6 @@ # hidden_contact_attrs :text(65535) # contact_attrs_passed_on_to_supercamp :text(65535) # display_booking_info :boolean default(TRUE), not null -# bsv_days :decimal(6, 2) # class Event::Camp < Event diff --git a/app/models/pbs/event/course.rb b/app/models/pbs/event/course.rb index be823fe12..f9611c58e 100644 --- a/app/models/pbs/event/course.rb +++ b/app/models/pbs/event/course.rb @@ -16,7 +16,7 @@ module Pbs::Event::Course included do include Event::RestrictedRole - self.used_attributes += [:advisor_id, :express_fee, :bsv_days, :has_confirmations] + + self.used_attributes += [:advisor_id, :express_fee, :has_confirmations] + LANGUAGES.collect { |key| "language_#{key}".to_sym } + APPROVALS.collect(&:to_sym) self.used_attributes -= [:requires_approval, :j_s_kind, :canton, :camp_submitted, @@ -35,10 +35,7 @@ module Pbs::Event::Course restricted_role :advisor, Event::Course::Role::Advisor - validates :number, presence: true - validates :bsv_days, numericality: { greater_than_or_equal_to: 0, allow_blank: true } - validate :assert_bsv_days_precision ### CALLBACKS after_initialize :become_campy diff --git a/app/models/pbs/event/participation.rb b/app/models/pbs/event/participation.rb index 82a1ec736..c9bc9a198 100644 --- a/app/models/pbs/event/participation.rb +++ b/app/models/pbs/event/participation.rb @@ -10,9 +10,6 @@ module Pbs::Event::Participation extend ActiveSupport::Concern included do - validates :bsv_days, numericality: { greater_than_or_equal_to: 0, allow_blank: true } - validate :assert_bsv_days_precision - validate :assert_bsv_days_set after_create :send_black_list_mail, if: :person_blacklisted? end @@ -26,20 +23,6 @@ def send_black_list_mail BlackListMailer.hit(person, event).deliver_now end - def assert_bsv_days_precision - if bsv_days && bsv_days % 0.5 != 0 - msg = I18n.t('activerecord.errors.messages.must_be_multiple_of', multiple: 0.5) - errors.add(:bsv_days, msg) - end - end - - def assert_bsv_days_set - if event.try(:bsv_days).present? && %w[attended].include?(state) && bsv_days.blank? - msg = I18n.t('activerecord.errors.messages.must_exist') - errors.add(:bsv_days, msg) - end - end - def person_blacklisted? person.black_listed? end diff --git a/app/views/event/attendances/_list.html.haml b/app/views/event/attendances/_list.html.haml index 762d08af2..9bc55ccef 100644 --- a/app/views/event/attendances/_list.html.haml +++ b/app/views/event/attendances/_list.html.haml @@ -8,7 +8,7 @@ %th{ colspan: 3 } %h2= caption %th - = entries.sum { |p| p.bsv_days || @event.bsv_days || 0 } + = entries.sum { |p| p.training_days || @event.training_days || 0 } %tbody - entries.each do |p| %tr{id: dom_id(p)} @@ -17,11 +17,11 @@ %td= p.roles_short %td= p.town %td - = number_field_tag("bsv_days[#{p.id}]", - p.bsv_days || @event.bsv_days, + = number_field_tag("training_days[#{p.id}]", + p.training_days || @event.training_days, min: 0, step: 0.5, size: 10, class: 'span2') - - if p.bsv_days.nil? + - if p.training_days.nil? %span.muted{ style: 'float:xleft;' }= t('.not_persisted') diff --git a/app/views/events/_fields_pbs.html.haml b/app/views/events/_fields_pbs.html.haml index 4dad1a3eb..55e021df8 100644 --- a/app/views/events/_fields_pbs.html.haml +++ b/app/views/events/_fields_pbs.html.haml @@ -19,9 +19,6 @@ - entry.used?(:training_days) do = f.labeled_input_field(:training_days, maxlength: 10) - - entry.used?(:bsv_days) do - = f.labeled_input_field :bsv_days - - entry.used?(:j_s_kind) do = f.labeled(:j_s_kind) do = f.inline_radio_button(:j_s_kind, '', t('.j_s_kind_none'), false) @@ -59,4 +56,3 @@ = f.boolean_field :allow_sub_camps, caption: Event::Camp.human_attribute_name(:allow_sub_camps), readonly: entry.sub_camps.any? - diff --git a/db/migrate/20210306220612_remove_bsv_days.rb b/db/migrate/20210306220612_remove_bsv_days.rb new file mode 100644 index 000000000..3ab34bec2 --- /dev/null +++ b/db/migrate/20210306220612_remove_bsv_days.rb @@ -0,0 +1,20 @@ +# Copyright (c) 2021, 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_pbs. + +class RemoveBsvDays < ActiveRecord::Migration[6.0] + def up + add_column :event_participations, :training_days, :decimal, scale: 2, precision: 6 + #TODO: copy values from :bsv_days to :training_days + remove_column :event_participations, :bsv_days + remove_column :events, :bsv_days + end + + def down + remove_column :event_participations, :training_days + #TODO: copy values from :training_days to :bsv_days + add_column :events, :bsv_days, :decimal, scale: 2, precision: 6 + add_column :event_participations, :bsv_days, :decimal, scale: 2, precision: 6 + end +end diff --git a/spec/controllers/event/attendances_controller_spec.rb b/spec/controllers/event/attendances_controller_spec.rb index a284d0ea5..72512182d 100644 --- a/spec/controllers/event/attendances_controller_spec.rb +++ b/spec/controllers/event/attendances_controller_spec.rb @@ -17,20 +17,20 @@ @p1 = Fabricate(Event::Course::Role::Leader.name.to_sym, participation: Fabricate(:event_participation, - event: course, bsv_days: 5)).participation - @p2 = Fabricate(Event::Course::Role::Helper.name.to_sym, participation: Fabricate(:event_participation, event: course, bsv_days: 5)).participation + event: course, training_days: 5)).participation + @p2 = Fabricate(Event::Course::Role::Helper.name.to_sym, participation: Fabricate(:event_participation, event: course, training_days: 5)).participation @p3 = Fabricate(Event::Role::Speaker.name.to_sym, participation: Fabricate(:event_participation, - event: course, bsv_days: 5)).participation + event: course, training_days: 5)).participation @p4 = Fabricate(Event::Role::Cook.name.to_sym, participation: Fabricate(:event_participation, - event: course, bsv_days: 5)).participation + event: course, training_days: 5)).participation @p5 = Fabricate(Event::Course::Role::Participant.name.to_sym, participation: Fabricate(:event_participation, - event: course, bsv_days: 5)).participation + event: course, training_days: 5)).participation @p6 = Fabricate(Event::Course::Role::Participant.name.to_sym, participation: Fabricate(:event_participation, - event: course, bsv_days: 5)).participation + event: course, training_days: 5)).participation end context 'GET index' do @@ -47,12 +47,12 @@ context 'PATCH update' do - it 'updates all given bsv_days' do + it 'updates all given training_days' do patch :update, params: { group_id: group.id, id: course.id, - bsv_days: { + training_days: { @p1.id.to_s => 1.5, @p2.id.to_s => '', @p4.id.to_s => 0 @@ -60,10 +60,10 @@ } expect(response).to redirect_to(attendances_group_event_path(group.id, course.id)) - expect(@p1.reload.bsv_days).to eq(1.5) - expect(@p2.reload.bsv_days).to be_nil - expect(@p3.reload.bsv_days).to eq(5) - expect(@p4.reload.bsv_days).to eq(0) + expect(@p1.reload.training_days).to eq(1.5) + expect(@p2.reload.training_days).to be_nil + expect(@p3.reload.training_days).to eq(5) + expect(@p4.reload.training_days).to eq(0) end it 'ignores invalid values' do @@ -71,7 +71,7 @@ params: { group_id: group.id, id: course.id, - bsv_days: { + training_days: { @p1.id.to_s => -1.5, @p2.id.to_s => RUBY_VERSION >= '2.4.0' ? -23.42 : 'jada', # ruby 2.4's big decimal does not handle strings @p3.id.to_s => 6, @@ -80,11 +80,11 @@ } expect(response).to redirect_to(attendances_group_event_path(group.id, course.id)) - expect(@p1.reload.bsv_days).to eq(5) - expect(@p2.reload.bsv_days).to eq(5) - expect(@p3.reload.bsv_days).to eq(6) - expect(@p4.reload.bsv_days).to eq(5) - expect(@p5.reload.bsv_days).to eq(5) + expect(@p1.reload.training_days).to eq(5) + expect(@p2.reload.training_days).to eq(5) + expect(@p3.reload.training_days).to eq(6) + expect(@p4.reload.training_days).to eq(5) + expect(@p5.reload.training_days).to eq(5) end end diff --git a/spec/controllers/event/lists_controller_spec.rb b/spec/controllers/event/lists_controller_spec.rb index f95f4b400..a77da14ea 100644 --- a/spec/controllers/event/lists_controller_spec.rb +++ b/spec/controllers/event/lists_controller_spec.rb @@ -329,8 +329,7 @@ def create_course(number, date_from, date_to = nil) number: number, state: 'closed', advisor_id: person.id, - training_days: 5, - bsv_days: 3 + training_days: 5 ) course.dates.destroy_all date_from = Date.parse(date_from) @@ -347,7 +346,7 @@ def create_participations(course) def create_participation(course, role) person = Fabricate(:person, birthday: Date.new(1995,1,1), zip_code: 3012) - participation = Fabricate(:event_participation, event: course, person: person, bsv_days: 3) + participation = Fabricate(:event_participation, event: course, person: person) Fabricate(role.to_sym, participation: participation) end diff --git a/spec/domain/export/tabular/events/bsv_row_spec.rb b/spec/domain/export/tabular/events/bsv_row_spec.rb index 5836d1942..2e1f780ed 100644 --- a/spec/domain/export/tabular/events/bsv_row_spec.rb +++ b/spec/domain/export/tabular/events/bsv_row_spec.rb @@ -33,15 +33,6 @@ expect(row.fetch(:training_days)).to be_blank end - it 'formats bsv_days' do - course.bsv_days = 1 - expect(row.fetch(:bsv_days)).to eq '1' - course.bsv_days = 1.5 - expect(row.fetch(:bsv_days)).to eq '1.5' - course.bsv_days = nil - expect(row.fetch(:bsv_days)).to be_blank - end - it 'bsv_eligible_participants_count is zero' do expect(row.fetch(:bsv_eligible_participations_count)).to eq 0 end @@ -55,7 +46,7 @@ describe 'advanced_bsv_export' do before do - course.bsv_days = 7 + course.training_days = 7 create_eligible_participation(course, location: bern, age: 20) create_eligible_participation(course, location: bern, age: 12) end @@ -80,11 +71,11 @@ def fabricate_course course end - def create_eligible_participation(course, age: 20, location: nil, bsv_days: course.bsv_days) + def create_eligible_participation(course, age: 20, location: nil, training_days: course.training_days) birthday = (course.dates.first.start_at - age.years).to_date person = Fabricate(:person, birthday: birthday, location: location) participation = Fabricate(:event_participation, event: course, person: person, - bsv_days: bsv_days, state: :attended) + training_days: training_days, state: :attended) Fabricate(Event::Course::Role::Participant.name, participation: participation) end diff --git a/spec/domain/export/tabular/people/participations_full_spec.rb b/spec/domain/export/tabular/people/participations_full_spec.rb index 59790e1c6..c57e62e14 100644 --- a/spec/domain/export/tabular/people/participations_full_spec.rb +++ b/spec/domain/export/tabular/people/participations_full_spec.rb @@ -11,16 +11,12 @@ describe Export::Tabular::People::ParticipationsFull do let(:person) { people(:child) } - let(:participation) { Fabricate(:event_participation, person: person, event: events(:top_course), bsv_days: 4.5) } + let(:participation) { Fabricate(:event_participation, person: person, event: events(:top_course), training_days: 4.5) } let(:list) { [participation] } let(:people_list) { Export::Tabular::People::ParticipationsFull.new(list) } subject { people_list.attribute_labels } - context 'bsv days' do - its([:bsv_days]) { should eq 'BSV-Tage' } - end - context 'integration' do let(:data) { Export::Tabular::People::ParticipationsFull.export(:csv, list) } let(:csv) { CSV.parse(data, headers: true, col_sep: Settings.csv.separator) } @@ -35,9 +31,8 @@ its(['Vorname']) { should eq person.first_name } its(['Rollen']) { should be_blank } its(['Anmeldedatum']) { should eq I18n.l(Time.zone.now.to_date) } - its(['BSV-Tage']) { should eq '4.5' } + its(['Ausbildungstage']) { should eq '4.5' } end end end - diff --git a/spec/fixtures/events.yml b/spec/fixtures/events.yml index a17ad7c90..acf6211eb 100644 --- a/spec/fixtures/events.yml +++ b/spec/fixtures/events.yml @@ -87,7 +87,6 @@ # required_contact_attrs :text # hidden_contact_attrs :text # display_booking_info :boolean default(TRUE), not null -# bsv_days :decimal(6, 2) # top_event: diff --git a/spec/models/event/camp_spec.rb b/spec/models/event/camp_spec.rb index 34f4233d8..f7a852995 100644 --- a/spec/models/event/camp_spec.rb +++ b/spec/models/event/camp_spec.rb @@ -87,7 +87,6 @@ # required_contact_attrs :text # hidden_contact_attrs :text # display_booking_info :boolean default(TRUE), not null -# bsv_days :decimal(6, 2) # diff --git a/spec/models/event/participation_spec.rb b/spec/models/event/participation_spec.rb index f0cb55938..4c39c5283 100644 --- a/spec/models/event/participation_spec.rb +++ b/spec/models/event/participation_spec.rb @@ -116,7 +116,7 @@ def create_participant(state) end - context '#bsv_days' do + context '#training_days' do let(:participation) { Fabricate(:pbs_participation, event: event) } it 'is valid when empty' do @@ -124,23 +124,23 @@ def create_participant(state) end it 'is valid when multiple of 0.5' do - participation.bsv_days = 3.5 + participation.training_days = 3.5 expect(participation).to be_valid end it 'is not valid when negative' do - participation.bsv_days = -1 + participation.training_days = -1 expect(participation).not_to be_valid end it 'is not valid when not multiple of 0.5' do - participation.bsv_days = 2.25 + participation.training_days = 2.25 expect(participation).not_to be_valid end - it 'is not valid when course has bsv_days set' do - participation.event.bsv_days = 3 - participation.bsv_days = nil + it 'is not valid when course has training_days set' do + participation.event.training_days = 3 + participation.training_days = nil participation.state = :attended expect(participation).not_to be_valid end From cf8b47d1790631f36afc8dbc347e64828f83ae09 Mon Sep 17 00:00:00 2001 From: Michael Schaer Date: Sun, 7 Mar 2021 00:00:52 +0100 Subject: [PATCH 2/3] Remove unused Migration --- db/migrate/20210306220612_remove_bsv_days.rb | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 db/migrate/20210306220612_remove_bsv_days.rb diff --git a/db/migrate/20210306220612_remove_bsv_days.rb b/db/migrate/20210306220612_remove_bsv_days.rb deleted file mode 100644 index 3ab34bec2..000000000 --- a/db/migrate/20210306220612_remove_bsv_days.rb +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright (c) 2021, 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_pbs. - -class RemoveBsvDays < ActiveRecord::Migration[6.0] - def up - add_column :event_participations, :training_days, :decimal, scale: 2, precision: 6 - #TODO: copy values from :bsv_days to :training_days - remove_column :event_participations, :bsv_days - remove_column :events, :bsv_days - end - - def down - remove_column :event_participations, :training_days - #TODO: copy values from :training_days to :bsv_days - add_column :events, :bsv_days, :decimal, scale: 2, precision: 6 - add_column :event_participations, :bsv_days, :decimal, scale: 2, precision: 6 - end -end From cba3ecbb17dda6267488d0f73ff923a6cd15d72d Mon Sep 17 00:00:00 2001 From: Michael-Schaer Date: Sun, 7 Mar 2021 14:27:50 +0100 Subject: [PATCH 3/3] Naming and simplification --- ...210306220439_merge_bsv_and_training_days.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 db/migrate/20210306220439_merge_bsv_and_training_days.rb diff --git a/db/migrate/20210306220439_merge_bsv_and_training_days.rb b/db/migrate/20210306220439_merge_bsv_and_training_days.rb new file mode 100644 index 000000000..0a8438eb9 --- /dev/null +++ b/db/migrate/20210306220439_merge_bsv_and_training_days.rb @@ -0,0 +1,18 @@ +# Copyright (c) 2021, 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_pbs. + +class MergeBsvAndTrainingDays < ActiveRecord::Migration[6.0] + def up + rename_column:event_participations, :bsv_days, :training_days + Event.update_all "training_days = bsv_days" + remove_column :events, :bsv_days + end + + def down + rename_column:event_participations, :training_days, :bsv_days + add_column :events, :bsv_days, :decimal, scale: 2, precision: 6 + Event.update_all "bsv_days = training_days" + end +end