Class: Cats::Core::DispatchPlan

Inherits:
ApplicationRecord show all
Defined in:
app/models/cats/core/dispatch_plan.rb

Constant Summary collapse

DRAFT =
"Draft".freeze
APPROVED =
"Approved".freeze
STATUSES =
[DRAFT, APPROVED].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



79
80
81
82
83
84
85
# File 'app/models/cats/core/dispatch_plan.rb', line 79

def self.ransackable_associations(_auth_object = nil)
  %w[
    dispatchable
    reported_by
    approved_by
  ]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



70
71
72
73
74
75
76
77
# File 'app/models/cats/core/dispatch_plan.rb', line 70

def self.ransackable_attributes(_auth_object = nil)
  %w[
    approved_by_id
    prepared_by_id
    status
    upstream
  ]
end

Instance Method Details

#approveObject

Raises:

  • (StandardError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/cats/core/dispatch_plan.rb', line 39

def approve
  raise(StandardError, "Dispatch plan already approved.") if status == APPROVED

  raise(StandardError, "Dispatch plan is empty.") if dispatch_plan_items.count.zero?

  # Raise error if the total dispatch plan quantity is not equal to the
  # dispatchable quantity
  if dispatchable.instance_of?(RhnRequest) && dispatchable.quantity != quantity
    raise(StandardError, "Requested quantity and plan quantity do not match.")
  end

  self.status = APPROVED
  dispatchable&.allocate if dispatchable.instance_of?(RhnRequest)
  save!
end

#quantityObject

A method which returns the total quantity in the dispatch plan based on dispatch plan item quantities.

Raises:

  • (StandardError)


57
58
59
60
61
# File 'app/models/cats/core/dispatch_plan.rb', line 57

def quantity
  raise(StandardError, "Dispatchable must be RHN request.") unless dispatchable.instance_of?(RhnRequest)

  UnitConversion.harmonized_total(dispatch_plan_items, dispatchable.unit)
end

#regionObject



63
64
65
66
67
68
# File 'app/models/cats/core/dispatch_plan.rb', line 63

def region
  return nil if dispatchable.instance_of?(RhnRequest)
  return dispatch_plan_items.first.source.parent.name if dispatchable.nil?

  dispatchable.region.name
end

#validate_dispatchableObject



30
31
32
33
34
35
36
37
# File 'app/models/cats/core/dispatch_plan.rb', line 30

def validate_dispatchable
  # Check if dispatchable is already completed and raise error if so.
  return unless dispatchable

  return if dispatchable.status == RhnRequest::APPROVED

  errors.add(:dispatchable, "is already completed.") if id.nil? && dispatchable.status == Dispatchable::COMPLETED
end