Discourse has a rails plugin generator which generates a basic Discourse plugin scaffolding.
So, I created a "hello world" plugin with this generator using rails g plugin hello-world
and added a bunch of Ruby statements to each .rb
file to write to a file for each of the Rails lifecycle events.
When I did this, I found that the plugin generated by the generator was ignoring the two Rails controllers (the controllers were not logging any actions).
After some debugging, I moved the controllers up one directory from the generated directory structure:
Controller generated by the plugin generator:
app/controllers/hello-world/actions_controller.rb
app/controllers/hello-world/hello_world_controller.rb
New controller location (up one directory):
app/controllers/actions_controller.rb
app/controllers/hello_world_controller.rb
After making this change, the plugin lifecycle logging for the rails controllers works as expected:
2020-08-12 11:01:41 +0700 - HelloWorld plugin.rb after_initalize
2020-08-12 11:01:58 +0700 - class HelloWorldConstraint matches?(request)
2020-08-12 11:02:07 +0700 - class HelloWorldConstraint matches?(request)
2020-08-12 11:02:07 +0700 - class ActionsController index
2020-08-12 11:02:19 +0700 - class HelloWorldConstraint matches?(request)
2020-08-12 11:02:20 +0700 - class HelloWorldConstraint matches?(request)
2020-08-12 11:02:29 +0700 - class HelloWorldConstraint matches?(request)
2020-08-12 11:02:29 +0700 - class ActionsController show
2020-08-12 11:02:36 +0700 - class HelloWorldConstraint matches?(request)
2020-08-12 11:02:43 +0700 - class HelloWorldConstraint matches?(request)
2020-08-12 11:02:43 +0700 - class ActionsController index
In addition, I added a Javascript/Ember controller and template for the main index page, added some HTML and CSS to all the templates and added some JS code to read the cookies:
http://localhost:3000/hello-world/
http://localhost:3000/hello-world/actions/
http://localhost:3000/hello-world/actions/show/
Screen shots of log:
After testing this, I pushed this testing plugin to github for anyone who wishes to observe the Rails lifecycle in a logfile and learn a bit more about Discourse plugins and Rails:
Hopefully, this helps a few inspiring plugin developers.
If anyone has any questions, please ask. I'm still learning step-by-step in my spare time, but making progress and having a lot of fun.