Class: Cats::Core::Location

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

Constant Summary collapse

REGION =
"Region".freeze
ZONE =
"Zone".freeze
WOREDA =
"Woreda".freeze
FDP =
"Fdp".freeze
HUB =
"Hub".freeze
WAREHOUSE =
"Warehouse".freeze
LOCATION_TYPES =
[REGION, ZONE, WOREDA, FDP, HUB, WAREHOUSE].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_attributes(_auth_object = nil) ⇒ Object



57
58
59
# File 'app/models/cats/core/location.rb', line 57

def self.ransackable_attributes(_auth_object = nil)
  %w[code location_type name]
end

Instance Method Details

#fdp_storeObject



51
52
53
54
55
# File 'app/models/cats/core/location.rb', line 51

def fdp_store
  return nil unless location_type == FDP

  Store.find_by(code: "FDP-ST-#{code}")
end

#validate_location_parentObject



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
# File 'app/models/cats/core/location.rb', line 23

def validate_location_parent
  parents = {
    REGION => [],
    ZONE => [REGION],
    WOREDA => [REGION, ZONE],
    FDP => [REGION, ZONE, WOREDA],
    HUB => [REGION, ZONE, WOREDA, FDP],
    WAREHOUSE => [REGION, ZONE, WOREDA, FDP, HUB]
  }

  return if location_type.nil? || location_type.empty?

  return if location_type == REGION && parent.nil?

  # Check that parent is not nil for locations other than region
  errors.add(:location, "parent cannot be empty") if location_type != REGION && parent.nil?

  # At this point, we may return if parent.nil? is true
  # because the above statement already took care of that.
  return unless parent

  return if parents[location_type].include?(parent.location_type)

  # If we reach this far then it means that we have assigned a
  # wrong type of parent to our location.
  errors.add(:location, "cannot have #{parent.location_type} as parent")
end