1 Tell Rails to load our extensions
Tell Rails to load our extensions by adding some lines into config/application.rb
There should be a Application block where you add this.
config.to_prepare do Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_extension.rb")) do |e| Rails.env.production? ? require(e) : load(e) end end
If you for some reason aren’t using the rails standard for naming your environments, you should use something like this:
config.to_prepare do Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_extension.rb")) do |e| ['env1', 'env2', 'env3'].include?(Rails.env) ? require(e) : load(e) end end
2 Adding a extension
After you set up the loading for our extensions, we can actually start making some. Lets assume you want to add a before_action method to the Alchemy::PagesController. You go into your host app and add a file in app/controllers/alchemy/ called pages_controller_extension.rb. It’s important to add the _extension to the filename so rails will load them.
Into this file you add the following code:
Alchemy::PagesController.module_exec do end
Into this block you can add anything you want your controller to do. As already mentioned, we want to add a before_action to this controller for some show off. It could then look like this:
Alchemy::PagesController.module_exec do before_action :some_method, only: :show end
The last step is adding some_method to your ApplicationController. This is done like you are used to in Rails.