@@ -14,7 +14,7 @@ module Limitable
14
14
class << self
15
15
def included ( klass )
16
16
safe_column_names ( klass ) . each do |column_name |
17
- add_limit_validation klass , column_name
17
+ attach_limit_validator_if_needed klass , column_name
18
18
end
19
19
end
20
20
@@ -26,50 +26,47 @@ def safe_column_names(klass)
26
26
[ ]
27
27
end
28
28
29
- def add_limit_validation ( klass , column_name )
29
+ def attach_limit_validator_if_needed ( klass , column_name )
30
30
column = klass . column_for_attribute column_name
31
31
limit = column . sql_type_metadata . limit
32
32
return if limit . blank?
33
33
34
34
case column . type
35
35
when :integer
36
- add_integer_limit_validation klass , column_name , limit
36
+ klass . validate ( & build_integer_limit_validator ( column_name , limit ) )
37
37
when :string , :text
38
- add_string_limit_validation klass , column_name , limit
38
+ klass . validate ( & build_string_limit_validator ( column_name , limit ) )
39
39
end
40
40
end
41
41
42
- def add_integer_limit_validation ( klass , column_name , limit )
42
+ def build_integer_limit_validator ( column_name , limit )
43
43
min , max = integer_limit_range limit
44
- integer_type_normalizer = method :integer_type_normalizer
45
- klass . validate do
46
- value = integer_type_normalizer . call klass , column_name , self [ column_name ]
47
- next unless value . is_a? ( Integer )
44
+ lambda do
45
+ value = begin
46
+ self . class . type_for_attribute ( column_name ) . serialize self [ column_name ]
47
+ rescue ActiveModel ::RangeError => e
48
+ e . message . match ( /(?<number>\d +) is out of range/ ) [ :number ] . to_i
49
+ end
50
+ next unless value . is_a? Integer
48
51
49
52
errors . add column_name , I18n . t ( 'errors.messages.greater_than_or_equal_to' , count : min ) if value < min
50
53
errors . add column_name , I18n . t ( 'errors.messages.less_than_or_equal_to' , count : max ) if value > max
51
54
end
52
55
end
53
56
54
- def integer_limit_range ( limit )
55
- max = ( 1 << ( ( limit * 8 ) - 1 ) ) - 1
56
- min = -max
57
- [ min , max ]
58
- end
59
-
60
- def integer_type_normalizer ( klass , column_name , value )
61
- klass . type_for_attribute ( column_name ) . serialize value
62
- rescue ActiveModel ::RangeError => e
63
- e . message . match ( /(?<number>\d +) is out of range/ ) [ :number ] . to_i
64
- end
65
-
66
- def add_string_limit_validation ( klass , column_name , limit )
67
- klass . validate do
68
- value = klass . type_for_attribute ( column_name ) . serialize self [ column_name ]
57
+ def build_string_limit_validator ( column_name , limit )
58
+ lambda do
59
+ value = self . class . type_for_attribute ( column_name ) . serialize self [ column_name ]
69
60
next unless value . is_a? ( String ) && value . bytesize > limit
70
61
71
62
errors . add column_name , I18n . t ( 'errors.messages.too_long.other' , count : limit )
72
63
end
73
64
end
65
+
66
+ def integer_limit_range ( limit )
67
+ max = ( 1 << ( ( limit * 8 ) - 1 ) ) - 1
68
+ min = -max
69
+ [ min , max ]
70
+ end
74
71
end
75
72
end
0 commit comments