Skip to content

Commit 23c2dd6

Browse files
committed
fixes regression from #192 and adds specs
1 parent f40eeab commit 23c2dd6

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

ruby/hyper-model/lib/reactive_record/active_record/class_methods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ def server_method(name, default: nil)
342342
def define_attribute_methods
343343
columns_hash.each do |name, column_hash|
344344
next if name == primary_key
345+
column_hash[:serialized?] = ReactiveRecord::Base.serialized?[self][name]
345346
define_method(name) { @backing_record.get_attr_value(name, nil) }
346347
define_method("#{name}!") { @backing_record.get_attr_value(name, true) }
347348
define_method("#{name}=") { |val| @backing_record.set_attr_value(name, val) }

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/dummy_value.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ def initialize(column_hash = nil)
2929
column_type = Base.column_type(@column_hash) || 'nil'
3030
default_value_method = "build_default_value_for_#{column_type}"
3131
@object = __send__ default_value_method
32-
rescue ::Exception => e
32+
rescue ::Exception
3333
end
3434

3535
def build_default_value_for_nil
36-
@column_hash.key?(:default) ? @column_hash[:default] : nil
36+
@column_hash[:default] || nil
3737
end
3838

3939
def build_default_value_for_datetime
@@ -56,23 +56,24 @@ def build_default_value_for_date
5656
end
5757

5858
def build_default_value_for_boolean
59-
@column_hash.key?(:default) ? @column_hash[:default] : false
59+
@column_hash[:default] || false
6060
end
6161

6262
def build_default_value_for_float
63-
@column_hash.key?(:default) ? @column_hash[:default] : Float(0.0)
63+
@column_hash[:default] || Float(0.0)
6464
end
6565

6666
alias build_default_value_for_decimal build_default_value_for_float
6767

6868
def build_default_value_for_integer
69-
@column_hash.key?(:default) ? @column_hash[:default] : Integer(0)
69+
@column_hash[:default] || Integer(0)
7070
end
7171

7272
alias build_default_value_for_bigint build_default_value_for_integer
7373

7474
def build_default_value_for_string
75-
@column_hash.key?(:default) ? @column_hash[:default] : ''
75+
return @column_hash[:default] if @column_hash[:serialized?]
76+
@column_hash[:default] || ''
7677
end
7778

7879
alias build_default_value_for_text build_default_value_for_string

ruby/hyper-model/spec/batch1/column_types/column_type_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ class DefaultTest < ActiveRecord::Base
145145
check_errors
146146
end
147147

148+
it 'will not override a default of nil if the attribute is serialized' do
149+
isomorphic do
150+
TypeTest.serialize :string
151+
TypeTest.serialize :text
152+
end
153+
[:string, :text].each do |attr|
154+
expect_evaluate_ruby("TypeTest.find(1).#{attr}.class").to eq('NilClass')
155+
end
156+
end
157+
148158
it 'while loading the dummy value delegates the correct type with operators etc' do
149159
t = Time.parse('1/2/2003')
150160
TypeTest.create(
@@ -293,6 +303,15 @@ class DefaultTest < ActiveRecord::Base
293303
check_errors
294304
end
295305

306+
it 'uses the default value if even if the attribute is serialized' do
307+
isomorphic do
308+
DefaultTest.serialize :string
309+
end
310+
expect_evaluate_ruby do
311+
DefaultTest.find(1).string
312+
end.to eq("I'm a string!")
313+
end
314+
296315
it 'uses the default value if specified when initializing a new record' do
297316
expect_evaluate_ruby do
298317
DefaultTest.new.string

0 commit comments

Comments
 (0)