Sha256: 34bbaf525b29e8dbc0f12f440d82168fd01e1369f3fcfd22541a241fec0d6618

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

module SimpleForm
  module Components
    # Needs to be enabled in order to do automatic lookups.
    module Maxlength
      def maxlength
        input_html_options[:maxlength] ||= maximum_length_from_validation || limit
        nil
      end

      private

      def maximum_length_from_validation
        maxlength = options[:maxlength]
        if maxlength.is_a?(String) || maxlength.is_a?(Integer)
          maxlength
        else
          length_validator = find_length_validator

          if length_validator && !has_tokenizer?(length_validator)
            length_validator.options[:is] || length_validator.options[:maximum]
          end
        end
      end

      def find_length_validator
        find_validator(:length)
      end

      def has_tokenizer?(length_validator)
        tokenizer = length_validator.options[:tokenizer]

        # TODO: Remove this check when we drop Rails 3.0 support
        if ActiveModel::Validations::LengthValidator.const_defined?(:DEFAULT_TOKENIZER)
          tokenizer && tokenizer != ActiveModel::Validations::LengthValidator::DEFAULT_TOKENIZER
        else
          tokenizer
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
simple_form-2.1.3 lib/simple_form/components/maxlength.rb
simple_form-2.1.2 lib/simple_form/components/maxlength.rb
simple_form-2.1.1 lib/simple_form/components/maxlength.rb
simple_form_awesome-2.5.0 lib/simple_form/components/maxlength.rb
simple_form_awesome-2.4.0 lib/simple_form/components/maxlength.rb
simple_form_awesome-2.3.0 lib/simple_form/components/maxlength.rb
simple_form_awesome-2.2.0 lib/simple_form/components/maxlength.rb
simple_form-2.1.0 lib/simple_form/components/maxlength.rb