New columns in directory (/users)

Hello everyone,

I want to add a few columns in the directory items model. ( ‘Solutions’ and ‘Points’ )
I was able to modify the code for the model and templates directly, but how can I achieve this via a plugin ?

If not possible via plugin, is it possible to use my own github repo ( with these changes ) instead but somehow continue to get updates from Discourse repo.

Thanks

I’m Not able to understand the exact usecase but definitely adding some columns can be achieved via plugins!

You may start writing a plugin using the official Guidelines and Then use it in order to ensure You get official updates.

Here is Some Help:

Hey @itsbhanusharma, thanks for the link.
I’ve gone through the Beginner’s Guide, but couldn’t find DB modifications in it.

You’ve any plugin in mind which does something similar ?

Have You gone through the whole series?

I’d have a look in the Plugin repo to find if there is something.

EDIT: have a Look Here maybe this Helps!

FWIW I’ve decided to go this route with a plugin of mine, where PluginStoreRow and CustomFields didn’t qqquite cut it on their own. :warning: Caveat emptor :warning: You should be trying really hard not to do this - It’s not recommended behaviour to modify the database from within a plugin, but it can be done unobtrusively in small, additive doses (I’d say you never for any reason want to remove or change existing columns)

Here’s how I did it:

  • Write a migration in the plugin which modifies the DB
# plugins/my_plugin/migrations/add_column.rb
class AddColumn < ActiveRecord::Migration[5.1]
  def change
    add_column :post_custom_fields, :my_column, :jsonb, default: {}, index: true
  end
end

NB: it’s a good idea to namespace your column with the name of your plugin, since adding a column with a generic name like ‘value’ could end up actually existing in the future and then you’d have a real problem.

  • When the plugin is loaded, check to see if the migration has been run, and if not, run it
# plugin.rb
if !PostCustomField.new.respond_to?(:my_column)
  require Rails.root.join('plugins', 'my_plugin', 'migrations', 'add_column')
  AddColumns.new.up # <-- this runs the migration
end
  • The first time you run rails s or rails c after this, you should see the following:
add_column(:post_custom_fields, :my_column, :jsonb, {:default=>{}, :index=>true})

that means it worked! Subsequent boots of the app shouldn’t display this message.

  • Important, don’t skip plz - Provide a method for users to clean up your database changes if they don’t want your plugin anymore. I did it with a rake task.
# plugins/my_plugin/tasks/remove_column.rake

# (I think there's a better way to do this, but this line works.)
# Essentially, we need the environment loaded so that ActionRecord::Migration is defined.
require File.expand_path(File.dirname(__FILE__) + '../../../config/environment'

desc "remove my_column from the database"
task "my_column:remove" do
  require Rails.root.join('plugins', 'my_plugin', 'migrations', 'add_column')
  AddColumn.new.down
end

Then, in your README, instructions on how to use it

This plugin adds a column to the database. If you want to remove this plugin and don't plan on using it again, perform the following steps:

- Enter your discourse container 
./launcher enter app
- Run the following command to reverse the database changes
bundle exec rake -f plugins/my_plugin/tasks/remove_column.rake -T
- Remove the 'git clone' line from your app.yml
- Rebuild your container
./launcher rebuild app

Note that dropping this column is permanent and irreversible for the data stored there..

As always, let me know if I’ve missed something or if this can be improved.

Rails の Active Record ドキュメントには、up メソッドを直接呼び出す例は見つかりませんでした。

AddColumns.new.up  

通常、マイグレーションは rake db:migrate コマンドで実行され、Ruby で直接行うものではないようです。
そのため、プラグイン内に migrate ディレクトリを設け、plugin.rb ファイルからそれを /discourse/db/migrate ディレクトリへシンボリックリンクで結ぶ方が良さそうです。こうすれば、ロールバックを含む Rails のマイグレーションインフラストラクチャをそのまま活用できます。プラグインの README にも、特定のマイグレーションをロールバックする方法について記載すると良いでしょう。以下のようなコマンドで特定のマイグレーションをロールバックできるようです:

rake db:migrate:down VERSION=20100905201547

https://stackoverflow.com/questions/3647685/how-to-rollback-a-specific-migration
この件についてご意見をいただければ幸いです。また、カスタムフィールドへのリンクだけを提示するのは生産的ではないと考えています。私は Discourse を使って中英辞典を作成したいと考えており、カスタムフィールドだけでは対応できません。

プラグイン用のマイグレーションとモデルジェネレーターを作成しました:GitHub - spirobel/discourse at plugin_model_and_migrations · GitHub
ここで示されているようにプラグインのマイグレーションを処理する方法は、良いアイデアではないと思います。