Sha256: 2e78cb8efe19f6fd5f5b452a7c9a45ff5643b237cd9f29cf573f77cc50e3647a

Contents?: true

Size: 1.41 KB

Versions: 15

Compression:

Stored size: 1.41 KB

Contents

module SimpleForm
  module Components
    module MinMax
      def min_max
        if numeric_validator = find_numericality_validator
          validator_options = numeric_validator.options
          input_html_options[:min] ||= minimum_value(validator_options)
          input_html_options[:max] ||= maximum_value(validator_options)
        end
        nil
      end

      private

      def integer?
        input_type == :integer
      end

      def minimum_value(validator_options)
        if integer? && validator_options.key?(:greater_than)
          evaluate_numericality_validator_option(validator_options[:greater_than]) + 1
        else
          evaluate_numericality_validator_option(validator_options[:greater_than_or_equal_to])
        end
      end

      def maximum_value(validator_options)
        if integer? && validator_options.key?(:less_than)
          evaluate_numericality_validator_option(validator_options[:less_than]) - 1
        else
          evaluate_numericality_validator_option(validator_options[:less_than_or_equal_to])
        end
      end

      def find_numericality_validator
        find_validator(:numericality)
      end

      def evaluate_numericality_validator_option(option)
        if option.is_a?(Numeric)
          option
        elsif option.is_a?(Symbol)
          object.send(option)
        elsif option.respond_to?(:call)
          option.call(object)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
simple_form-3.0.4 lib/simple_form/components/min_max.rb
simple_form-2.1.3 lib/simple_form/components/min_max.rb
simple_form-2.1.2 lib/simple_form/components/min_max.rb
simple_form-3.0.3 lib/simple_form/components/min_max.rb
simple_form-3.0.2 lib/simple_form/components/min_max.rb
simple_form-3.0.1 lib/simple_form/components/min_max.rb
simple_form-2.1.1 lib/simple_form/components/min_max.rb
simple_form-3.0.0 lib/simple_form/components/min_max.rb
simple_form_awesome-2.5.0 lib/simple_form/components/min_max.rb
simple_form_awesome-2.4.0 lib/simple_form/components/min_max.rb
simple_form_awesome-2.3.0 lib/simple_form/components/min_max.rb
simple_form_awesome-2.2.0 lib/simple_form/components/min_max.rb
simple_form-3.0.0.rc lib/simple_form/components/min_max.rb
simple_form-2.1.0 lib/simple_form/components/min_max.rb
simple_form-3.0.0.beta1 lib/simple_form/components/min_max.rb