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/
SLUG_PATTERN_HTML =
'^[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$'
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



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

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

.entity_parametersObject



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

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)


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

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>)


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

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

#branch_nameObject



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

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

#cache_children!Object



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

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



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

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)


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

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



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

def depth
  parent_ids.count
end

#editable_by?(user) ⇒ Boolean

Parameters:

  • user (User)

Returns:

  • (Boolean)


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

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



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

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

#parent_idsObject



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

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

#parentsObject



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

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

#subbranch_idsArray<Integer>

Returns:

  • (Array<Integer>)


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

def subbranch_ids
  [id] + children_cache
end