semester_process_volunteers.rb 1.83 KB
Newer Older
1 2 3
FactoryBot.define do
  factory :semester_process_volunteer do
    volunteer
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
4
    semester_process
5
    notes { ['some note', 'another note'] }
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
6 7 8

    transient do
      add_mission { false }
9
      mission_count { 0 }
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
10 11 12
      mission_type { :assignment }
      add_feedbacks { false }
      add_hours { false }
13 14 15 16 17 18 19
      add_mail { false }
    end

    trait :with_mail do
      transient do
        add_mail { true }
      end
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
20 21 22 23 24
    end

    trait :with_mission do
      transient do
        add_mission { true }
25
        mission_count { 1 }
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
      end
    end

    trait :with_feedbacks do
      transient do
        add_feedbacks { true }
      end
    end

    trait :with_hours do
      transient do
        add_hours { true }
      end
    end

41 42
    after(:create) do |spv, ev|
      ev.mission_count.times do
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
43
        create(:semester_process_volunteer_mission, semester_process_volunteer: spv,
44
          mission: create(ev.mission_type || :assignment, volunteer: spv.volunteer))
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
45 46
      end

47 48 49 50 51
      if (ev.add_feedbacks || ev.add_hours) && spv.semester_process_volunteer_missions.none?
        create(:semester_process_volunteer_mission, semester_process_volunteer: spv,
          mission: create(ev.mission_type || :assignment, volunteer: spv.volunteer))
      end

Jiri Strojil's avatar
Jiri Strojil committed
52 53 54 55 56
      if ev.add_mail
        create(:semester_process_mail, semester_process_volunteer: spv, kind: 'mail',
          sent_by: spv.semester_process.creator)
      end

57
      if ev.add_feedbacks
Jiri Strojil's avatar
Jiri Strojil committed
58
        spv.reload
59 60
        spv.semester_process_volunteer_missions.map do |sem_proc_mission|
          create(:semester_feedback, mission: sem_proc_mission.mission,
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
61 62 63 64
            semester_process_volunteer: spv)
        end
      end

65
      if ev.add_hours
66
        spv.semester_process_volunteer_missions.map do |sem_proc_mission|
Jiri Strojil's avatar
Jiri Strojil committed
67
          create(:hour, hourable: sem_proc_mission.mission)
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
68 69 70
        end
      end
    end
71 72
  end
end