Sha256: 597eae90ce65d08e93b1563570d5abb88f3cc1018b75b01944063a89b983aef3

Contents?: true

Size: 1.44 KB

Versions: 12

Compression:

Stored size: 1.44 KB

Contents

module SimpleForm
  module Components
    module MinMax
      def min_max(wrapper_options = nil)
        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

12 entries across 12 versions & 3 rubygems

Version Path
simple_form-3.5.0 lib/simple_form/components/min_max.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/simple_form-3.4.0/lib/simple_form/components/min_max.rb
simple_form-3.4.0 lib/simple_form/components/min_max.rb
simple_form-3.3.1 lib/simple_form/components/min_max.rb
simple_form-3.3.0 lib/simple_form/components/min_max.rb
simple_form-3.2.1 lib/simple_form/components/min_max.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/simple_form-3.2.0/lib/simple_form/components/min_max.rb
simple_form-3.2.0 lib/simple_form/components/min_max.rb
simple_form-3.1.1 lib/simple_form/components/min_max.rb
simple_form-3.1.0 lib/simple_form/components/min_max.rb
simple_form-3.1.0.rc2 lib/simple_form/components/min_max.rb
simple_form-3.1.0.rc1 lib/simple_form/components/min_max.rb