Now it makes a lot of sense. I didn’t run into issues coz I use to do a server restart even when working on discourse core instead of reloading the code.
What I did recently, not yet a super expert like many of you in building a Rails apps, is to plan which configuration variables should reload without restarting Rails, and I move that code to the ApplicationController.
I am sure there are better ways to do this, but since this particular Rails implementation is a back-office app and performance is not an issue:
class ApplicationController < ActionController::Base
before_action :set_site_settings
private
def set_site_settings
@use_custom_date_format = Sitesetting.where(name: "custom_date_format").pluck(:value).last
end
end
I will be happy when I find a better way to do this!
However, moving this out of the Rails initializers means the user can change this site setting easily because it is now in the DB using a basic Rails MVC CRUD scaffold.
Since SQL caching in Rails does not work outside the scope of an action, someday I need to learn how to move this to cache and make sure that the cache clears when the Rails controller processes the action (for example, save a new value in the site settings controller, etc.)
Anyway, this client Rails app (back-office only) is not for high performance use, so adding the query to the application controller works OK and saves me from repeating this code in each controller in the project where that site setting is required.
Hey @fzngagan, I am certainly not a “rubyist” and have a lot less experience with Rails than most Discourse plugin developers; but having said that: maybe you could get by with something like this in the future if you need to reload files in your plugin without restarting the application in production (or development):
after_initialize do
# change the following to your controller of choice
# or use the application controller if you need to
ApplicationController.class_eval do
before_action :do_my_stuff
def do_my_stuff
load File.open(FAIZAANS_FAV_FILE)
end
end
end
This will reload the files as expected
I am currently using this on a plugin like so, and it works as expected:
after_initialize do
Admin::AdminController.class_eval do
before_action :do_neo_plugin_info
def do_neo_plugin_info
load File.open(PLUGIN_LOGIC)
end
end
end
I am currently using this code in a plugin I have been working on from time-to-time which displays the names of the containers which is parsed from ENV["DATA_NAME"
] and also the disk space parsed from some system code using df
and grep
.
In our admin views:
As mentioned, I am not a Rubyist by any means; but this method works for me.
I looked at the plugin code in instance.rb
and after some different trials and errors, I decided to go with the class_eval
code above, and it might not be the preferred way of doing things, but it certainly works for me.
For example, if I reload the page after deleting a lot of Discourse backups, the disk space indicator changes as expected.