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

Add explicit #start_on/#end_on to Role, remove FutureRole, Role#delete_on #339

Closed
wants to merge 13 commits into from
8 changes: 4 additions & 4 deletions app/domain/alumni/invitations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ def process

def relevant_roles
Role
.with_deleted
.where(deleted_at: time_range, alumni_invitation_processed_at: nil)
.with_inactive
.where(end_on: date_range, alumni_invitation_processed_at: nil)
.includes(:person, :group)
end

def time_range
def date_range
from = parse_duration(:alumni, :invitation, :role_deleted_after_ago)
to = parse_duration(:alumni, :invitation, :role_deleted_before_ago)
from.ago..to.ago
(from.ago.to_date..to.ago.to_date)
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/domain/alumni/reminders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module Alumni
class Reminders < Invitations
self.type = :reminder

def time_range
..parse_duration(:alumni, :reminder, :role_deleted_before_ago).ago
def date_range
..parse_duration(:alumni, :reminder, :role_deleted_before_ago).ago.to_date
end
end
end
2 changes: 1 addition & 1 deletion app/domain/event/approver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def approvers_for_groups(groups)
Person.joins(:roles)
.where(roles: {group_id: groups.collect(&:id),
type: role_types.collect(&:sti_name),
deleted_at: nil})
end_on: [nil, ..Time.zone.today]})
.distinct
end

Expand Down
3 changes: 1 addition & 2 deletions app/domain/member_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def region
def members
Person.joins(:roles)
.where(roles: {group_id: abteilung.self_and_descendants,
type: self.class.counted_roles.collect(&:sti_name),
deleted_at: nil})
type: self.class.counted_roles.collect(&:sti_name)})
.distinct
end

Expand Down
26 changes: 2 additions & 24 deletions app/models/pbs/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ module Pbs::Role
before_create :detect_group_membership_notification
after_create :send_group_membership_notification
after_create :send_black_list_mail, if: :person_blacklisted?

after_save :update_primary_group

alias_method_chain :set_first_primary_group, :kantonalverband
alias_method_chain :reset_primary_group, :kantonalverband
end

def detect_group_membership_notification
Expand Down Expand Up @@ -61,25 +56,8 @@ def send_black_list_mail
BlackListMailer.hit(person, group).deliver_now
end

def update_primary_group
if soft_deleted_change?
reset_primary_group
end
end

def soft_deleted_change?
previous_changes.include?(:deleted_at) &&
previous_changes[:deleted_at].first.nil? &&
previous_changes[:deleted_at].last
end

def set_first_primary_group_with_kantonalverband
set_first_primary_group_without_kantonalverband
person.reload.reset_kantonalverband!
end

def reset_primary_group_with_kantonalverband
reset_primary_group_without_kantonalverband
def set_first_primary_group
super
person.reload.reset_kantonalverband!
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ def up
add_column :roles, :alumni_invitation_processed_at, :datetime, null: true
add_column :roles, :alumni_reminder_processed_at, :datetime, null: true

if connection.adapter_name.downcase =~ /sqlite/
if /sqlite/.match?(connection.adapter_name.downcase)
connection.execute <<~SQL
UPDATE roles
SET alumni_invitation_processed_at = '1970-01-01', alumni_reminder_processed_at = '1970-01-01'
WHERE deleted_at < TIME('now')
WHERE en_on < TIME('now')
SQL
else
connection.execute <<~SQL
UPDATE roles
SET alumni_invitation_processed_at = '1970-01-01', alumni_reminder_processed_at = '1970-01-01'
WHERE deleted_at < NOW()
WHERE end_on < NOW()
SQL
end
end
Expand Down
28 changes: 12 additions & 16 deletions db/seeds/development/events.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
# encoding: utf-8

# Copyright (c) 2012-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.

require Rails.root.join('db', 'seeds', 'support', 'event_seeder')
require Rails.root.join("db", "seeds", "support", "event_seeder")

srand(42)

class PbsEventSeeder < EventSeeder

def seed_event(group_id, type)
values = event_values(group_id)
case type
when :course then seed_course(values)
when :camp then seed_camp(values)
when :base then seed_base_event(values)
when :course then seed_course(values)
when :camp then seed_camp(values)
when :base then seed_base_event(values)
end
end

def seed_course(values)
event = super(values.merge(Hash[approvals]))
event.reload
event.state = Event::Course.possible_states.shuffle.first
event.state = Event::Course.possible_states.sample
event.save!
end

Expand Down Expand Up @@ -59,20 +56,19 @@ def camp_group_types
end

def camp_group_ids
Group.where(type: camp_group_types).pluck(:id)
Group.where(type: camp_group_types.map(&:sti_name)).pluck(:id)
end

def camp_attributes(values)
kind = @@kinds.shuffle.first
kind = @@kinds.sample
values.merge(name: "#{kind.try(:short_name)} #{values[:number]}".strip,
kind_id: kind.try(:id),
state: Event::Camp.possible_states.shuffle.first,
signature: Event::Camp.used_attributes.include?(:signature),
external_applications: Event::Camp.used_attributes.include?(:external_applications))
kind_id: kind.try(:id),
state: Event::Camp.possible_states.sample,
signature: Event::Camp.used_attributes.include?(:signature),
external_applications: Event::Camp.used_attributes.include?(:external_applications))
end
end


seeder = PbsEventSeeder.new

layer_types = Group.all_types.select(&:layer).collect(&:sti_name)
Expand All @@ -94,4 +90,4 @@ def camp_attributes(values)
end
end

Event::Participation.update_all(state: 'assigned', active: true)
Event::Participation.update_all(state: "assigned", active: true)
6 changes: 3 additions & 3 deletions lib/tasks/people.rake
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace :people do
roles = Role.deleted.
select('type, label, person_id, DATE(deleted_at) as deleted_at').
distinct.
where(deleted_at: last_year_range, type: types)
where(end_on: last_year_range, type: types)

def human_attrs(attrs, model_class)
attrs.collect { |attr| model_class.human_attribute_name(attr) }
Expand All @@ -69,7 +69,7 @@ namespace :people do
roles.each do |role|

person_values = values(person_attrs, role.person) do |model, attr|

value = model.send(attr)
case attr
when 'birthday' then value ? value.strftime('%Y-%m-%d') : nil
Expand All @@ -82,7 +82,7 @@ namespace :people do

role_values = values(role_attrs, role) do |model, attr|
case attr
when 'deleted_at' then model.send(attr).strftime('%Y-%m-%d')
when 'deleted_at' then model.send(:end_on).strftime('%Y-%m-%d')
when 'Rolle' then model.model_name.human
else model.send(attr)
end
Expand Down
46 changes: 21 additions & 25 deletions spec/controllers/population_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,76 +1,72 @@
# encoding: utf-8

# Copyright (c) 2012-2014, 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.

require 'spec_helper'
require "spec_helper"

describe PopulationController do

let(:abteilung) { groups(:schekka) }
let(:pegasus) { groups(:pegasus) }


let!(:leader) { Fabricate(Group::Abteilung::Abteilungsleitung.name.to_sym, group: abteilung).person }
let!(:guide) { Fabricate(Group::Abteilung::StufenleitungWoelfe.name.to_sym, group: abteilung).person }
let!(:webmaster) { Fabricate(Group::Abteilung::Webmaster.name.to_sym, group: abteilung).person }
let!(:deleted) do
Fabricate(Group::Abteilung::AbteilungsleitungStv.name.to_sym,
group: abteilung,
created_at: 2.years.ago,
deleted_at: 1.year.ago)
group: abteilung,
created_at: 2.years.ago,
end_on: 1.year.ago)
end
let!(:group_leader) { Fabricate(Group::Pfadi::Mitleitung.name.to_sym, group: pegasus, person: guide).person }
let!(:child) { Fabricate(Group::Pfadi::Pfadi.name.to_sym, group: pegasus).person }

before { sign_in(leader) }

describe 'GET index' do
before { get :index, params: { id: abteilung.id } }
describe "GET index" do
before { get :index, params: {id: abteilung.id} }

describe 'groups' do
describe "groups" do
subject { assigns(:groups) }

it do
is_expected.to eq([abteilung,
groups(:sunnewirbu),
groups(:baereried),
groups(:medusa),
groups(:pegasus),
groups(:poseidon),
groups(:rovers),
groups(:elternrat),
groups(:fussballers)])
groups(:sunnewirbu),
groups(:baereried),
groups(:medusa),
groups(:pegasus),
groups(:poseidon),
groups(:rovers),
groups(:elternrat),
groups(:fussballers)])
end
end

describe 'people by group' do
describe "people by group" do
subject { assigns(:people_by_group) }

it { expect(subject[abteilung].collect(&:to_s)).to contain_exactly(*[leader, people(:al_schekka), guide].collect(&:to_s)) }
it { expect(subject[groups(:pegasus)].collect(&:to_s)).to contain_exactly(*[group_leader, child, people(:child)].collect(&:to_s)) }
it { expect(subject[groups(:baereried)]).to be_nil } # no people in group - not displayed at all
end

describe 'complete' do
describe "complete" do
subject { assigns(:people_data_complete) }

it { is_expected.to be_falsey }
end

describe 'shows year data' do
describe "shows year data" do
subject { assigns(:members_per_birthyear) }

it { expect(subject.map(&:count).sum).to eq(assigns(:total).total) }
end
end

describe 'GET index does not include deleted groups' do
describe "GET index does not include deleted groups" do
before do
groups(:poseidon).destroy
get :index, params: { id: abteilung.id }
get :index, params: {id: abteilung.id}
end

subject { assigns(:groups) }
Expand Down
Loading
Loading