2014-06-12 18:03:03 -04:00
|
|
|
class IntegerSettingValidator
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(opts = {})
|
2014-06-12 18:03:03 -04:00
|
|
|
@opts = opts
|
2017-04-22 02:36:02 -04:00
|
|
|
@opts[:min] = 0 unless @opts[:min].present? || @opts[:hidden]
|
|
|
|
@opts[:max] = 20000 unless @opts[:max].present? || @opts[:hidden]
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_value?(val)
|
|
|
|
return false if val.to_i.to_s != val.to_s
|
2017-07-27 21:20:09 -04:00
|
|
|
return false if @opts[:min] && @opts[:min].to_i > (val.to_i)
|
|
|
|
return false if @opts[:max] && @opts[:max].to_i < (val.to_i)
|
2014-06-12 18:03:03 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-06-18 10:49:21 -04:00
|
|
|
def error_message
|
2014-06-12 18:03:03 -04:00
|
|
|
if @opts[:min] && @opts[:max]
|
2017-07-27 21:20:09 -04:00
|
|
|
I18n.t('site_settings.errors.invalid_integer_min_max', min: @opts[:min], max: @opts[:max])
|
2014-06-12 18:03:03 -04:00
|
|
|
elsif @opts[:min]
|
2017-07-27 21:20:09 -04:00
|
|
|
I18n.t('site_settings.errors.invalid_integer_min', min: @opts[:min])
|
2014-06-12 18:03:03 -04:00
|
|
|
elsif @opts[:max]
|
2017-07-27 21:20:09 -04:00
|
|
|
I18n.t('site_settings.errors.invalid_integer_max', max: @opts[:max])
|
2014-06-12 18:03:03 -04:00
|
|
|
else
|
|
|
|
I18n.t('site_settings.errors.invalid_integer')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|