I want to make a plugin that needs its own tables. So its a good idea to have migration and model generators, because manually copying them from the main app dir will lead to errors and is just not very convenient. So I started making my own generators. I started with the migration generator:
https://github.com/spirobel/discourse/commit/3ba8190286b5c40eeace1b58238cbf5075db597e
I could move on now and do the same for models and controllers. But now I started to read this Getting Started with Engines — Ruby on Rails Guides and now it seems to me that all of this might be a duplication of efforts. Why does the discourse plugin generator diverge so much from the rails plugin generator?
The rails plugin generator makes a plugin specific bin/rails that enables the plugin specific migration and model generators. It would be nice to have something like this as well. I tried to make something like this as well but I could not get it to work, so I went the other route and just started creating these special snowflake generators. If I understood better what the differences between the rails and the discourse way of making plugins is, I might be able to go this other route which might save time. Maybe someone can expand on this. Why are discourse and rails plugins something different and why do they not build on each other?
AI-generated summary
The discussion revolves around the differences between Rails plugins and Discourse plugins, focusing on the challenges of creating plugins with custom tables. spirobel questions why Discourse plugins diverge from Rails plugin generators, especially since Discourse plugins require specific structures and can extend Ember code. They express frustration over the lack of clear documentation for creating migrations and models, citing the poll plugin as an example that uses custom tables but lacks a documented generator. fzngagan advises that new tables should only be created when necessary due to Discourse’s existing ~150 tables, though they acknowledge the potential for more creative plugins. pfaffman recommends starting with existing tables like post custom fields and suggests reading the plugin development guides. spirobel counters that they need tables for their plugin and finds the reluctance to create them puzzling, especially with namespacing. Mittineague suggests that if the plugin is for personal use, a browser extension or desktop app might be more appropriate. merefield shares a related discussion on adding JSONB columns for custom fields.
Rails plugins are different from Discourse plugins due to many reasons. Discourse plugins need to have a specific folder structure, they can extend/override the ember code of the core discourse etc. I think the rails side of a discourse plugin would be closer to a generic rails plugin but they aren’t the same things.
Moreover, one should only create new tables if they’re really needed. Discourse already has ~150 tables and its not too often that your job isn’t done using one or a couple of them.
But, moving forward, I personally would wish and like to see that happen. It would mean people are building huge and more exciting plugins and getting more creative with what could be done with Discourse.
I need them. Even basic functionality needs tables. I think its really annoying that there is no clear documented way on how to do this. For example the poll plugin uses its own tables, but I cant find the generator for how to make them. discourse/plugins/poll at main · discourse/discourse · GitHub
Another benefit from documenting is that there will be conventions on table/migration namespacing. (I saw some really hacky solutions recommended here in the forums on how to add migrations to a plugin)
Nice! So which way to go? moving closer to the rails plugin generator with own bin/rails in the plugin folder, or own versions for all the generators (migrations/models/controllers) ?
You need to read the plugin development howto topics here. For most plugins you can use existing tables (like post custom field). There are times that you need more tables, but you’d probably do well to start with something less difficult to start.
I read the beginners plugin guide and this . Is there something else? I also looked at the code of some plugins. I just know that I need tables. And there are other plugins as well that use their own tables. Why is there this obsession with not creating new tables? Is there a big cost involved with creating new tables? Especially if they are namespaced with the plugin name at the beginning I really dont see the downside.
https://meta.discourse.org/t/creating-routes-in-discourse-and-showing-data/48827/19?u=fzngagan
These ones here. I highly recommend these ones hands on before making any comment about anything related to dev. Not to say you might not need custom tables ever but these are must anyway.
this thing is just a basic key value store. I cant store relations between tables there. I like SQL. And I need to use it for the plugin I want to create.
I can understand this problem applies to people that want to create plugins that “non technical people” can install and uninstall. I am not one of those people. So I would be glad if we could go back to the topic.
If the intent is to create something only you yourself will be using, IMHO it would likely be better to write a browser extension or a desktop app instead of a plugin. Any reason this must be a plugin?
Here’s an interesting related discussion: