Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default value conversion should support strings #325

Closed
catmando opened this issue Nov 16, 2020 · 0 comments
Closed

default value conversion should support strings #325

catmando opened this issue Nov 16, 2020 · 0 comments
Labels
bug Something isn't working good first issue Good for newcomers ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch

Comments

@catmando
Copy link
Contributor

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
@catmando catmando added bug Something isn't working good first issue Good for newcomers labels Nov 16, 2020
@catmando catmando modified the milestone: alpha1.5 Nov 16, 2020
@catmando catmando added this to To do in ALPHA to production via automation Nov 16, 2020
ALPHA to production automation moved this from To do to Done Nov 18, 2020
@catmando catmando added the ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch label Mar 15, 2021
@catmando catmando reopened this Mar 15, 2021
ALPHA to production automation moved this from Done to In progress Mar 15, 2021
ALPHA to production automation moved this from In progress to Done Mar 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers ready-to-release Internal Use Only: Has been fixed, specs passing and pushed to edge branch
Projects
Development

No branches or pull requests

1 participant