mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 06:29:30 +08:00
FEATURE: allow registration of an array custom field
This commit is contained in:
parent
bcfd9cf8b5
commit
1ac1ee4287
|
@ -6,7 +6,7 @@ module HasCustomFields
|
||||||
def self.append_field(target, key, value, types)
|
def self.append_field(target, key, value, types)
|
||||||
if target.has_key?(key)
|
if target.has_key?(key)
|
||||||
target[key] = [target[key]] if !target[key].is_a? Array
|
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)
|
||||||
else
|
else
|
||||||
target[key] = cast_custom_field(key, value, types)
|
target[key] = cast_custom_field(key, value, types)
|
||||||
end
|
end
|
||||||
|
@ -28,16 +28,26 @@ module HasCustomFields
|
||||||
types[key]
|
types[key]
|
||||||
end
|
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)
|
return value unless type = get_custom_field_type(types, key)
|
||||||
|
|
||||||
case type
|
array = nil
|
||||||
when :boolean then !!CUSTOM_FIELD_TRUE.include?(value)
|
|
||||||
when :integer then value.to_i
|
if Array === type
|
||||||
when :json then ::JSON.parse(value)
|
type = type[0]
|
||||||
else
|
array = true if return_array
|
||||||
value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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] : result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -98,6 +98,15 @@ describe HasCustomFields do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "handles arrays properly" do
|
it "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.new
|
test_item = CustomFieldsTestItem.new
|
||||||
test_item.custom_fields = { "a" => ["b", "c", "d"] }
|
test_item.custom_fields = { "a" => ["b", "c", "d"] }
|
||||||
test_item.save
|
test_item.save
|
||||||
|
|
Loading…
Reference in New Issue
Block a user