Creating a Plugin for Discourse

Discourse Meta

If you want to contribute to Discourse there is the awesome Discourse Meta Community to help you. So in order to avoid starting without any tipps and hints, just create a new post on Discourse Meta and describe your project and ask for hints.

Example Plugin

There are already plugins existing for Discourse. For example we used the dropbox-plugin to understand the structure of a plugin and how it is built. The best way to start is to learn how to install plugins first. There is a great tutorial on that on Discourse Meta as well. You might try that first.

Setting up Github

In order to create your own plugin, you will need to create your own folder inside the discourse/plugins folder and give it a good name. To keep track of your own progress there you can navigate (in the Terminal) into your plugin folder and initialize it as a new github repository with git init and push it to a new github repository. This is also good to share your current code with others. If you want, you can also create branches for working on different issues in your plugin and only commit things to the master that are actually working.

The Structure of a Backup Plugin

In Discourse, the Backup is a defined model called backup.rb. Read through it in order to understand how the backup in Discourse actually works. By default Discourse is doing the backup to s3. But there is a hook method inside the class that makes it possible for the plugin to interfere and overwrite the method. We followed Falco's way and did it like the dropbox-plugin by using the method .class_eval on the class Backup. This all happens inside the plugin-folder in a file called plugin.rb.
If you want to know more about how the Discourse app is handling plugins, there is a lot of internal file junlge in Discourse to explore. There are files calles instance.rb and site_setting.rb and many more, that play a role in handling the plugins. 
The actual backup is wrapped in a Job, that we (after Falco) called a synchronizer. The synchronizer is where the actual API code from the provider goes. For us that was Google Drive. In order to make everything work, we first had to understand the API and also install the google_drive gem. After that we also had to install other depending gems, because the discourse-bundler doesn't work inside the plugin. This is a bit of manual work, but it's also neccessary.

Next Thing to do

It is a good idea to write some specs for your plugin. But this is a bigger chapter and will be another whole blog post next time. Until then: happy coding!

Kommentare

Beliebte Posts