From c36318985811ba37e4db5755020f470ef056dd63 Mon Sep 17 00:00:00 2001 From: fzngagan Date: Tue, 25 Aug 2020 11:39:34 +0530 Subject: [PATCH] Accounted for the change while reading the fields added specs to confirm working --- app/models/concerns/has_custom_fields.rb | 2 +- .../concern/has_custom_fields_spec.rb | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rb index 3d6b52427b5..ca8d6df23e6 100644 --- a/app/models/concerns/has_custom_fields.rb +++ b/app/models/concerns/has_custom_fields.rb @@ -274,7 +274,7 @@ module HasCustomFields protected def refresh_custom_fields_from_db - target = Hash.new + target = HashWithIndifferentAccess.new _custom_fields.order('id asc').pluck(:name, :value).each do |key, value| self.class.append_custom_field(target, key, value) end diff --git a/spec/components/concern/has_custom_fields_spec.rb b/spec/components/concern/has_custom_fields_spec.rb index a567f3e8eda..e74da75159e 100644 --- a/spec/components/concern/has_custom_fields_spec.rb +++ b/spec/components/concern/has_custom_fields_spec.rb @@ -337,6 +337,32 @@ describe HasCustomFields do expect(test_item.custom_fields['hello']).to eq('world') expect(test_item.custom_fields['abc']).to eq('ghi') end + + it 'allows using string and symbol indices interchangably' do + test_item = CustomFieldsTestItem.new + + test_item.custom_fields["bob"] = "marley" + test_item.custom_fields["jack"] = "black" + + # In memory + expect(test_item.custom_fields[:bob]).to eq('marley') + expect(test_item.custom_fields[:jack]).to eq('black') + + # Persisted + test_item.save + test_item.reload + expect(test_item.custom_fields[:bob]).to eq('marley') + expect(test_item.custom_fields[:jack]).to eq('black') + + ## Update via string index again + test_item.custom_fields['bob'] = 'the builder' + + expect(test_item.custom_fields[:bob]).to eq('the builder') + test_item.save + test_item.reload + + expect(test_item.custom_fields[:bob]).to eq('the builder') + end end end end