Skip to content

default value conversion should support strings #325

Closed
@catmando

Description

@catmando

It is legal in the rails schema to say

t.decimal "total_cost_of_failure", precision: 10, scale: 2, default: "0.0", null: false

for example. In this case default value of '0.0' should be converted to the number 0.0.

This applies to decimals, floats, integers, and booleans.

Currently the code just returns the default value without conversion (if it happens to be a string)

This is the patch needed to accomplish this:

module ReactiveRecord
  class Base
    class DummyValue < BasicObject
      def build_default_value_for_float
        return Float(0.0) unless @column_hash[:default]

        @column_hash[:default].to_f
      end

      alias build_default_value_for_decimal build_default_value_for_float

      def build_default_value_for_integer
        return Integer(0) unless @column_hash[:default]

        @column_hash[:default].to_i
      end

      alias build_default_value_for_bigint build_default_value_for_integer

      def build_default_value_for_boolean
        return false unless @column_hash[:default]

        ![false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"].include?(@column_hash[:default])
      end
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomersready-to-releaseInternal Use Only: Has been fixed, specs passing and pushed to edge branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions