Skip to content

Commit

Permalink
FIX: Allow setting an array custom field to a singleton value (discou…
Browse files Browse the repository at this point in the history
…rse#24636)

Also, validation happens per item in an array field.
  • Loading branch information
danielwaterworth authored Nov 29, 2023
1 parent eef93ac commit 434ae5b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
11 changes: 2 additions & 9 deletions app/models/concerns/has_custom_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ def append_field(target, key, value)
def validate(obj, name, value)
return if value.nil?

size =
if Array === type || (type != :json && Array === value)
value.map { |v| serialize(v).bytesize }.max || 0
else
serialize(value).bytesize
end

if size > max_length
if serialize(value).bytesize > max_length
obj.errors.add(
:base,
I18n.t("custom_fields.validations.max_value_length", max_value_length: max_length),
Expand Down Expand Up @@ -281,7 +274,7 @@ def save_custom_fields(force = false)
field_type = descriptor.type

if Array === field_type || (field_type != :json && Array === value)
value = value || []
value = Array(value || [])
value.compact!
sub_type = field_type[0]

Expand Down
7 changes: 7 additions & 0 deletions spec/lib/concern/has_custom_fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ class CustomFieldsTestItemCustomField < ActiveRecord::Base
expect(db_item.custom_fields).to eq("a" => "b", "c" => "d")
end

it "handles assigning singleton values to array fields" do
CustomFieldsTestItem.register_custom_field_type "array", [:integer]
test_item = CustomFieldsTestItem.new
test_item.custom_fields = { "array" => "1" }
test_item.save
end

it "handles arrays properly" do
CustomFieldsTestItem.register_custom_field_type "array", [:integer]
test_item = CustomFieldsTestItem.new
Expand Down

0 comments on commit 434ae5b

Please sign in to comment.