#79 Mountable Engines


Mountable Engines are a great way to extract code into its own namespace and allow the code to be reused in other applications. Other popular gems that are mountable engines are Devise and Doorkeeper.
rails view gem engine 9:23


Terminalrails plugin new books_module --mountable
test/dummy/config/routes.rbRails.application.routes.draw do
  resources :users
  mount BooksModule::Engine => "/books_module", as: :books_engine
  root to: 'users#index'
Terminalcd test/dummy
rails g scaffold users first_name last_name
rake db:migrate

cd ../..
rails g scaffold books name year:date
rake db:migrate

rails g migration add_book_id_to_users book_id:integer:index
rake db:migrate

rake books_module:install:migrations
test/dummy/app/views/users/index.html.erb<%= link_to 'Books', books_engine.books_path %>
app/views/books/index.html<%= link_to 'Users', main_app.users_path %>
lib/books_module.rbrequire "books_module/engine"

module BooksModule
  require_relative 'books_module/model.rb'
lib/books_module/model.rbrequire_relative 'models/user.rb'
lib/books_module/models/user.rbmodule BooksModule
  module UserModel
    extend ActiveSupport::Concern
    included do
      belongs_to :book, class_name: 'BooksModule::Book', optional: true

      def has_favorite_book?
lib/test/dummy/app/models/user.rbclass User < ApplicationRecord
  # belongs_to :book, class_name: 'BooksModule::Book', optional: true

  # def has_favorite_book?
  #   book.present?
  # end
  include BooksModule::UserModel
app/models/books_module/book.rbmodule BooksModule
  class Book < ApplicationRecord
    has_many :users
test/dummy/app/views/users/_form.html.erb    <%= form.label :book_id %>
    <%= form.collection_select :book_id, BooksModule::Book.all, :id, :name, include_blank: true %>
test/dummy/app/controllers/uesrs_controller.rb    private
    def user_params
      params.require(:user).permit(:first_name, :last_name, :book_id)
yewness said over 2 years ago on Mountable Engines :

Thanks for having this episode up! I am wondering, what could be the drawbacks of using engines for leave and attendance modules in HR application? Is that the right approach to start with in the first place?

kobaltz PRO said over 2 years ago on Mountable Engines :

One of the drawbacks is the added complexity of having an engine. If your main app is really complicated and has a lot of dependencies, it might be worth scripting a task to copy and modify the main app into the dummy app. Another drawback that I found a few years ago were that the ActionMailer Previews for the engine had to be within the main application. It was an annoyance, but got by with it.

Depending on the size of your HR application, it might be a good thing to separate the TLM (Time & Labor Management) stuff. If you added an onboarding module as well, you could make this into an engine as well. 

Basically, If there is a group of users who would not have the need for a particular grouping of features, because they are either an additional cost or premium feature, then I would build this kind of functionality into a separate engine.

Login to Comment