module Incline::Extensions::IntegerValue

Patches the ActiveRecord Integer type to be able to accept more numbers.

Specifically this will allow comma delimited numbers to be provided to active record models.

Public Instance Methods

cast_value(value) click to toggle source
# File lib/incline/extensions/integer_value.rb, line 20
def cast_value(value)
  begin
    case value
      when true then 1
      when false then 0
      when ::String
        # 1,234.56789
        if value =~ Incline::NumberFormats::WITH_DELIMITERS
          value = value.gsub(',', '')
        end
        if value =~ Incline::NumberFormats::WITHOUT_DELIMITERS
          value.to_i
        else
          nil
        end
      else
        if value.respond_to?(:to_i)
          value.to_i
        else
          nil
        end
    end
  rescue
    Incline::Log::warn "Failed to parse #{value.inspect}: #{$!.message}"
    nil
  end
end