mirror of
https://github.com/discourse/discourse.git
synced 2024-11-30 03:43:38 +08:00
FIX: Allow setting an array custom field to a singleton value (#24636)
Also, validation happens per item in an array field.
This commit is contained in:
parent
7f3edcbdc6
commit
7fbbefe363
|
@ -20,14 +20,7 @@ module HasCustomFields
|
|||
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),
|
||||
|
@ -281,7 +274,7 @@ module HasCustomFields
|
|||
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]
|
||||
|
||||
|
|
|
@ -134,6 +134,13 @@ RSpec.describe HasCustomFields do
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user