Class: Cats::Core::RoundPlan

Inherits:
ApplicationRecord show all
Includes:
Dispatchable
Defined in:
app/models/cats/core/round_plan.rb

Constant Summary collapse

NEEDS_APPROVED =
"Needs Approved".freeze
IN_PROGRESS =
"In Progress".freeze

Constants included from Dispatchable

Dispatchable::APPROVED, Dispatchable::COMPLETED, Dispatchable::DRAFT, Dispatchable::STATUSES

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/cats/core/round_plan.rb', line 85

def self.ransackable_associations(_auth_object = nil)
  %w[
    plan
    region
    round_plan_items
    round_rations
    beneficiary_round_plan_items
    round_plan_item_details
    round_beneficiaries
  ]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'app/models/cats/core/round_plan.rb', line 75

def self.ransackable_attributes(_auth_object = nil)
  %w[
    plan_id
    reference_no
    region_id
    rounds
    status
  ]
end

Instance Method Details

#approveObject

Raises:

  • (StandardError)


39
40
41
42
43
44
45
# File 'app/models/cats/core/round_plan.rb', line 39

def approve
  raise(StandardError, "Plan is not in draft state.") unless status == DRAFT

  raise(StandardError, "Empty plan cannot be approved.") if round_plan_items.count.zero?

  update!(status: APPROVED)
end

#in_progressObject

Raises:

  • (StandardError)


47
48
49
50
51
52
# File 'app/models/cats/core/round_plan.rb', line 47

def in_progress
  raise(StandardError, "Plan is not approved.") unless status == APPROVED || status == IN_PROGRESS

  self.status = IN_PROGRESS
  save!
end

#request_quantityObject



35
36
37
# File 'app/models/cats/core/round_plan.rb', line 35

def request_quantity
  nil
end

#request_referenceObject



31
32
33
# File 'app/models/cats/core/round_plan.rb', line 31

def request_reference
  reference_no
end

#validate_regionObject



69
70
71
72
73
# File 'app/models/cats/core/round_plan.rb', line 69

def validate_region
  return unless region

  errors.add(:region, "is not valid.") unless region.location_type == Location::REGION
end

#validate_roundsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/cats/core/round_plan.rb', line 54

def validate_rounds
  return unless rounds && plan

  errors.add(:rounds, "cannot be an empty list") if rounds.empty?

  existing_rounds = plan.round_plans.where(region: region).map(&:rounds).sum([])
  rounds.each do |round|
    if round > plan.rounds
      errors.add(:rounds, "cannot exceed the total number of rounds specified in the plan.")
      break
    end
  end
  errors.add(:rounds, "cannot be repeated.") unless existing_rounds - rounds == existing_rounds
end