我刚刚经历了这个过程,经历了很多试错,所以我想记录下我的发现,以帮助接下来的开发者。
我需要的内容:
-
注册您的自定义字段类型(我的是布尔类型,默认是字符串类型)
# plugin.rb User.register_custom_field_type 'my_preference', :boolean -
注册该自定义字段应由用户可编辑。语法与
params.permit(...)匹配# plugin.rb register_editable_user_custom_field :my_preference # 标量类型(字符串、整数等) 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!