From 63e1ba6e5852ca78390c901791ea7fd824a9caf3 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 1 Oct 2019 01:08:18 +0500 Subject: [PATCH] Add method ApplicationEachValidator::Validation#str_value --- app/validators/application_each_validator.rb | 4 ++++ app/validators/codename_validator.rb | 6 +++--- app/validators/good_big_text_validator.rb | 4 ++-- app/validators/good_small_text_validator.rb | 4 ++-- app/validators/good_text_validator.rb | 2 +- app/validators/timezone_validator.rb | 2 +- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/validators/application_each_validator.rb b/app/validators/application_each_validator.rb index 0674309..a0d07eb 100644 --- a/app/validators/application_each_validator.rb +++ b/app/validators/application_each_validator.rb @@ -35,5 +35,9 @@ class ApplicationEachValidator < ActiveModel::EachValidator error(*args) throw :stop_validating end + + def str_value + @str_value ||= String(value).dup.freeze + end end end diff --git a/app/validators/codename_validator.rb b/app/validators/codename_validator.rb index a2fabf4..6124ff5 100644 --- a/app/validators/codename_validator.rb +++ b/app/validators/codename_validator.rb @@ -8,10 +8,10 @@ class CodenameValidator < ApplicationEachValidator MAX = 36 def perform - error! :blank if value.to_s.blank? + error! :blank if str_value.blank? error! :codename unless CODENAME_RE.match? value - error! :too_short, count: MIN if value.to_s.length < MIN - error! :too_long, count: MAX if value.to_s.length > MAX + error! :too_short, count: MIN if str_value.length < MIN + error! :too_long, count: MAX if str_value.length > MAX end end end diff --git a/app/validators/good_big_text_validator.rb b/app/validators/good_big_text_validator.rb index 25fc511..bbc8f14 100644 --- a/app/validators/good_big_text_validator.rb +++ b/app/validators/good_big_text_validator.rb @@ -7,8 +7,8 @@ class GoodBigTextValidator < GoodTextValidator def perform super - error :too_short, count: MIN if value.to_s.length < MIN - error :too_long, count: MAX if value.to_s.length > MAX + error :too_short, count: MIN if str_value.length < MIN + error :too_long, count: MAX if str_value.length > MAX end end end diff --git a/app/validators/good_small_text_validator.rb b/app/validators/good_small_text_validator.rb index 05e3503..fc12304 100644 --- a/app/validators/good_small_text_validator.rb +++ b/app/validators/good_small_text_validator.rb @@ -7,8 +7,8 @@ class GoodSmallTextValidator < GoodTextValidator def perform super - error :too_short, count: MIN if value.to_s.length < MIN - error :too_long, count: MAX if value.to_s.length > MAX + error :too_short, count: MIN if str_value.length < MIN + error :too_long, count: MAX if str_value.length > MAX end end end diff --git a/app/validators/good_text_validator.rb b/app/validators/good_text_validator.rb index 15595e8..9eedddc 100644 --- a/app/validators/good_text_validator.rb +++ b/app/validators/good_text_validator.rb @@ -5,7 +5,7 @@ class GoodTextValidator < ApplicationEachValidator GOOD_TEXT_RE = /\A[^\s](.*[^\s])?\z/.freeze def perform - error :blank if value.to_s.blank? + error :blank if str_value.blank? error :good_text unless GOOD_TEXT_RE.match? value end end diff --git a/app/validators/timezone_validator.rb b/app/validators/timezone_validator.rb index 22c80f1..e299a94 100644 --- a/app/validators/timezone_validator.rb +++ b/app/validators/timezone_validator.rb @@ -5,7 +5,7 @@ class TimezoneValidator < ApplicationEachValidator TIMEZONE_RE = /\A-?\d\d:\d\d:00\z/.freeze def perform - error! :blank if value.to_s.blank? + error! :blank if str_value.blank? error! :timezone unless TIMEZONE_RE.match? value end end