A rails engine is a miniature application that provides functionality to its host application. It can be considered a plugin that inherits from Rails::Engine class. You can generate an engine using the rails plugin new
command with the --mountable
option.
Some of the common rails engines that we use every day are:
- Devise: An engine that provides authentication for its parent applications. It allows you to create user models, sessions, passwords, confirmations, registrations, etc. with minimal configuration.
- Spree: An engine that provides an e-commerce platform for its parent applications. It allows you to create products, orders, payments, shipments, promotions, etc. with a modular and customizable architecture1.
- Refinery CMS: An engine that provides a content management system for its parent applications. It allows you to create pages, blogs, images, files, etc. with a user-friendly interface and extensible plugins1.
- Thredded: An engine that provides forum functionality for its parent applications. It allows you to create topics, posts, private messages, notifications, etc. with a responsive and modern design.
To write a rails engine, you need to follow these steps:
- Generate an engine using the command rails plugin new <name> –mountable. This will create a directory structure similar to a Rails application, but with some differences. For example, the engine will have a lib/<name>/engine.rb file that defines the engine class and inherits from Rails::Engine.
- Build features for the engine, such as models, controllers, views, routes, migrations, etc. You can use generators or write them manually. You can also use namespaces to isolate your engine from the host application1.
- Hook the engine into an application by adding it to the Gemfile and running bundle install. You can also mount the engine’s routes in the application’s config/routes.rb file using the mount method.
- Override engine functionality in the application if needed. You can do this by creating files with the same name and path as the ones in the engine, but in the application’s directory. For example, to override a view template, you can create a file in app/views/<engine_name>/<controller_name>/<action_name>.html.erb.
- Test your engine using the test directory in the engine. You can use any testing framework you prefer, such as RSpec or Minitest. You can also test how your engine works with different applications by creating dummy applications in the test/dummy directory.
Some of the benefits of using rails engines are:
- Code reuse: Rails engines allow you to extract reusable code into separate modules, making it easier to share code across multiple projects. This can save time and effort in development, as you can develop and test functionality once and then reuse it in different contexts.
- Modularity: Rails engines allow you to organize your code into smaller and more manageable units, each with its own models, controllers, views, routes, migrations, etc. This can improve the readability, maintainability, and testability of your code, as well as reduce coupling and complexity.
- Isolation: Rails engines allow you to isolate your code from the host application, using namespaces and mountable routes. This can prevent naming conflicts and ensure that the engine’s functionality does not interfere with the application’s functionality.
- Enhancement: Rails engines allow you to enhance your host application with additional functionality, without changing it drastically. You can also override the engine’s functionality in the application if needed, by creating files with the same name and path as the ones in the engine.