A plugin for debugging the Discourse rails plugin generator

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.

1 Like

Just updated this little "learning plugin" by adding some cookies generated by the Rails controllers.

Showing how we can write cookies with Rails and read with Javascript/Ember:

Reference:

https://api.rubyonrails.org/v5.1.7/classes/ActionDispatch/Cookies.html

Version 0.13

Roughed in some code to dump the Rails process ENV vars via a cookie.

Did this for "no particular good reason" except to learn how to dump the process ENV in Rails to a cookie and then read the cookie in an Ember controller, parse and display the results.

Well, color me confused.

With your hello-world I get

expected file /home/pfaffman/src/hello-world-rails-plugin-gen/app/controllers/hello_world_controller.rb to define constant HelloWorldController, but didn't

when I hit /hello-world, and when I move them back into controllers/hello-world, it works as expected.

But in my plugin, when I hit /mypath I get no errors, but just a blank page (still has expected headers at top), so it seems it's not pulling in my index.hbs.

More confusing still, is that with both your plugin and mine, my index.hbs is getting loaded when I hit /hello-word, but NOT when I hit /my-path.

It's a twisty maze of passages, I tell you.

1 Like

Hey Jay,

Welcome and thanks for posting.

Maybe it is an "OS thing"?

As we know there are a lot of quirks getting Ruby to run on macOS.

I am guessing you are not using macOS for dev and I am?

Or maybe it is the fact that on my machine I moved the plugin to the hello-world directory?

In my dev setup, I did not change the directory to the name of the plugin I used for github:

hello-world-rails-plugin-gen


Because I did not want such a simple name like hello-world for this dev debug plugin.

Will test later today and post back ...

Note: Just woke up and fired this up again on dev after a total computer shutdown while sleeping, as it is working OK (yea!)

Well, and then the index.hbs from my plugin was displaying in the /hello-world path and not in my /whatever-I-called-it path. That was when I gave up yesterday. :expressionless:

I'll start again on this later today.

FWIW, the notebook tutorial works well now and can be used as a kind of plugin template, for anyone having issues with the Discourse rails plugin generator.

I am currently using both of these:

  • the notebook tutorial code
  • the Discourse rails plugin generator

To be honest, the notebook tutorial code works very well after the latest PR.

The rails plugin generator works "OK"; but requires more "tweaking".