Class: TimezoneValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/timezone_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.error_message(value) ⇒ Object



10
11
12
# File 'lib/validators/timezone_validator.rb', line 10

def self.error_message(value)
  I18n.t("errors.messages.invalid_timezone", tz: value)
end

.valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
# File 'lib/validators/timezone_validator.rb', line 4

def self.valid?(value)
  ok = ActiveSupport::TimeZone[value].present?
  Rails.logger.warn("Invalid timezone '#{value}' detected!") if !ok
  ok
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



14
15
16
17
# File 'lib/validators/timezone_validator.rb', line 14

def validate_each(record, attribute, value)
  return if value.blank? || TimezoneValidator.valid?(value)
  record.errors.add(attribute, :timezone, message: TimezoneValidator.error_message(value))
end