From 1c341eb7cbaeb52435f14c2595e391bfb2e5e38b Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Sun, 29 Mar 2015 16:34:01 -0600 Subject: Deprecate the `:tokenizer` option to `validates_length_of` As demonstrated by #19570, this option is severely limited, and satisfies an extremely specific use case. Realistically, there's not much reason for this option to exist. Its functionality can be trivially replicated with a normal Ruby method. Let's deprecate this option, in favor of the simpler solution. --- activemodel/lib/active_model/validations/length.rb | 38 +++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'activemodel/lib/active_model') diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index 23201b264a..060919fac3 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -1,3 +1,5 @@ +require "active_support/core_ext/string/strip" + module ActiveModel # == Active \Model Length Validator @@ -18,6 +20,27 @@ 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 be 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 @@ -88,8 +111,13 @@ module ActiveModel # validates_length_of :user_name, within: 6..20, too_long: 'pick a shorter name', too_short: 'pick a longer name' # validates_length_of :zip_code, minimum: 5, too_short: 'please enter at least 5 characters' # validates_length_of :smurf_leader, is: 4, message: "papa is spelled with 4 characters... don't play me." - # validates_length_of :essay, minimum: 100, too_short: 'Your essay must be at least 100 words.', - # tokenizer: ->(str) { str.scan(/\w+/) } + # validates_length_of :words_in_essay, minimum: 100, too_short: 'Your essay must be at least 100 words.' + # + # private + # + # def words_in_essay + # essay.scan(/\w+/) + # end # end # # Configuration options: @@ -112,12 +140,6 @@ module ActiveModel # * :message - The error message to use for a :minimum, # :maximum, or :is violation. An alias of the appropriate # too_long/too_short/wrong_length message. - # * :tokenizer - A method (as a symbol), proc or string to - # specify how to split up the attribute string. (e.g. - # tokenizer: :word_tokenizer to call the +word_tokenizer+ method - # or tokenizer: ->(str) { str.scan(/\w+/) } to count words as in - # above example). Defaults to ->(value) { value.split(//) } which - # counts individual characters. # # There is also a list of default options supported by every validator: # +:if+, +:unless+, +:on+ and +:strict+. -- cgit v1.2.3