Desarrollando Plugins de Discourse - Parte 5 - Añadir una interfaz de administrador

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


A veces, la configuración del sitio no es suficiente como interfaz de administración para que tu complemento funcione como deseas. Por ejemplo, si instalas el complemento discourse-akismet, es posible que hayas notado que agrega un elemento de navegación a la sección de complementos de administración en tu Discourse:

En este tutorial te mostraremos cómo agregar una interfaz de administración para tu complemento. Llamaré a mi complemento purple-tentacle, en honor a uno de mis juegos de computadora favoritos. ¡En serio, me encanta ese juego!

Configuración de la Ruta de Administración

Empecemos agregando un plugin.rb como lo hemos hecho en partes anteriores del tutorial.

plugin.rb

# name: purple-tentacle
# about: Un complemento de ejemplo que muestra cómo agregar una ruta de complemento
# 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

La línea add_admin_route le dice a Discourse que este complemento necesitará un enlace en la página /admin/plugins. Su título será purple_tentacle.title de nuestro archivo de traducciones i18n y enlazará a la ruta purple-tentacle.

Las líneas debajo configuran el mapeo del lado del servidor de rutas para nuestro complemento. Una suposición que hace Discourse es que casi todas las rutas en el front end tienen una ruta del lado del servidor que proporciona datos. Para este complemento de ejemplo, en realidad no necesitamos ningún dato del back end, pero necesitamos decirle a Discourse que sirva algo en caso de que el usuario visite /admin/plugins/purple-tentacle directamente. Esta línea simplemente le dice: ‘¡oye, si el usuario visita esa URL directamente en el lado del servidor, sirve el contenido predeterminado de los complementos!’.

(Si esto es confuso, no te preocupes demasiado, volveremos a ello en un tutorial futuro cuando manejemos las acciones del lado del servidor).

A continuación, agregaremos una plantilla que se mostrará cuando el usuario visite la ruta /admin/plugins/purple-tentacle. Será solo un botón que mostrará un gif animado de un tentáculo morado cuando el usuario haga clic en un botón:

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>

Si has aprendido los conceptos básicos de handlebars, la plantilla debería ser bastante sencilla de entender. El <DButton /> es un componente en Discourse que usamos para mostrar un botón con una etiqueta e icono.

Para conectar nuestra nueva plantilla, necesitamos crear un mapa de rutas:

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

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

Un mapa de rutas es algo que agregamos a Discourse para que los complementos pudieran agregar rutas a la aplicación ember. La sintaxis dentro de map() es muy similar al enrutador de Ember. En este caso, nuestro mapa de rutas es muy simple, solo declara una ruta llamada purple-tentacle bajo /admin/plugins.

Finalmente, agreguemos nuestras cadenas de traducción:

config/locales/client.en.yml

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

Si reinicias tu servidor de desarrollo, deberías poder visitar /admin/plugins y ver nuestro enlace. Si haces clic en él, verás el botón para mostrar nuestro tentáculo morado:

Desafortunadamente, cuando haces clic en el botón, no sucede nada :frowning:

Si miras en tu consola de desarrollador, deberías ver un error que da una pista de por qué es:

Uncaught Error: Nothing handled the action 'showTentacle'

Ah, sí, la razón está en nuestra plantilla handlebars que depende de un par de cosas:

  1. Que cuando el usuario haga clic en el botón, se llame a showTentacle.
  2. showTentacle debería establecer la propiedad tentacleVisible en true para que la imagen aparezca.

Si no has leído las Guías de Ember sobre Controladores ahora es un buen momento para hacerlo, porque implementaremos un controlador para nuestra plantilla purple-tentacle que manejará esta lógica.

Crea el siguiente archivo:

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

¡Y ahora cuando actualizamos nuestra página, hacer clic en el botón muestra nuestro personaje animado!

Dejo como ejercicio adicional para el lector agregar un botón que oculte el tentáculo cuando se haga clic :smile:

Si tienes problemas para hacer funcionar tu versión de este complemento, lo he subido a github.


Más en la serie

Parte 1: Conceptos básicos de complementos
Parte 2: Salidas de complementos
Parte 3: Configuración del sitio
Parte 4: Configuración de git
Parte 5: Este tema
Parte 6: Pruebas de aceptación
Parte 7: Publica tu complemento


Este documento está controlado por versiones: sugiere cambios en github.

28 Me gusta

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 me gusta

@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 Me gusta

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 me gusta

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 Me gusta

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 Me gusta

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 me gusta

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 Me gusta

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 me gusta

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 Me gusta

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.