Permalink
Browse files

Removed deprecated :tokenizer in the length validator

  • Loading branch information...
rafaelfranca committed Oct 10, 2016
1 parent 9de6457 commit 6a78e0ecd6122a6b1be9a95e6c4e21e10e429513
View
@@ -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`.
@@ -6,7 +6,7 @@ class LengthValidator < EachValidator # :nodoc:
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 @@ def initialize(options)
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 @@ def check_validity!
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 @@ def validate_each(record, attribute, value)
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
@@ -318,42 +318,6 @@ def test_validates_length_of_using_is_utf8
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)
@@ -36,8 +36,4 @@ def my_validation
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

0 comments on commit 6a78e0e

Please sign in to comment.