Class: IntegerSettingValidator
- Inherits:
-
Object
- Object
- IntegerSettingValidator
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/validators/integer_setting_validator.rb
Instance Method Summary collapse
- #error_message ⇒ Object
-
#initialize(opts = {}) ⇒ IntegerSettingValidator
constructor
A new instance of IntegerSettingValidator.
- #valid_value?(val) ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ IntegerSettingValidator
Returns a new instance of IntegerSettingValidator.
6 7 8 9 10 11 |
# File 'lib/validators/integer_setting_validator.rb', line 6 def initialize(opts = {}) @opts = opts @opts[:min] = 0 unless @opts[:min].present? || @opts[:hidden] # set max closer to a long int @opts[:max] = 2_000_000_000 unless @opts[:max].present? || @opts[:hidden] end |
Instance Method Details
#error_message ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/validators/integer_setting_validator.rb', line 20 def if @opts[:min] && @opts[:max] I18n.t( "site_settings.errors.invalid_integer_min_max", min: number_with_delimiter(@opts[:min]), max: number_with_delimiter(@opts[:max]), ) elsif @opts[:min] I18n.t("site_settings.errors.invalid_integer_min", min: number_with_delimiter(@opts[:min])) elsif @opts[:max] I18n.t("site_settings.errors.invalid_integer_max", max: number_with_delimiter(@opts[:max])) else I18n.t("site_settings.errors.invalid_integer") end end |
#valid_value?(val) ⇒ Boolean
13 14 15 16 17 18 |
# File 'lib/validators/integer_setting_validator.rb', line 13 def valid_value?(val) return false if val.to_i.to_s != val.to_s return false if @opts[:min] && @opts[:min].to_i > (val.to_i) return false if @opts[:max] && @opts[:max].to_i < (val.to_i) true end |