mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 03:51:07 +08:00
DEV: Plugin API to allow creation of directory columns with item query (#13402)
The first thing we needed here was an enum rather than a boolean to determine how a directory_column was created. Now we have `automatic`, `user_field` and `plugin` directory columns. This plugin API is assuming that the plugin has added a migration to a column to the `directory_items` table. This was created to be initially used by discourse-solved. PR with API usage - https://github.com/discourse/discourse-solved/pull/137/
This commit is contained in:

committed by
GitHub

parent
ea2833d0d8
commit
0c42a29dc4
@ -373,6 +373,17 @@ class Plugin::Instance
|
||||
assets
|
||||
end
|
||||
|
||||
def add_directory_column(column_name, query:, icon: nil)
|
||||
validate_directory_column_name(column_name)
|
||||
directory_column = DirectoryColumn
|
||||
.find_or_create_by(name: column_name, icon: icon, type: DirectoryColumn.types[:plugin]) do |column|
|
||||
column.position = DirectoryColumn.maximum("position") + 1
|
||||
column.enabled = false
|
||||
end
|
||||
DirectoryColumn.add_plugin_directory_column(column_name)
|
||||
DirectoryItem.add_plugin_query(query)
|
||||
end
|
||||
|
||||
def delete_extra_automatic_assets(good_paths)
|
||||
return unless Dir.exists? auto_generated_path
|
||||
|
||||
@ -964,6 +975,11 @@ class Plugin::Instance
|
||||
|
||||
private
|
||||
|
||||
def validate_directory_column_name(column_name)
|
||||
match = /^[_a-z]+$/.match(column_name)
|
||||
raise "Invalid directory column name '#{column_name}'. Can only contain a-z and underscores" unless match
|
||||
end
|
||||
|
||||
def write_asset(path, contents)
|
||||
unless File.exists?(path)
|
||||
ensure_directory(path)
|
||||
|
Reference in New Issue
Block a user