Разработка плагинов для Discourse — часть 5: добавление административного интерфейса

Предыдущий урок: Developing Discourse Plugins - Part 4 - Setup git


Иногда настройки сайта недостаточно для того, чтобы ваш плагин работал так, как вы хотите, с точки зрения административного интерфейса. Например, если вы установите плагин discourse-akismet, вы можете заметить, что он добавляет элемент навигации в раздел администрирования плагинов вашего Discourse:

В этом уроке мы покажем, как добавить административный интерфейс для вашего плагина. Я назову свой плагин purple-tentacle в честь одной из моих любимых компьютерных игр. Серьезно, я действительно люблю эту игру!

Настройка административного маршрута

Начнем с добавления файла plugin.rb, как мы делали в предыдущих частях урока.

plugin.rb

# name: purple-tentacle
# about: A sample plugin showing how to add a plugin route
# version: 0.1
# authors: Robin Ward
# url: https://github.com/discourse/purple-tentacle

add_admin_route 'purple_tentacle.title', 'purple-tentacle'

Discourse::Application.routes.append do
  get '/admin/plugins/purple-tentacle' => 'admin/plugins#index', constraints: StaffConstraint.new
end

Строка add_admin_route сообщает Discourse, что этот плагин потребует ссылку на странице /admin/plugins. Её заголовок будет purple_tentacle.title из нашего файла i18n-переводов, а ссылка будет вести на маршрут purple-tentacle.

Строки ниже настраивают серверную часть сопоставления маршрутов для нашего плагина. Discourse исходит из предположения, что почти каждый маршрут на фронтенде имеет соответствующий серверный маршрут, предоставляющий данные. Для этого примера плагина нам на самом деле не нужны данные с бэкенда, но мы должны сообщить Discourse, чтобы он отдавал что-то, если пользователь перейдет по адресу /admin/plugins/purple-tentacle напрямую. Эта строка просто говорит: «эй, если пользователь перейдет по этому URL напрямую на серверной стороне, отдай содержимое по умолчанию для плагинов!»

(Если это сбивает с толку, не переживайте слишком сильно — мы вернемся к этому в следующем уроке, когда будем разбирать действия на стороне сервера.)

Далее добавим шаблон, который будет отображаться, когда пользователь перейдет по пути /admin/plugins/purple-tentacle. Это будет просто кнопка, которая показывает анимированный GIF с фиолетовым щупальцем при нажатии на кнопку:

assets/javascripts/discourse/templates/admin/plugins-purple-tentacle.gjs

import DButton from "discourse/components/d-button";

<template>
  {{#if @controller.tentacleVisible}}
    <div class="tentacle">
      <img src="https://eviltrout.com/images/tentacle.gif" />
    </div>
  {{/if}}

  <div class="buttons">
    <DButton
      @label="purple_tentacle.show"
      @action={{@controller.showTentacle}}
      @icon="eye"
      @id="show-tentacle"
    />
  </div>
</template>

Если вы знакомы с основами Handlebars, этот шаблон должен быть довольно простым для понимания. Компонент <DButton /> используется в Discourse для отображения кнопки с меткой и иконкой.

Чтобы подключить наш новый шаблон, нам нужно создать карту маршрутов:

assets/javascripts/discourse/purple-tentacle-route-map.js

export default {
  resource: "admin.adminPlugins",
  path: "/plugins",
  map() {
    this.route("purple-tentacle");
  },
};

Карта маршрутов — это механизм, добавленный в Discourse, чтобы плагины могли добавлять маршруты в приложение Ember. Синтаксис внутри map() очень похож на маршрутизатор Ember. В нашем случае карта маршрутов очень проста: она просто объявляет один маршрут под названием purple-tentacle внутри /admin/plugins.

Наконец, добавим строки переводов:

config/locales/client.en.yml

en:
  js:
    purple_tentacle:
      title: "Purple Tentacle"
      show: "Show Purple Tentacle"

Если вы перезапустите сервер разработки, вы сможете перейти на страницу /admin/plugins и увидите нашу ссылку! Если вы нажмете на неё, вы увидите кнопку для отображения нашего фиолетового щупальца:

К сожалению, при нажатии на кнопку ничего не происходит :frowning:

Если вы посмотрите в консоль разработчика, вы увидите ошибку, которая дает подсказку, почему это происходит:

Uncaught Error: Nothing handled the action 'showTentacle'`

Ага, причина в том, что в нашем шаблоне мы полагаемся на несколько вещей:

  1. При нажатии пользователем на кнопку будет вызван метод showTentacle контроллера.
  2. Метод showTentacle должен установить свойство tentacleVisible в true, чтобы изображение появилось.

Если вы еще не читали руководство Ember по контроллерам, сейчас самое время это сделать, так как мы реализуем контроллер для нашего шаблона purple-tentacle, который будет обрабатывать эту логику.

Создайте следующий файл:

assets/javascripts/discourse/controllers/admin-plugins-purple-tentacle.js

import Controller from "@ember/controller";
import { action } from "@ember/object";
import { tracked } from "@glimmer/tracking";

export default class AdminPluginsPurpleTentacleController extends Controller {
  @tracked tentacleVisible = false;

  @action
  showTentacle() {
    this.tentacleVisible = true;
  }
}

Теперь при обновлении страницы нажатие на кнопку показывает нашего анимированного персонажа!

Оставлю читателю в качестве дополнительного упражнения добавить кнопку, которая скрывает щупальце при нажатии :smile:

Если у вас возникли трудности с запуском вашей версии этого плагина, я выложил его на GitHub.


Подробнее в серии

Часть 1: Основы плагинов
Часть 2: Выходы плагинов
Часть 3: Настройки сайта
Часть 4: Настройка git
Часть 5: Эта тема
Часть 6: Тесты принятия
Часть 7: Опубликуйте свой плагин


Этот документ находится под версионным контролем — предлагайте изменения на GitHub.

28 лайков

Hm, doesn’t seem to work on my local side… did anyone face the same problem?

What’s the problem? If you install the purple tentacle plugin does it not work?

edit: issue resolved. it works!

@eviltrout nope, it doesn’t work. I believe @ladydanger is also facing the same problem.
nothing appears in the site settings or plugins tabs.
I tried both following the instructions as well as symlink-ing your plugin (dl-ed from github).
I then tried to create server.en.yml, client.en.yml, settings.yml following the format of other plugins, but the image wouldn’t render anyhow.

probably something wrong with the …route-map.js.es6 or config files.

interestingly enough, akismet’s plugin does work for me.


One issue that keeps on biting me is that I still need to do

rm -fr /tmp/cache

To kick away some odd caching we have in some cases.

1 лайк

@eviltrout

oh now this is an interesting finding - apparently our ad plugins were interfering with the purple-tentacle plugin. or the other way round. it works now, after having removed the ad plugin files.

  • it now works with our ad plugins! possibly some minor bug yesterday. all is good!

@sam, what’s the difference between rm -rf tmp and rm -fr /tmp/cache

I usually only delete the cache directory, no need to nuke the entire tmp directory.

2 лайка

Are you sure it was a plugin conflict?

I copied it into my localhost plugin folder and started up the VM without removing the temp folder and got the same results as you posted.

I was puzzled to see the route in object dot notation but ignored that.

I then started up the VM and did remove the temp folder and all was OK

1 лайк

Actually, sometimes you need just cache, and sometimes you need the whole tmp directory.

Maybe we could have an initializer that checks the timestamps on the plugin folders and nukes tmp if they were updated (dev only).

3 лайка

One thing I really should have warned you about is I started raising errors on ember deprecations. We are really working towards upgrading our version of Ember soon and I wanted to make sure developers caught them. It’s possible your plugin has a deprecation or two and is now raising an error with the latest version of Discourse.

If you can’t figure out how to fix the deprecation, it’s okay to temporarily disable it in the Discourse working directory you are using. Try removing these two lines.

2 лайка

No clue what I’m doing wrong here, but it shows up like this:

It must be the /config/locales/en.yml not loading, but why?

I put this file structure in the root of the plugin directory. Verified it’s there, nuked the tmp, restart rails.

en:
  js:
    purple_tentacle:
      title: "Purple Tentacle"
      show: "Show Purple Tentacle"
1 лайк

I was a bit surprised to see the file named “en.yml” but figured either eviltrout knew something I didn’t (which I’m sure he does … anyway) or that maybe something had changed the way things worked.

I’m guessing that what Robin meant was client,en.yml because that works for me.

the file goes in the plugins config/locales folder

4 лайка

Thanks I’ll try that after I get my dev environment setup on the xeon, went through hell getting ubuntu 16.04 to work right with my hardware. Turned out it wasn’t the server mobo, but the new nvidia card.

Yea I meant I put all the directories listed hierarchically starting with the plugins folder. Was wondering if for some reason that last file went elsewhere.

Same thing.

I tried naming the file client,en.yml, client.en.yml, and creating a client folder placing en.yml inside it.

Same result

Did you stop vagrant, clear the cache
$> rm -rf tmp
and restart vagrant?

I’m not using vagrant, yes I nuked tmp, restart rails.

This is a bit too annoying, and not a big deal. I’m going to move forward, but thanks for the efforts.

I have a feeling you may have the folder structure wrong, or maybe the naming. Mine is like

/discourse
../plugins
..../{yourpluginname}
......plugin.rb
....../config
........settings.yml
......../locales
..........client.en.yml
..........server.en.yml
....../assets
......../javascripts
........../discourse
............{yourpluginname}-route-map.js.es6
............/controllers
..............admin-plugins-{yourpluginname}.js.es6
............/templates
............../admin
................plugins-{yourpluginname}.hbs
1 лайк

That’s the file structure I have.

I don’t see a point in getting stuck on this, already hacked together a couple of rails sites and trying to absorb as much as possible.

Just going to leave this as some simple error on my part that I’ll come look back at later on and probably realize right away what it was.

Thanks, thou

Another mistake of mine! I’ve updated the OP, thanks.

2 лайка

Is there any way I can change a global plugin setting, say, a siteSetting, from here?
I need admins to be able to use this interface to add data into a global array.