Mountable Engines

#79 Mountable Engines
5/7/2017

Summary

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.
7
rails view gem engine 9:23 min

Summary

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'
end
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'
end
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?
        book.present?
      end
    end
  end
end
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
end
app/models/books_module/book.rbmodule BooksModule
  class Book < ApplicationRecord
    has_many :users
  end
end
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)
    end
14829481?v=3&s=64
yewness said 4 months ago:

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?

635114?v=3&s=64
kobaltz said 4 months ago:

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