Skip to content

Commit 6a78e0e

Browse files
committed
Removed deprecated :tokenizer in the length validator
1 parent 9de6457 commit 6a78e0e

File tree

4 files changed

+5
-74
lines changed

4 files changed

+5
-74
lines changed

activemodel/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Removed deprecated `:tokenizer` in the length validator.
2+
3+
*Rafael Mendonça França*
4+
15
* Removed deprecated methods in `ActiveModel::Errors`.
26

37
`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.

activemodel/lib/active_model/validations/length.rb

+1-34
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class LengthValidator < EachValidator # :nodoc:
66
MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze
77
CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze
88

9-
RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]
9+
RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :too_short, :too_long]
1010

1111
def initialize(options)
1212
if range = (options.delete(:in) || options.delete(:within))
@@ -18,27 +18,6 @@ def initialize(options)
1818
options[:minimum] = 1
1919
end
2020

21-
if options[:tokenizer]
22-
ActiveSupport::Deprecation.warn(<<-EOS.strip_heredoc)
23-
The `:tokenizer` option is deprecated, and will be removed in Rails 5.1.
24-
You can achieve the same functionality by defining an instance method
25-
with the value that you want to validate the length of. For example,
26-
27-
validates_length_of :essay, minimum: 100,
28-
tokenizer: ->(str) { str.scan(/\w+/) }
29-
30-
should be written as
31-
32-
validates_length_of :words_in_essay, minimum: 100
33-
34-
private
35-
36-
def words_in_essay
37-
essay.scan(/\w+/)
38-
end
39-
EOS
40-
end
41-
4221
super
4322
end
4423

@@ -59,7 +38,6 @@ def check_validity!
5938
end
6039

6140
def validate_each(record, attribute, value)
62-
value = tokenize(record, value)
6341
value_length = value.respond_to?(:length) ? value.length : value.to_s.length
6442
errors_options = options.except(*RESERVED_OPTIONS)
6543

@@ -80,17 +58,6 @@ def validate_each(record, attribute, value)
8058
end
8159

8260
private
83-
def tokenize(record, value)
84-
tokenizer = options[:tokenizer]
85-
if tokenizer && value.kind_of?(String)
86-
if tokenizer.kind_of?(Proc)
87-
tokenizer.call(value)
88-
elsif record.respond_to?(tokenizer)
89-
record.send(tokenizer, value)
90-
end
91-
end || value
92-
end
93-
9461
def skip_nil_check?(key)
9562
key == :maximum && options[:allow_nil].nil? && options[:allow_blank].nil?
9663
end

activemodel/test/cases/validations/length_validation_test.rb

-36
Original file line numberDiff line numberDiff line change
@@ -318,42 +318,6 @@ def test_validates_length_of_using_is_utf8
318318
assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"]
319319
end
320320

321-
def test_validates_length_of_with_block
322-
assert_deprecated do
323-
Topic.validates_length_of(
324-
:content,
325-
minimum: 5,
326-
too_short: "Your essay must be at least %{count} words.",
327-
tokenizer: lambda { |str| str.scan(/\w+/) },
328-
)
329-
end
330-
t = Topic.new(content: "this content should be long enough")
331-
assert t.valid?
332-
333-
t.content = "not long enough"
334-
assert t.invalid?
335-
assert t.errors[:content].any?
336-
assert_equal ["Your essay must be at least 5 words."], t.errors[:content]
337-
end
338-
339-
def test_validates_length_of_with_symbol
340-
assert_deprecated do
341-
Topic.validates_length_of(
342-
:content,
343-
minimum: 5,
344-
too_short: "Your essay must be at least %{count} words.",
345-
tokenizer: :my_word_tokenizer,
346-
)
347-
end
348-
t = Topic.new(content: "this content should be long enough")
349-
assert t.valid?
350-
351-
t.content = "not long enough"
352-
assert t.invalid?
353-
assert t.errors[:content].any?
354-
assert_equal ["Your essay must be at least 5 words."], t.errors[:content]
355-
end
356-
357321
def test_validates_length_of_for_integer
358322
Topic.validates_length_of(:approved, is: 4)
359323

activemodel/test/models/topic.rb

-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,4 @@ def my_validation
3636
def my_validation_with_arg(attr)
3737
errors.add attr, "is missing" unless send(attr)
3838
end
39-
40-
def my_word_tokenizer(str)
41-
str.scan(/\w+/)
42-
end
4339
end

0 commit comments

Comments
 (0)