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

WIP: Merge bsv and training days #189

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions app/controllers/event/attendances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 0 additions & 1 deletion app/domain/export/tabular/events/advanced_bsv_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 6 additions & 10 deletions app/domain/pbs/export/tabular/events/bsv_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 }
]
Expand Down
4 changes: 2 additions & 2 deletions app/domain/pbs/export/tabular/people/participation_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion app/models/event/camp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions app/models/pbs/event/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
17 changes: 0 additions & 17 deletions app/models/pbs/event/participation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions app/views/event/attendances/_list.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand All @@ -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')
4 changes: 0 additions & 4 deletions app/views/events/_fields_pbs.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -59,4 +56,3 @@
= f.boolean_field :allow_sub_camps,
caption: Event::Camp.human_attribute_name(:allow_sub_camps),
readonly: entry.sub_camps.any?

18 changes: 18 additions & 0 deletions db/migrate/20210306220439_merge_bsv_and_training_days.rb
Original file line number Diff line number Diff line change
@@ -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
36 changes: 18 additions & 18 deletions spec/controllers/event/attendances_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,31 +47,31 @@

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
}
}

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
patch :update,
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,
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions spec/controllers/event/lists_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
15 changes: 3 additions & 12 deletions spec/domain/export/tabular/events/bsv_row_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
9 changes: 2 additions & 7 deletions spec/domain/export/tabular/people/participations_full_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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

1 change: 0 additions & 1 deletion spec/fixtures/events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion spec/models/event/camp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#


Expand Down
Loading