Skip to content

A platform for community discussion. Free, open, simple.

DEV: Allow setting max_length for field types using the plugin API (#24635)

Daniel Waterwortheef93ac926f5
diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rb
index 3662c4534f6..6174bd13a9d 100644
--- a/app/models/concerns/has_custom_fields.rb
+++ b/app/models/concerns/has_custom_fields.rb
@@ -114,7 +114,9 @@ module HasCustomFields
get_custom_field_descriptor(key).append_field(target, key, value)
end
- def register_custom_field_type(name, type, max_length: DEFAULT_FIELD_DESCRIPTOR.max_length)
+ def register_custom_field_type(name, type, max_length: nil)
+ max_length ||= DEFAULT_FIELD_DESCRIPTOR.max_length
+
if Array === type
Discourse.deprecate(
"Array types for custom fields are deprecated, use type :json instead",
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index 4346f1caf46..35acf3e5a1d 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -552,28 +552,38 @@ class Plugin::Instance
end
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
- def register_category_custom_field_type(name, type)
- reloadable_patch { |plugin| Category.register_custom_field_type(name, type) }
+ def register_category_custom_field_type(name, type, max_length: nil)
+ reloadable_patch do |plugin|
+ Category.register_custom_field_type(name, type, max_length: max_length)
+ end
end
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
- def register_topic_custom_field_type(name, type)
- reloadable_patch { |plugin| ::Topic.register_custom_field_type(name, type) }
+ def register_topic_custom_field_type(name, type, max_length: nil)
+ reloadable_patch do |plugin|
+ ::Topic.register_custom_field_type(name, type, max_length: max_length)
+ end
end
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
- def register_post_custom_field_type(name, type)
- reloadable_patch { |plugin| ::Post.register_custom_field_type(name, type) }
+ def register_post_custom_field_type(name, type, max_length: nil)
+ reloadable_patch do |plugin|
+ ::Post.register_custom_field_type(name, type, max_length: max_length)
+ end
end
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
- def register_group_custom_field_type(name, type)
- reloadable_patch { |plugin| ::Group.register_custom_field_type(name, type) }
+ def register_group_custom_field_type(name, type, max_length: nil)
+ reloadable_patch do |plugin|
+ ::Group.register_custom_field_type(name, type, max_length: max_length)
+ end
end
# Applies to all sites in a multisite environment. Ignores plugin.enabled?
- def register_user_custom_field_type(name, type)
- reloadable_patch { |plugin| ::User.register_custom_field_type(name, type) }
+ def register_user_custom_field_type(name, type, max_length: nil)
+ reloadable_patch do |plugin|
+ ::User.register_custom_field_type(name, type, max_length: max_length)
+ end
end
def register_seedfu_fixtures(paths)