Rails engines extending functionality
require MyEngine::Engine.root.join(‘app’, ‘models’, ‘my_engine’, ‘my_model’) before the model class definition in your application.
require MyEngine::Engine.root.join(‘app’, ‘models’, ‘my_engine’, ‘my_model’) before the model class definition in your application.
I couldn’t get any of the above to work for me so I dug in the Rails code a bit and came up with this: New file: config/initializers/reload_lib.rb if Rails.env == “development” lib_reloader = ActiveSupport::FileUpdateChecker.new(Dir[“lib/**/*”]) do Rails.application.reload_routes! # or do something better here end # For Rails 5.1+ ActiveSupport::Reloader.to_prepare do lib_reloader.execute_if_updated end # For Rails … Read more
By design, classes in a Rails::Engine are supposed to be scoped to the engine. That way they don’t introduce strange bugs by accidentally stomping all over code loaded in the main app or by other engines. Monkeypatching ActiveSupport::Dependencies to mix engines across-the-board is a really bad workaround. Just use a Rails::Railtie, instead. They have all … Read more
Include them in your gemfile and run bundle install. Then require them in your lib/<your_engine>/engine.rb file. Don’t forget to require rubygems require ‘rubygems’ require ‘paperclip’ require ‘jquery-rails’ require ‘rails3-jquery-autocomplete’ require ‘remotipart’ require ‘cancan’ Then in your host app (The app where you included your gem) run bundle install/ bundle update (bundle update did the trick … Read more
I have noticed the following: Full Engine With a full engine, the parent application inherits the routes from the engine. It is not necessary to specify anything in parent_app/config/routes.rb. Specifying the gem in Gemfile is enough for the parent app to inherit the models, routes etc. The engine routes are specified as: # my_engine/config/routes.rb Rails.application.routes.draw … Read more