Commit c270ddb5 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻 Committed by Kaspar
Browse files

Model scopes and some view adaptions to resolve new state ...

... and handle acceptance with enum field
parent db17b573
......@@ -77,6 +77,7 @@ class VolunteersController < ApplicationController
end
def volunteer_params
params.require(:volunteer).permit(volunteer_attributes, :bank, :iban, :waive, :external)
params.require(:volunteer).permit(volunteer_attributes, :bank, :iban, :waive, :acceptance,
:take_more_assignments, :external)
end
end
......@@ -49,6 +49,7 @@ class Assignment < ApplicationRecord
scope :start_within, ->(date_range) { where(period_start: date_range) }
scope :active, (-> { not_ended.started })
scope :inactive, (-> { ended })
scope :active_between, lambda { |start_date, end_date|
start_within(start_date..end_date)
......
......@@ -9,6 +9,7 @@ class Volunteer < ApplicationRecord
before_validation :handle_external
before_save :default_state
before_save :record_acceptance_changed
# States
REGISTERED = 'registered'.freeze
......@@ -28,9 +29,10 @@ class Volunteer < ApplicationRecord
STATES = STATES_FOR_REVIEWED.dup.unshift(REGISTERED).freeze
enum acceptance: [:undecided, :accepted, :rejected, :resigned]
belongs_to :user, -> { with_deleted }, optional: true
belongs_to :registrar, optional: true,
class_name: 'User', foreign_key: 'registrar_id'
belongs_to :registrar, optional: true, class_name: 'User', foreign_key: 'registrar_id'
has_many :certificates
......@@ -62,12 +64,17 @@ class Volunteer < ApplicationRecord
default_scope { order(created_at: :desc) }
scope :seeking_clients, (-> { where(state: SEEKING_CLIENTS) })
scope :created_between, ->(start_date, end_date) { where(created_at: start_date..end_date) }
scope :created_before, ->(max_time) { where('volunteers.created_at < ?', max_time) }
scope :created_after, ->(min_time) { where('volunteers.created_at > ?', min_time) }
scope :with_hours, (-> { joins(:hours).distinct })
scope :all_accepted, (-> { where(acceptance: :accepted) })
scope :all_resigned, (-> { where(acceptance: :resigned) })
scope :all_rejected, (-> { where(acceptance: :rejected) })
scope :all_undecided, (-> { where(acceptance: :undecided) })
scope :with_assignments, (-> { joins(:assignments).distinct })
scope :with_active_assignments, (-> { joins(:assignments).merge(Assignment.active).distinct })
scope :with_active_assignments_between, lambda { |start_date, end_date|
......@@ -79,10 +86,28 @@ class Volunteer < ApplicationRecord
scope :not_in_any_group_offer, lambda {
left_joins(:group_offers).where(group_offers_volunteers: { volunteer_id: nil })
}
scope :with_inactive_assignments, (-> { joins(:assignments).merge(Assignment.inactive).distinct })
scope :external, (-> { where(external: true) })
scope :internal, (-> { where(external: false) })
scope :will_take_more_assignments, (-> { where(take_more_assignments: true) })
scope :seeking_clients, lambda {
all_inactive + all_active.will_take_more_assignments
}
scope :all_active, (-> { all_accepted.with_active_assignments })
scope :all_inactive, lambda {
all_accepted.without_assignment + all_accepted.with_inactive_assignments
}
def active?
accepted? && assignments.active.any?
end
def inactive?
accepted? && assignments.active.blank?
end
def handle_external
contact.external = true if external
......@@ -136,22 +161,28 @@ class Volunteer < ApplicationRecord
state == REGISTERED
end
def accepted?
state == ACCEPTED
end
def rejected?
state == REJECTED
end
def resigned?
state == RESIGNED
end
# def accepted?
# state == ACCEPTED
# end
# def rejected?
# state == REJECTED
# end
# def resigned?
# state == RESIGNED
# end
def self.state_collection
STATES.map(&:to_sym)
end
def self.acceptance_collection
acceptances.keys.map(&:to_sym)
end
SINGLE_ACCOMPANIMENTS = [:man, :woman, :family, :kid, :unaccompanied].freeze
GROUP_ACCOMPANIMENTS = [:sport, :creative, :music, :culture, :training, :german_course,
:dancing, :health, :cooking, :excursions, :women, :teenagers,
......@@ -176,6 +207,10 @@ class Volunteer < ApplicationRecord
private
def record_acceptance_changed
self["#{acceptance_change[1]}_at".to_sym] = Time.zone.now if acceptance_changed?
end
def default_state
self.state ||= REGISTERED
end
......
= simple_error_notice f
- if policy(Volunteer).supervisor_privileges?
= f.input :external, input_html: { data: { state: { remove: ['contacted', 'resigned', 'inactive', 'rejected'], selected: 'accepted' }, hide: ['banc-data', 'checklist'] }, class: 'volunteer-active-checkbox-changes' }
fieldset
legend= t_attr(:state)
.row
.col-xs-12
= f.input :external, input_html: { data: { state: { remove: ['contacted', 'resigned', 'inactive', 'rejected'], selected: 'accepted' }, hide: ['banc-data', 'checklist'] }, class: 'volunteer-active-checkbox-changes' }
.col-xs-6
= f.input :state, collection: Volunteer.state_collection, default: :registered
.col-xs-6
= f.input :rejection_type, collection: Volunteer::REJECTIONS, as: :radio_buttons
.col-xs-12
= f.input :rejection_text
legend= t('.acceptance.management')
= f.input :acceptance, collection: Volunteer.acceptance_collection, as: :radio_buttons, include_blank: false, selected: @volunteer.acceptance.to_i
- unless @volunteer.undecided?
p= "#{t_attr(:accepted_at)} #{l(@volunteer.accepted_at)}" if @volunteer.accepted_at.present?
p= "#{t_attr(:undecided_at)} #{l(@volunteer.undecided_at)}" if @volunteer.undecided_at.present?
p= "#{t_attr(:rejected_at)} #{l(@volunteer.rejected_at)}" if @volunteer.rejected_at.present?
p= "#{t_attr(:resigned_at)} #{l(@volunteer.resigned_at)}" if @volunteer.resigned_at.present?
= f.input :state, collection: Volunteer.state_collection, default: :registered
= f.input :rejection_type, collection: Volunteer::REJECTIONS, as: :radio_buttons
= f.input :rejection_text
- if policy(Volunteer).checklist?
fieldset.checklist
......
......@@ -20,6 +20,4 @@ nav.navbar.section-navigation
= list_boolean_filter_dropdown(:available, Volunteer::AVAILABILITY)
hr
= render 'columns', volunteers: @volunteers
......@@ -101,6 +101,9 @@ h3= t('personal_background')
td= t_attr(:interests)
td= @volunteer.interests
- if policy(Volunteer).destroy?
tr
td= t_attr(:acceptance)
td= @volunteer.acceptance
tr
td= t_attr(:state)
td= t("state.#{@volunteer.state}")
......
......@@ -124,8 +124,22 @@ de:
resigned: Abgemeldet
updated_at: Geändert um
volunteer_applications: &id-volunteers-by-actions
acceptance: &id-volunteer-acceptance
management: Aufnahme Verwaltung
accepted: Akzeptiert
accept: Akzeptieren
accepted_at: Akzeptiert am
registered: Registriert
registered_at: Registriert am
rejected: Abgelehnt
reject: Ablehnen
rejected_at: Abgelehnt am
resigned: Ausgetreten
resign: Austreten
resigned_at: Ausgetreten am
create: &id-volunteers-create
<<: *id-availability
acceptance: *id-volunteer-acceptance
availability: Wann sind Sie für einen Freiwilligeneinsatz verfügbar?
edit: *id-volunteers-create
form: *id-volunteers-create
......@@ -302,7 +316,7 @@ de:
profile: Profil
role: Rolle
volunteer: &id-volunteer_attributes
<<: [*id-availability, *id-generic_keys]
<<: [*id-availability, *id-generic_keys, *id-volunteer-acceptance]
additional_email_addresses: Zusätzliche Mailadressen
additional_nationality: Zusätzliche Nationalität
additional_phone_numbers: Zusätzliche Telefonnummern
......@@ -346,6 +360,7 @@ de:
skills: Kompetenzen
sport: Sport
strengths: Stärken
take_more_assignments: Akzeptiere weitere Einsätze
teenagers: Jugendliche
training: Bildung
trial_period: Probezeitbericht erhalten
......
......@@ -119,6 +119,19 @@ en:
reserved: Reserved
resigned: Terminated
volunteer_applications: &id-volunteers-by-actions
acceptance: &id-volunteer-aceptance
acceptance_management: Acceptance Management
accepted: Accepted
accept: Accept
accepted_at: Accepted at
registered: Registered
registered_at: Registered at
rejected: Rejected
reject: Reject
rejected_at: Rejected at
resigned: Resigned
resign: Resign
resigned_at: Resigned at
create: &id-volunteers-create
<<: *id-availability
availability: What times are you available for voluntary engagement?
......@@ -189,7 +202,7 @@ en:
department: Department
email: Email
volunteer: &id-volunteer_attributes
<<: [*id-availability, *id-generic_keys]
<<: [*id-availability, *id-generic_keys, *id-volunteer-aceptance]
additional_email_addresses: Additional email addresses
additional_phone_numbers: Additional phone numbers
address: Address
......
class NewVolunteerStateFields < ActiveRecord::Migration[5.1]
def change
change_table :volunteers do |t|
t.integer :acceptance, default: 0
t.datetime :accepted_at
t.datetime :rejected_at
t.datetime :resigned_at
t.datetime :undecided_at
t.boolean :take_more_assignments, default: false
end
end
end
......@@ -443,6 +443,13 @@ ActiveRecord::Schema.define(version: 20170922120423) do
t.string "iban"
t.boolean "waive", default: false
t.boolean "external", default: false
t.datetime "registered"
t.integer "acceptance", default: 0
t.datetime "accepted_at"
t.datetime "rejected_at"
t.datetime "resigned_at"
t.datetime "undecided_at"
t.boolean "take_more_assignments", default: false
t.index ["deleted_at"], name: "index_volunteers_on_deleted_at"
t.index ["user_id"], name: "index_volunteers_on_user_id"
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment