A platform for community discussion. Free, open, simple.
FEATURE: allow registration of an array custom field
Sam1ac1ee428721
diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rbindex b33b4fa213e..a2bc01b254d 100644--- a/app/models/concerns/has_custom_fields.rb+++ b/app/models/concerns/has_custom_fields.rb@@ -6,7 +6,7 @@ module HasCustomFieldsdef self.append_field(target, key, value, types)if target.has_key?(key)target[key] = [target[key]] if !target[key].is_a? Array- target[key] << cast_custom_field(key, value, types)+ target[key] << cast_custom_field(key, value, types, _return_array = false)elsetarget[key] = cast_custom_field(key, value, types)end@@ -28,16 +28,26 @@ module HasCustomFieldstypes[key]end- def self.cast_custom_field(key, value, types)+ def self.cast_custom_field(key, value, types, return_array = true)return value unless type = get_custom_field_type(types, key)- case type- when :boolean then !!CUSTOM_FIELD_TRUE.include?(value)- when :integer then value.to_i- when :json then ::JSON.parse(value)- else- value+ array = nil++ if Array === type+ type = type[0]+ array = true if return_arrayend++ result =+ case type+ when :boolean then !!CUSTOM_FIELD_TRUE.include?(value)+ when :integer then value.to_i+ when :json then ::JSON.parse(value)+ else+ value+ end++ array ? [result] : resultendenddiff --git a/spec/components/concern/has_custom_fields_spec.rb b/spec/components/concern/has_custom_fields_spec.rbindex 2097583a4a3..035e0260c5a 100644--- a/spec/components/concern/has_custom_fields_spec.rb+++ b/spec/components/concern/has_custom_fields_spec.rb@@ -98,6 +98,15 @@ describe HasCustomFields doendit "handles arrays properly" do++ CustomFieldsTestItem.register_custom_field_type "array", [:integer]+ test_item = CustomFieldsTestItem.new+ test_item.custom_fields = { "array" => ["1"] }+ test_item.save++ db_item = CustomFieldsTestItem.find(test_item.id)+ expect(db_item.custom_fields).to eq("array" => [1])+test_item = CustomFieldsTestItem.newtest_item.custom_fields = { "a" => ["b", "c", "d"] }test_item.save