Skip to content

Commit

Permalink
DEV: Allow setting max_length for field types using the plugin API (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwaterworth authored Nov 29, 2023
1 parent 384a8b1 commit eef93ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion app/models/concerns/has_custom_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def append_custom_field(target, key, value)
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",
Expand Down
30 changes: 20 additions & 10 deletions lib/plugin/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -552,28 +552,38 @@ def notify_after_initialize
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)
Expand Down

0 comments on commit eef93ac

Please sign in to comment.