このプロセスを通過し、試行錯誤を繰り返した経験から、次にやってくる開発者のために私の知見をまとめておくことにしました。
必要な手順は以下の通りです:
-
カスタムフィールドタイプを登録する(私の場合は boolean、デフォルトは string)
# plugin.rb User.register_custom_field_type 'my_preference', :boolean -
カスタムフィールドがユーザーによって編集可能であることを登録する。構文は
params.permit(...)に一致します。# plugin.rb register_editable_user_custom_field :my_preference # スカラー型(string、integer など) register_editable_user_custom_field [:my_preference, my_preference: []] # 配列型の 경우 register_editable_user_custom_field [:my_preference, my_preference: {}] # JSON 型の 경우 -
これらを CurrentUserSerializer でシリアライズされるフィールドに追加する
# plugin.rb DiscoursePluginRegistry.serialized_current_user_fields << 'my_preference' -
ユーザー設定を表示するコンポーネントを作成する
// assets/javascripts/discourse/templates/components/my-preference.hbs <label class="control-label">マイカスタム設定!</label> {{preference-checkbox labelKey="my_plugin.preferences.key" checked=user.custom_fields.my_preference}} -
そのコンポーネントを、設定プラグインの出口(私の場合はユーザー設定内の ‘interface’)に接続する
# assets/javascripts/discourse/connectors/user-preferences-interface/my-preference.hbs {{my-preference user=model}} -
その設定タブで「カスタムフィールド」が保存されるようにする
import { withPluginApi } from 'discourse/lib/plugin-api' export default { name: 'post-read-email', initialize () { withPluginApi('0.8.22', api => { api.modifyClass('controller:preferences/emails', { actions: { save () { this.get('saveAttrNames').push('custom_fields') this._super() } } }) }) } }
このドキュメントはバージョン管理されています。変更案は GitHub で提案してください。
of course. Just add another. It’s been a long day. Thanks!