Sha256: ce5e890b16f15975c03615cb511eead25852878dd0332d0f15c2d190fbe62fa1

Contents?: true

Size: 1.18 KB

Versions: 13

Compression:

Stored size: 1.18 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(ActiveModel::Validations::LengthValidator)
      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

13 entries across 13 versions & 3 rubygems

Version Path
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/simple_form-2.0.4/lib/simple_form/components/maxlength.rb
simple_form-2.0.4 lib/simple_form/components/maxlength.rb
simple_form-2.0.3 lib/simple_form/components/maxlength.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/simple_form-2.0.2/lib/simple_form/components/maxlength.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/simple_form-2.0.2/lib/simple_form/components/maxlength.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/simple_form-2.0.2/lib/simple_form/components/maxlength.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/simple_form-2.0.2/lib/simple_form/components/maxlength.rb
sunrise-cms-0.3.0.rc vendor/bundle/ruby/1.9.1/gems/simple_form-2.0.2/lib/simple_form/components/maxlength.rb
simple_form-2.0.2 lib/simple_form/components/maxlength.rb
ehoch_simple_form-2.0.2.dev lib/simple_form/components/maxlength.rb
simple_form-2.0.1 lib/simple_form/components/maxlength.rb
simple_form-2.0.0 lib/simple_form/components/maxlength.rb
simple_form-2.0.0.rc lib/simple_form/components/maxlength.rb