Engines can be considered as a small applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the
Rails::Application class inheriting a lot of its behavior from Rails::Engine.
Therefore, engines and applications can be thought of almost the same thing, just with subtle differences, as you'll see throughout this guide. Engines and applications also share a common structure.Engines are also closely related to plugins. The two share a common
lib directory structure, and are both generated using the rails plugin new generator. The difference is that an engine is considered a "full plugin" by Rails (as indicated by the --full option that's passed to the generator command).. An engine can be a plugin, and a plugin can be an engine.
1). Getting help: For help regarding engine run the following command:
rails plugin --help
2).Generating an engine: To generate an engine, you will need to run the plugin generator and pass it options as appropriate to the need.run the following command in terminal:
rails plugin new engine_name --mountable
The
--mountable option tells the generator that you want to create a "mountable" and namespace-isolated engine. This generator will provide the same skeleton structure as would the --full option, and will add:
Asset manifest files (
application.js and application.css
A namespaced
ApplicationController stub
A namespaced
ApplicationHelper stub
A layout view template for the engine
Namespace isolation to
config/routes.rb
Note: Don't forget to run bundle install command to install all dependencies.
Mountable engine is as like full rails application you can generate models,controller and do all the stuff what you do in a rails application.
3).Setup engine in rails app:
for install all migrations:
rake engine_name:install:migrations
If you have multiple engines then run:
rake railties:install:migrations
4).Running migration: if you have only one enigne then run
rake db:migrate
If you have multiple engines then run:
rake db:migrate SCOPE=engine_name
5).Using in application:
a).Add following line in you application's gem file
gem 'engine_name' , path :'path_of_your_enigne'
path_of_your_enigne is the full path where your engine_name.gemspec is present.
b).Add this line in your routes.rb file:
mount engine_name::Engine => "/mount_name"
mount_name can be anything that you want to give to your engine's path usually it is the engine_name.
6).Communicating between rails app and engine:
a). if you want to access engine resource in your rails app then:
routes: engine_name.path_to_your_resource
Model: EngineName::ModelName
b). if you want to access main rails application from engine then:
routes: main_app.path_to_your_resource
No comments:
Post a Comment