Skip to content

Commit

Permalink
closes #325
Browse files Browse the repository at this point in the history
  • Loading branch information
catmando committed Nov 16, 2020
1 parent 9e324ad commit 215836b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ def build_default_value_for_date
end
end

FALSY_VALUES = [false, nil, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"]

def build_default_value_for_boolean
@column_hash[:default] || false
!FALSY_VALUES.include?(@column_hash[:default])
end

def build_default_value_for_float
@column_hash[:default] || Float(0.0)
@column_hash[:default]&.to_f || Float(0.0)
end

alias build_default_value_for_decimal build_default_value_for_float

def build_default_value_for_integer
@column_hash[:default] || Integer(0)
@column_hash[:default]&.to_i || Integer(0)
end

alias build_default_value_for_bigint build_default_value_for_integer
Expand Down
12 changes: 10 additions & 2 deletions ruby/hyper-model/spec/batch1/column_types/column_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,16 @@ class DefaultTest < ActiveRecord::Base
r = DefaultTest.create(string: "no no no")
expect_evaluate_ruby do
t = DefaultTest.find(1)
[t.string, t.date, t.datetime]
end.to eq(["I'm a string!", r.date.as_json, Timex.new(r.datetime.localtime).as_json])
[
t.string, t.date, t.datetime, t.integer_from_string, t.integer_from_int,
t.float_from_string, t.float_from_float,
t.boolean_from_falsy_string, t.boolean_from_truthy_string, t.boolean_from_falsy_value
]
end.to eq([
"I'm a string!", r.date.as_json, Timex.new(r.datetime.localtime).as_json, 99, 98,
0.02, 0.01,
false, true, false
])
check_errors
end

Expand Down
13 changes: 10 additions & 3 deletions ruby/hyper-model/spec/test_app/app/models/public/default_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
class DefaultTest < ActiveRecord::Base
def self.build_tables
connection.create_table :default_tests, force: true do |t|
t.string :string, default: "I'm a string!"
t.date :date, default: Date.today
t.datetime :datetime, default: Time.now
t.string :string, default: "I'm a string!"
t.date :date, default: Date.today
t.datetime :datetime, default: Time.now
t.integer :integer_from_string, default: "99"
t.integer :integer_from_int, default: 98
t.float :float_from_string, default: "0.02"
t.float :float_from_float, default: 0.01
t.boolean :boolean_from_falsy_string, default: "OFF"
t.boolean :boolean_from_truthy_string, default: "something-else"
t.boolean :boolean_from_falsy_value, default: false
end
end
end

0 comments on commit 215836b

Please sign in to comment.