1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Removed deprecated :tokenizer in the length validator

This commit is contained in:
Rafael Mendonça França 2016-10-10 20:29:24 -03:00
parent 9de6457ab0
commit 6a78e0ecd6
No known key found for this signature in database
GPG key ID: FC23B6D0F1EEE948
4 changed files with 5 additions and 74 deletions

View file

@ -1,3 +1,7 @@
* Removed deprecated `:tokenizer` in the length validator.
*Rafael Mendonça França*
* Removed deprecated methods in `ActiveModel::Errors`.
`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.

View file

@ -6,7 +6,7 @@ module ActiveModel
MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze
CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze
RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]
RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :too_short, :too_long]
def initialize(options)
if range = (options.delete(:in) || options.delete(:within))
@ -18,27 +18,6 @@ module ActiveModel
options[:minimum] = 1
end
if options[:tokenizer]
ActiveSupport::Deprecation.warn(<<-EOS.strip_heredoc)
The `:tokenizer` option is deprecated, and will be removed in Rails 5.1.
You can achieve the same functionality by defining an instance method
with the value that you want to validate the length of. For example,
validates_length_of :essay, minimum: 100,
tokenizer: ->(str) { str.scan(/\w+/) }
should be written as
validates_length_of :words_in_essay, minimum: 100
private
def words_in_essay
essay.scan(/\w+/)
end
EOS
end
super
end
@ -59,7 +38,6 @@ module ActiveModel
end
def validate_each(record, attribute, value)
value = tokenize(record, value)
value_length = value.respond_to?(:length) ? value.length : value.to_s.length
errors_options = options.except(*RESERVED_OPTIONS)
@ -80,17 +58,6 @@ module ActiveModel
end
private
def tokenize(record, value)
tokenizer = options[:tokenizer]
if tokenizer && value.kind_of?(String)
if tokenizer.kind_of?(Proc)
tokenizer.call(value)
elsif record.respond_to?(tokenizer)
record.send(tokenizer, value)
end
end || value
end
def skip_nil_check?(key)
key == :maximum && options[:allow_nil].nil? && options[:allow_blank].nil?
end

View file

@ -318,42 +318,6 @@ class LengthValidationTest < ActiveModel::TestCase
assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"]
end
def test_validates_length_of_with_block
assert_deprecated do
Topic.validates_length_of(
:content,
minimum: 5,
too_short: "Your essay must be at least %{count} words.",
tokenizer: lambda { |str| str.scan(/\w+/) },
)
end
t = Topic.new(content: "this content should be long enough")
assert t.valid?
t.content = "not long enough"
assert t.invalid?
assert t.errors[:content].any?
assert_equal ["Your essay must be at least 5 words."], t.errors[:content]
end
def test_validates_length_of_with_symbol
assert_deprecated do
Topic.validates_length_of(
:content,
minimum: 5,
too_short: "Your essay must be at least %{count} words.",
tokenizer: :my_word_tokenizer,
)
end
t = Topic.new(content: "this content should be long enough")
assert t.valid?
t.content = "not long enough"
assert t.invalid?
assert t.errors[:content].any?
assert_equal ["Your essay must be at least 5 words."], t.errors[:content]
end
def test_validates_length_of_for_integer
Topic.validates_length_of(:approved, is: 4)

View file

@ -36,8 +36,4 @@ class Topic
def my_validation_with_arg(attr)
errors.add attr, "is missing" unless send(attr)
end
def my_word_tokenizer(str)
str.scan(/\w+/)
end
end