rails engine
Engines & plugins
Common: [1]lib dir, [2]generated using rails plugin new
Difference: [1]engine is considered a "full plugin" by Rails, [2]Engines can be isolated from host apps.
Generating an engine
rails plugin new blorgh --mountable
--full: [1] appdir tree, [2]config/routes.rb, [3] lib/your_engine/engine.rb(similar to config/application.rb)
--mountable: --full + [1]Asset manifest files (application.js and application.css), [2]namespaced ApplicationController, ApplicationHelper, layout view template, config/routes.rb , [3] Namespace tolib/your_engine/engine.rb, [4] mount the engine inside the dummy test
Base class for the engine
Base class = lib/blorgh/engine.rb
module Blorgh
class Engine < ::Rails::Engine # inheriting from the Rails::Engine class, this will let rails know how to deal with this.
isolate_namespace Blorgh
end
endisolated_namespaceIsolates the controllers, models, routes, etc into own namespace, away from similar components inside the app. Without this, the engine's helpers would be included in an app's controllers.
Create Article model/controller: YourEngine::Article, yourenginearticle, YourEngine::ArticlesController,
When Constants aren't Missed
Ambiguous can be prevent by
Require in host's gem file
In host app's gemfile: gem 'your_engine', path: 'engines/your_engine'
This will require lib/your_engine.rb.
This file requires lib/your_engine/engine.rb, and defines a base module called YourEngine.
Last updated
Was this helpful?