Skip to content

Commit

Permalink
Use default value for field is array and value is nil
Browse files Browse the repository at this point in the history
Fix: #80
  • Loading branch information
TayKangSheng authored and byroot committed Aug 17, 2023
1 parent 0dae8a9 commit 1b3fcb9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/active_record/typed_store/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def initialize(name, type, options={})

@accessor = options.fetch(:accessor, true)
@name = name
@array = options.fetch(:array, false)
if options.key?(:default)
@default = extract_default(options[:default])
end
@null = options.fetch(:null, true)
@blank = options.fetch(:blank, true)
@array = options.fetch(:array, false)
end

def has_default?
Expand All @@ -27,6 +27,8 @@ def cast(value)
casted_value = type_cast(value)
if !blank
casted_value = default if casted_value.blank?
elsif array && has_default?
casted_value = default if value.nil?
elsif !null
casted_value = default if casted_value.nil?
end
Expand Down
10 changes: 8 additions & 2 deletions spec/active_record/typed_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@
describe 'model.typed_stores' do
it "can access keys" do
stores = model.class.typed_stores
expect(stores[:settings].keys).to eq [:no_default, :name, :email, :cell_phone, :public, :enabled, :age, :max_length, :rate, :price, :published_on, :remind_on, :published_at_time, :remind_at_time, :published_at, :remind_at, :total_price, :shipping_cost, :grades, :tags, :nickname, :author, :source, :signup, :country]
expect(stores[:settings].keys).to eq [:no_default, :name, :email, :cell_phone, :public, :enabled, :age, :max_length, :rate, :price, :published_on, :remind_on, :published_at_time, :remind_at_time, :published_at, :remind_at, :total_price, :shipping_cost, :grades, :tags, :subjects, :nickname, :author, :source, :signup, :country]
end

it "can access keys even when accessors are not defined" do
Expand Down Expand Up @@ -889,9 +889,15 @@
expect(model.reload.grades).to be == [[1, 2], [3, 4, 5]]
end

it 'defaults to [] if provided default is not an array' do
model.update(subjects: nil)
expect(model.reload.subjects).to be == []
end

# Not sure about pg_native and if this test should be outside of this block.
it 'retreive default if assigned null' do
model.update(tags: nil)
expect(model.reload.tags).to be == []
expect(model.reload.tags).to be == ['article']
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def define_columns(t, array: false)

if t.is_a?(ActiveRecord::TypedStore::DSL)
t.integer :grades, array: true
t.string :tags, array: true, null: false, default: [].to_yaml
t.string :tags, array: true, null: false, default: ['article']
t.string :subjects, array: true, null: false, default: ['mathematics'].to_yaml

t.string :nickname, blank: false, default: 'Please enter your nickname'
end
Expand Down

0 comments on commit 1b3fcb9

Please sign in to comment.