开发 Discourse 插件 - 第一部分 - 创建一个基础插件

Hmmmm. That’s a tricky one, because the symlink needed to be deleted, otherwise the Docker mount fails (as it cannot overwrite the existing file).

I had a bit of a google around for solutions, and found an interesting solution which would dereference the symlink and copy the symlink’s target into the specified directory, but couldn’t find anything that would create a Docker volume mount, thus preserving the dynamic link and bi-directional synchronisation. Happy to be steered towards a solution as this would be a neat feature to have.

请告诉我如何将您的验证添加到 app/models/user.rb 模型中?

1 个赞

您可以在 plugin.rb 中使用常规 Ruby 代码来实现此功能:

class ::User < ActiveRecord::Base
  validates_presence_of :your_attribute
end
6 个赞

刚开始开发我的第一个小插件 :blush: 我有几个问题,希望能有人帮忙解答…

第一个问题是:我们是应该使用本指南中的内容,还是真的应该使用插件生成器?

本指南中未涵盖这一点,但我们如何从 plugin.rb 文件中将数据提供给模板?

例如,假设我想在用户访问主题时,于插件出口 topic-above-posts 显示一条随机欢迎消息,该消息从 plugin.rb 中的一个数组里随机选取……我该如何将数据发送到模板?我推测模板文件也应该放在这里:

plugins/my_new_plugin/assets/javascripts/discourse/templates/connectors/topic-above-posts/my_new_plugin.hbs(这样对吗?)

非常感谢您的任何建议 :blush:

2 个赞

使用插件生成器通常是入门并了解插件应如何构建的绝佳方式。我推荐使用它。

现在,关于如何将 Ruby 代码集成到前端:这是两个不同的应用程序。前端应用(Ember)需要通过某种方式从服务器(Rails)请求数据。通常我们通过 AJAX 调用来实现这一点,但你也可以选择在 SiteSerializer 中添加内容,使其作为论坛数据的一部分自动发送。

否则,你需要查阅如何在 Rails 中添加路由和控制器,并将消息以 JSON 格式发送。

8 个赞

谢谢 Robin!

您是否知道有关这方面的教程?或者,有没有一个简单的插件(甚至是一个示例插件)可以参考,能给我们指明正确的方向?(如果没有,您能否方便地为我们发布一个?我想这会帮助很多人 :blush:

另外,关于写书您还有什么想法吗?如果您有兴趣,我可以帮您联系相关人员 :smiley:

1 个赞

谢谢你的夸奖,但我确实太忙了,没法考虑写书。而且技术类书籍的受众非常有限。我认识的那些写过书的人都说,写书是为了自己,不是为了赚钱:slight_smile:

我想不出一个简单的插件能从服务器端返回内容。discourse-tooltips 添加了一个路由,然后利用它来获取主题的预览,当你将鼠标悬停在上面时就会显示,这或许会有所帮助。

8 个赞

很快,我将开始插件开发……

问题:

是否有我们可以实际插入 pluginhooks 列表?

例如,假设我们想编写一个插件,在帖子显示之前处理 cooked 数据,我们期望至少有一个开始 hook 和一个结束 hook,例如:

  • display_post_start

  • display_post_complex

然后,我们的 plugin 将在上述我们选择的 hook locationhook 到代码中。

是否有这些 plugin hooks 的列表?在 DiscourseWorld 中这些 plugin hooks 被称为什么?

更新:找到了这个:

git grep "plugin-outlet" -- "*.hbs"

但我没看到用于修改 cooked 帖子内容的 outlet

是否有关于创建自定义 outlet 的教程?例如,用于修改帖子 cooked 部分的 outlet

2 个赞

您正在寻找本指南的 decorateCooked() 部分:

5 个赞

我为自定义插件使用了这个骨架。我需要为即将到来的 Ember 5 过渡做任何更改吗?

我猜不用?虽然这只是一个 linter?

1 个赞

该骨架与 Ember 5 兼容。但如果您添加了任何 JavaScript 代码或 hbs 模板,则需要按照公告中的说明检查弃用情况。

是的,package.json 中的内容仅与 linting 相关,它不会影响插件的功能或兼容性。

3 个赞

请添加到这个

rake plugin:create[plugin-name]

我今天花了太长时间才找到 Automating Discourse Plugin Setup with `create-discourse-plugin` gem (不知道为什么我记不起这是 rake 任务的名称?! :person_shrugging:

5 个赞

我也想在 Docker 设置中添加一个注释,因为上面的说明在尝试创建插件时会因类似以下错误而失败:

ambientimpact:~/.../Discourse/discourse$ d/rake plugin:create[neurocracy-date-rewrite]
Cloning 'https://github.com/discourse/discourse-plugin-skeleton' to '/src/plugins/neurocracy-date-rewrite'...
Initializing git repository...
Initialized empty Git repository in /src/plugins/neurocracy-date-rewrite/.git/
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: empty ident name (for <discourse@localhost>) not allowed
rake aborted!
Command failed with exit 128: git
/src/lib/tasks/plugin.rake:354:in `system'
/src/lib/tasks/plugin.rake:354:in `block (2 levels) in <main>'
/src/lib/tasks/plugin.rake:344:in `chdir'
/src/lib/tasks/plugin.rake:344:in `block in <main>'
Tasks: TOP => plugin:create
(See full trace by running task with --trace)

在尝试创建插件之前,您需要先在容器中设置您的 .gitconfig(根据需要更改电子邮件和姓名):

d/exec git config --global user.email "you@example.com"
d/exec git config --global user.name "Your Name"
d/rake plugin:create[plugin-name]
1 个赞