2018-11-30 11:24:05 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-23 11:54:41 -04:00
|
|
|
class FederalSubject < ApplicationRecord
|
2019-07-19 22:40:56 -04:00
|
|
|
TIMEZONE_RE = /\A-?\d\d:\d\d:00\z/.freeze
|
|
|
|
|
2019-07-18 15:47:48 -04:00
|
|
|
##########
|
|
|
|
# Scopes #
|
|
|
|
##########
|
|
|
|
|
|
|
|
scope :order_by_display_name, lambda { |dir = :asc|
|
|
|
|
if I18n.locale == :ru
|
|
|
|
order(native_name: dir)
|
|
|
|
else
|
|
|
|
order(english_name: dir)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
################
|
|
|
|
# Associations #
|
|
|
|
################
|
|
|
|
|
2019-07-20 06:25:23 -04:00
|
|
|
has_one :regional_office
|
2018-12-06 17:49:50 -05:00
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
###############
|
|
|
|
# Validations #
|
|
|
|
###############
|
|
|
|
|
2019-07-20 09:17:37 -04:00
|
|
|
validates :english_name,
|
|
|
|
presence: true,
|
|
|
|
uniqueness: true,
|
|
|
|
length: { in: 1..255 }
|
2019-03-24 17:10:10 -04:00
|
|
|
|
2019-07-20 09:17:37 -04:00
|
|
|
validates :native_name,
|
|
|
|
presence: true,
|
|
|
|
uniqueness: true,
|
|
|
|
length: { in: 1..255 }
|
2019-03-24 17:37:36 -04:00
|
|
|
|
2019-07-22 05:14:14 -04:00
|
|
|
validates :centre,
|
|
|
|
presence: true,
|
|
|
|
length: { in: 1..255 }
|
|
|
|
|
2019-07-19 18:52:14 -04:00
|
|
|
validates :number,
|
|
|
|
presence: true,
|
|
|
|
uniqueness: true,
|
|
|
|
numericality: { only_integer: true, greater_than: 0 }
|
|
|
|
|
2019-07-19 22:40:56 -04:00
|
|
|
validates :timezone, presence: true, format: { with: TIMEZONE_RE }
|
|
|
|
|
2019-07-20 09:17:37 -04:00
|
|
|
validate :english_name_looks_realistic
|
|
|
|
validate :native_name_looks_realistic
|
2019-07-22 05:14:14 -04:00
|
|
|
validate :centre_looks_realistic
|
2019-07-20 09:17:37 -04:00
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
###########
|
|
|
|
# Methods #
|
|
|
|
###########
|
|
|
|
|
2019-03-24 17:37:36 -04:00
|
|
|
def display_name
|
2019-07-18 15:43:12 -04:00
|
|
|
if I18n.locale == :ru
|
|
|
|
native_name
|
|
|
|
else
|
|
|
|
english_name
|
|
|
|
end
|
2019-03-24 17:37:36 -04:00
|
|
|
end
|
2019-07-20 09:17:37 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def english_name_looks_realistic
|
|
|
|
return if english_name.blank?
|
|
|
|
|
2019-07-22 05:18:12 -04:00
|
|
|
errors.add :english_name, :leading_spaces if english_name =~ /\A\s+/
|
|
|
|
errors.add :english_name, :trailing_spaces if english_name =~ /\s+\z/
|
2019-07-20 09:17:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def native_name_looks_realistic
|
|
|
|
return if native_name.blank?
|
|
|
|
|
2019-07-22 05:18:12 -04:00
|
|
|
errors.add :native_name, :leading_spaces if native_name =~ /\A\s+/
|
|
|
|
errors.add :native_name, :trailing_spaces if native_name =~ /\s+\z/
|
2019-07-20 09:17:37 -04:00
|
|
|
end
|
2019-07-22 05:14:14 -04:00
|
|
|
|
|
|
|
def centre_looks_realistic
|
|
|
|
return if centre.blank?
|
|
|
|
|
2019-07-22 05:18:12 -04:00
|
|
|
errors.add :centre, :leading_spaces if centre =~ /\A\s+/
|
|
|
|
errors.add :centre, :trailing_spaces if centre =~ /\s+\z/
|
2019-07-22 05:14:14 -04:00
|
|
|
end
|
2018-11-30 11:24:05 -05:00
|
|
|
end
|