Class: Country

Inherits:
ApplicationRecord show all
Includes:
RequiredUniqueName, Toggleable
Defined in:
app/models/country.rb

Constant Summary collapse

NAME_LIMIT =
50
SLUG_LIMIT =
50
SLUG_PATTERN =
/\A[a-z][-a-z_]*[a-z]\z/
PRIORITY_RANGE =
(1..999)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.entity_parametersObject



32
33
34
# File 'app/models/country.rb', line 32

def self.entity_parameters
  %i(visible priority name short_name locative slug)
end

.page_for_administrationObject



28
29
30
# File 'app/models/country.rb', line 28

def self.page_for_administration
  ordered_by_priority
end

Instance Method Details

#change_priority(delta) ⇒ Object

Parameters:

  • delta (Integer)


37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/country.rb', line 37

def change_priority(delta)
  new_priority = priority + delta
  criteria     = { 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.ordered_by_priority.map { |e| [e.id, e.priority] }.to_h
end