Skip to content

Commit

Permalink
Removed deprecated :tokenizer in the length validator
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Oct 10, 2016
1 parent 9de6457 commit 6a78e0e
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 74 deletions.
4 changes: 4 additions & 0 deletions activemodel/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,7 @@
* Removed deprecated `:tokenizer` in the length validator.

*Rafael Mendonça França*

* Removed deprecated methods in `ActiveModel::Errors`. * Removed deprecated methods in `ActiveModel::Errors`.


`#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`. `#get`, `#set`, `[]=`, `add_on_empty` and `add_on_blank`.
Expand Down
35 changes: 1 addition & 34 deletions activemodel/lib/active_model/validations/length.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LengthValidator < EachValidator # :nodoc:
MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze
CHECKS = { is: :==, minimum: :>=, maximum: :<= }.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) def initialize(options)
if range = (options.delete(:in) || options.delete(:within)) if range = (options.delete(:in) || options.delete(:within))
Expand All @@ -18,27 +18,6 @@ def initialize(options)
options[:minimum] = 1 options[:minimum] = 1
end 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 super
end end


Expand All @@ -59,7 +38,6 @@ def check_validity!
end end


def validate_each(record, attribute, value) def validate_each(record, attribute, value)
value = tokenize(record, value)
value_length = value.respond_to?(:length) ? value.length : value.to_s.length value_length = value.respond_to?(:length) ? value.length : value.to_s.length
errors_options = options.except(*RESERVED_OPTIONS) errors_options = options.except(*RESERVED_OPTIONS)


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


private 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) def skip_nil_check?(key)
key == :maximum && options[:allow_nil].nil? && options[:allow_blank].nil? key == :maximum && options[:allow_nil].nil? && options[:allow_blank].nil?
end end
Expand Down
36 changes: 0 additions & 36 deletions activemodel/test/cases/validations/length_validation_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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"] assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"]
end 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 def test_validates_length_of_for_integer
Topic.validates_length_of(:approved, is: 4) Topic.validates_length_of(:approved, is: 4)


Expand Down
4 changes: 0 additions & 4 deletions activemodel/test/models/topic.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,8 +36,4 @@ def my_validation
def my_validation_with_arg(attr) def my_validation_with_arg(attr)
errors.add attr, "is missing" unless send(attr) errors.add attr, "is missing" unless send(attr)
end end

def my_word_tokenizer(str)
str.scan(/\w+/)
end
end end

0 comments on commit 6a78e0e

Please sign in to comment.