summaryrefslogtreecommitdiff
path: root/spec/factories/iterations.rb
blob: bd61cd469afed982e3ed11053573be53692144e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

FactoryBot.define do
  sequence(:sequential_date) do |n|
    n.days.from_now
  end

  factory :iteration do
    title
    start_date { generate(:sequential_date) }
    due_date { generate(:sequential_date) }

    transient do
      project { nil }
      group { nil }
      project_id { nil }
      group_id { nil }
      resource_parent { nil }
    end

    trait :upcoming do
      state_enum { Iteration::STATE_ENUM_MAP[:upcoming] }
    end

    trait :started do
      state_enum { Iteration::STATE_ENUM_MAP[:started] }
    end

    trait :closed do
      state_enum { Iteration::STATE_ENUM_MAP[:closed] }
    end

    trait(:skip_future_date_validation) do
      after(:stub, :build) do |iteration|
        iteration.skip_future_date_validation = true
      end
    end

    trait(:skip_project_validation) do
      after(:stub, :build) do |iteration|
        iteration.skip_project_validation = true
      end
    end

    after(:build, :stub) do |iteration, evaluator|
      if evaluator.group
        iteration.group = evaluator.group
      elsif evaluator.group_id
        iteration.group_id = evaluator.group_id
      elsif evaluator.project
        iteration.project = evaluator.project
      elsif evaluator.project_id
        iteration.project_id = evaluator.project_id
      elsif evaluator.resource_parent
        id = evaluator.resource_parent.id
        evaluator.resource_parent.is_a?(Group) ? evaluator.group_id = id : evaluator.project_id = id
      else
        iteration.group = create(:group)
      end
    end

    factory :upcoming_iteration, traits: [:upcoming]
    factory :started_iteration, traits: [:started]
    factory :closed_iteration, traits: [:closed]
  end
end