Class: Region

Inherits:
ApplicationRecord show all
Includes:
Toggleable
Defined in:
app/models/region.rb

Constant Summary collapse

SLUG_PATTERN =
/\A[a-z0-9](?:[a-z0-9\-]{0,61}[a-z0-9])?\z/
PER_PAGE =
20
NAME_LIMIT =
70
SLUG_LIMIT =
63
PRIORITY_RANGE =
(1..32767)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.creation_parametersObject



49
50
51
# File 'app/models/region.rb', line 49

def self.creation_parameters
  entity_parameters + %i(country_id parent_id)
end

.entity_parametersObject



45
46
47
# File 'app/models/region.rb', line 45

def self.entity_parameters
  %i(name short_name locative slug visible image header_image latitude longitude)
end

.page_for_administration(page = 1) ⇒ Object

Parameters:

  • page (Integer) (defaults to: 1)


41
42
43
# File 'app/models/region.rb', line 41

def self.page_for_administration(page = 1)
  ordered_by_name.page(page).per(PER_PAGE)
end

Instance Method Details

#branch_idsArray<Integer>

Returns:

  • (Array<Integer>)


70
71
72
# File 'app/models/region.rb', line 70

def branch_ids
  parents_cache.split(',').map(&:to_i).reject { |i| i < 1 }.uniq + [id]
end

#branch_nameObject



88
89
90
91
# File 'app/models/region.rb', line 88

def branch_name
  return short_name if parents.blank?
  "#{parents.map(&:short_name).join('/')}/#{short_name}"
end

#cache_children!Object



99
100
101
102
103
104
105
# File 'app/models/region.rb', line 99

def cache_children!
  child_regions.order('id asc').each do |child|
    self.children_cache += [child.id] + child.children_cache
  end
  save!
  parent.cache_children! unless parent.nil?
end

#cache_parents!Object



93
94
95
96
97
# File 'app/models/region.rb', line 93

def cache_parents!
  return if parent.nil?
  self.parents_cache = "#{parent.parents_cache},#{parent_id}".gsub(/\A,/, '')
  save!
end

#change_priority(delta) ⇒ Object

Parameters:

  • delta (Integer)


108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/region.rb', line 108

def change_priority(delta)
  new_priority = priority + delta
  criteria     = { country_id: county_id, parent_id: parent_id, priority: new_priority }
  adjacent     = self.class.find_by(criteria)
  if adjacent.is_a?(self.class) && (adjacent.id != id)
    adjacent.update!(priority: priority)
  end
  self.update(priority: new_priority)

  self.class.for_tree(country_id, parent_id).map { |e| [e.id, e.priority] }.to_h
end

#depthObject



79
80
81
# File 'app/models/region.rb', line 79

def depth
  parent_ids.count
end

#editable_by?(user) ⇒ Boolean

Parameters:

  • user (User)

Returns:

  • (Boolean)


54
55
56
57
58
# File 'app/models/region.rb', line 54

def editable_by?(user)
  chief   = UserPrivilege.user_has_privilege?(user, :chief_region_manager)
  manager = UserPrivilege.user_has_privilege?(user, :region_manager, self)
  chief || manager
end

#long_nameObject



83
84
85
86
# File 'app/models/region.rb', line 83

def long_name
  return name if parents.blank?
  "#{parents.map(&:name).join('/')}/#{name}"
end

#parent_idsObject



65
66
67
# File 'app/models/region.rb', line 65

def parent_ids
  parents_cache.split(',').compact
end

#parentsObject



60
61
62
63
# File 'app/models/region.rb', line 60

def parents
  return [] if parents_cache.blank?
  Region.where(id: parent_ids).order('id asc')
end

#subbranch_idsArray<Integer>

Returns:

  • (Array<Integer>)


75
76
77
# File 'app/models/region.rb', line 75

def subbranch_ids
  [id] + children_cache
end