Class: Cats::Core::TransportRequisition

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#approve(user) ⇒ Object

Raises:

  • (StandardError)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/cats/core/transport_requisition.rb', line 30

def approve(user)
  raise(StandardError, "Transport requisition is already approved.") if status == APPROVED

  begin
    self.status = APPROVED
    self.approved_by = user
    save!
    self
  rescue ActiveRecord::RecordInvalid => e
    error = e.record.errors.full_messages_for(:status)[0]
    raise(StandardError, error)
  end
end

#approved?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/cats/core/transport_requisition.rb', line 44

def approved?
  status == APPROVED
end

#quantityObject



25
26
27
28
# File 'app/models/cats/core/transport_requisition.rb', line 25

def quantity
  quantities = transport_requisition_items.map { |item| UnitConversion.convert(item.unit, unit, item.quantity) }
  quantities.sum
end

#validate_statusObject



48
49
50
51
52
53
54
55
56
# File 'app/models/cats/core/transport_requisition.rb', line 48

def validate_status
  return unless status

  return if status == DRAFT

  return if transport_requisition_items.count.positive?

  errors.add(:status, 'cannot be set to "APPROVED" because the requisition has no items.') if status == APPROVED
end