group_assignment.rb 2.4 KB
Newer Older
1
class GroupAssignment < ApplicationRecord
2
  include VolunteersGroupAndTandemStateUpdate
3
  include GroupAssignmentCommon
4

5 6
  belongs_to :reactivated_by, class_name: 'User', inverse_of: 'reactivated_group_assignments',
    optional: true
7

8
  has_many :group_assignment_logs, dependent: :nullify
9 10
  has_many :hours, ->(object) { where(volunteer: object.volunteer) }, through: :group_offer

11 12
  delegate :title, to: :group_offer

13 14 15
  validates :volunteer, uniqueness: {
    scope: :group_offer,
    message: 'Diese/r Freiwillige ist schon im Gruppenangebot'
16
  }
17

18 19
  after_save :update_group_offer_search_field

20 21
  scope :running, (-> { no_end.have_start })

22 23 24 25 26 27 28 29 30
  def termination_verifiable?
    ended? && termination_submitted_by.present?
  end

  def verify_termination(user)
    update(termination_verified_by: user, termination_verified_at: Time.zone.now)
    create_log_of_self
  end

31
  def create_log_of_self(start_date = period_start, end_date = period_end)
Tugce Nur Tas's avatar
Tugce Nur Tas committed
32
    GroupAssignmentLog.create(
33 34 35 36 37 38 39 40 41
      attributes.except(
        'id', 'created_at', 'updated_at', 'active',
        'pdf_file_name', 'pdf_content_type', 'pdf_file_size', 'pdf_updated_at', 'generate_pdf'
      ).merge(
        title: group_offer.title,
        group_assignment_id: id,
        period_start: start_date,
        period_end: end_date
      )
42
    )
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
43 44
  end

45
  def hours_since_last_submitted
46
    hours.since_last_submitted(submitted_at)
47 48
  end

49 50 51 52
  def polymorph_url_object
    group_offer
  end

53 54 55 56 57 58 59 60
  def assignment?
    false
  end

  def group_assignment?
    true
  end

61 62 63 64 65 66
  def import_terminate(user, date)
    update(termination_submitted_at: date, termination_submitted_by: user,
      termination_verified_at: date, termination_verified_by: user, period_end_set_by: user,
      period_end: period_end || date)
  end

Emily Wangler's avatar
Emily Wangler committed
67 68 69 70
  def default_values
    self.agreement_text ||= default_agreement_text
  end

71 72
  private

73 74 75
  def update_group_offer_search_field
    group_offer.update_search_volunteers
  end
Emily Wangler's avatar
Emily Wangler committed
76 77 78 79 80 81 82 83 84 85 86 87 88

  def default_agreement_text
    <<~HEREDOC
      Freiwillige beachten folgende Grundsätze während ihres Einsatzes in der AOZ:
      * Verhaltenskodex für Freiwillige
      * Rechte und Pflichten für Freiwillige
      * AOZ Leitlinien Praktische Integrationsarbeit

      Allenfalls auch
      * Verpflichtungserklärung zum Schutz der unbegleiteten minderjährigen Asylsuchenden (MNA)
      * Niederschwellige Gratis-Deutschkurse: Informationen für freiwillige Kursleitende
    HEREDOC
  end
89
end