Desenvolvendo Plugins do Discourse - Parte 5 - Adicionar uma interface de administrador

Tutorial anterior: Developing Discourse Plugins - Part 4 - Setup git


Às vezes, as configurações do site não são uma interface de administração suficiente para o seu plugin funcionar como você deseja. Por exemplo, se você instalar o plugin discourse-akismet, você deve ter notado que ele adiciona um item de navegação à seção de plugins de administração em sua instalação do Discourse:

Neste tutorial, mostraremos como adicionar uma interface de administração para o seu plugin. Vou chamar meu plugin de purple-tentacle, em homenagem a um dos meus jogos de computador favoritos. Sério, eu realmente amo esse jogo!

Configurando a Rota de Administração

Vamos começar adicionando um plugin.rb como fizemos nas partes anteriores do tutorial.

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

A linha add_admin_route informa ao Discourse que este plugin precisará de um link na página /admin/plugins. Seu título será purple_tentacle.title do nosso arquivo de tradução i18n e ele apontará para a rota purple-tentacle.

As linhas abaixo configuram o mapeamento do lado do servidor de rotas para o nosso plugin. Uma suposição que o Discourse faz é que quase toda rota no front-end tem uma rota no lado do servidor que fornece dados. Para este plugin de exemplo, na verdade, não precisamos de nenhum dado do back-end, mas precisamos dizer ao Discourse para servir algo caso o usuário visite /admin/plugins/purple-tentacle diretamente. Esta linha apenas diz: ‘ei, se o usuário visitar essa URL diretamente no lado do servidor, sirva o conteúdo padrão de plugins!’

(Se isso for confuso, não se preocupe muito, voltaremos a isso em um tutorial futuro quando lidarmos com ações do lado do servidor.)

Em seguida, adicionaremos um template que será exibido quando o usuário visitar o caminho /admin/plugins/purple-tentacle. Será apenas um botão que mostra um gif animado de um tentáculo roxo quando o usuário clica em um botão:

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

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

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

Se você aprendeu o básico do handlebars, o template deve ser bem simples de entender. O <DButton /> é um componente no Discourse que usamos para exibir um botão com um rótulo e um ícone.

Para conectar nosso novo template, precisamos criar um mapa de rotas:

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

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

Um mapa de rotas é algo que adicionamos ao discourse para que os plugins pudessem adicionar rotas ao aplicativo ember. A sintaxe dentro de map() é muito semelhante ao roteador do Ember. Neste caso, nosso mapa de rotas é muito simples, ele apenas declara uma rota chamada purple-tentacle sob /admin/plugins.

Finalmente, vamos adicionar nossas strings de tradução:

config/locales/client.en.yml

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

Se você reiniciar seu servidor de desenvolvimento, deverá conseguir visitar /admin/plugins e verá nosso link! Se você clicar nele, verá o botão para mostrar nosso tentáculo roxo:

Infelizmente, quando você clica no botão, nada acontece :frowning:

Se você olhar no seu console de desenvolvedor, deverá ver um erro que fornece uma pista do porquê:

Uncaught Error: Nothing handled the action 'showTentacle'

Ah sim, a razão está no nosso template handlebars, estamos dependendo de algumas coisas:

  1. Que quando o usuário clica no botão, showTentacle será chamado.
  2. showTentacle deve definir a propriedade tentacleVisible como true para que a imagem apareça.

Se você não leu os Guias do Ember sobre Controladores agora é um bom momento para fazê-lo, porque implementaremos um controlador para nosso template purple-tentacle que lidará com essa lógica.

Crie o seguinte arquivo:

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;
  }
}

E agora, quando atualizarmos nossa página, clicar no botão mostra nosso personagem animado!

Deixo como um exercício extra para o leitor adicionar um botão que esconde o tentáculo quando clicado :smile:

Se você está tendo problemas para fazer sua versão deste plugin funcionar, eu a enviei para o github.


Mais na série

Parte 1: Noções Básicas de Plugins
Parte 2: Plugin Outlets
Parte 3: Configurações do Site
Parte 4: Configuração do git
Parte 5: Este tópico
Parte 6: Testes de Aceitação
Parte 7: Publique seu plugin


Este documento é controlado por versão - sugira alterações no github.

28 curtidas

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 curtida

@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 curtidas

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 curtida

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 curtidas

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 curtidas

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 curtida

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 curtidas

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 curtida

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 curtidas

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.