2011-08-30 23:51:53 +00:00
|
|
|
module SimpleForm
|
2011-12-04 12:14:39 +00:00
|
|
|
module Components
|
2011-12-04 14:49:57 +00:00
|
|
|
# Needs to be enabled in order to do automatic lookups.
|
2011-12-04 12:14:39 +00:00
|
|
|
module Maxlength
|
2014-03-11 22:41:25 +00:00
|
|
|
def maxlength(wrapper_options = nil)
|
2011-12-04 12:29:59 +00:00
|
|
|
input_html_options[:maxlength] ||= maximum_length_from_validation || limit
|
2011-12-04 12:14:39 +00:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2011-09-02 15:48:20 +00:00
|
|
|
private
|
|
|
|
|
2011-08-30 23:51:53 +00:00
|
|
|
def maximum_length_from_validation
|
2012-01-11 20:12:49 +00:00
|
|
|
maxlength = options[:maxlength]
|
|
|
|
if maxlength.is_a?(String) || maxlength.is_a?(Integer)
|
|
|
|
maxlength
|
|
|
|
else
|
2012-01-05 10:40:48 +00:00
|
|
|
length_validator = find_length_validator
|
2016-07-01 17:55:15 +00:00
|
|
|
maximum_length_value_from(length_validator)
|
2011-12-04 12:14:39 +00:00
|
|
|
end
|
2011-08-30 23:51:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_length_validator
|
2012-10-03 03:05:47 +00:00
|
|
|
find_validator(:length)
|
2011-08-30 23:51:53 +00:00
|
|
|
end
|
2012-01-11 20:57:51 +00:00
|
|
|
|
|
|
|
def has_tokenizer?(length_validator)
|
2012-12-14 19:36:54 +00:00
|
|
|
length_validator.options[:tokenizer]
|
2012-01-11 20:57:51 +00:00
|
|
|
end
|
2016-07-01 17:55:15 +00:00
|
|
|
|
|
|
|
# Use validation with tokenizer if version of Rails is less than 5,
|
|
|
|
# if not validate without the tokenizer, if version is greater than Rails 4.
|
|
|
|
if ActionPack::VERSION::STRING < '5'
|
|
|
|
def maximum_length_value_from(length_validator)
|
|
|
|
if length_validator && !has_tokenizer?(length_validator)
|
|
|
|
length_validator.options[:is] || length_validator.options[:maximum]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif ActionPack::VERSION::STRING >= '5'
|
|
|
|
def maximum_length_value_from(length_validator)
|
|
|
|
if length_validator
|
|
|
|
length_validator.options[:is] || length_validator.options[:maximum]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-30 23:51:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|