rails engines - a presentation for the 22nd athens ruby meetup
TRANSCRIPT
![Page 1: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/1.jpg)
Rails EnginesDimitris Zorbas - Athens Ruby Meetup#22
github.com/zorbash @_zorbash
![Page 2: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/2.jpg)
What is an Engine?
Engines can be considered miniature applications that provide functionality to their host applications
![Page 3: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/3.jpg)
An Engineis_a?
↳ Gem
↳ Plugin
↳ Railtie
![Page 4: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/4.jpg)
Engine < RailtieRailtie
require 'rails/railtie'require 'rails/engine/railties'
module Rails class Engine < Railtie autoload :Configuration, "rails/engine/configuration"
![Page 5: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/5.jpg)
Engine < Railtie
Rails::Engine.initializers.map(&:name)# =>
[:set_load_path, :set_autoload_paths, :add_routing_paths, :add_locales, :add_view_paths, :load_environment_config, :append_assets_path, :prepend_helpers_path, :load_config_initializers, :engined_blank_point]
![Page 6: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/6.jpg)
Application < EngineEngine
require 'rails/engine'
module Rails class Application < Engine autoload :Bootstrap, "rails/application/bootstrap" autoload :Configuration, "rails/application/configuration"
![Page 7: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/7.jpg)
Popular Engines⍏ devise⍏ refinerycms⍏ spree⍏ kaminari⍏ doorkeeper
![Page 8: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/8.jpg)
Why create an Engine?⍏ Separation of concerns⍏ Tested independently Can be reused
![Page 9: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/9.jpg)
Obligatory GIF
![Page 10: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/10.jpg)
Caveats of an Engine? Slow prototyping velocity Dependency management More repositories Requires host app for dev / test
![Page 11: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/11.jpg)
Adequate Monolith™
Using Engines for sane application growth
![Page 12: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/12.jpg)
Adequate Monolith™Step 1: Get an Architectural overview
Monolith
You
![Page 13: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/13.jpg)
Adequate Monolith™Step 2: Extract smaller components
![Page 14: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/14.jpg)
Adequate Monolith™Step 3: Compose
![Page 15: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/15.jpg)
Engineering.initialize!Creating an Engine
rails plugin new ENGINE_NAME --full
![Page 16: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/16.jpg)
Engine StructureCreating an Engine▾ app/ ▸ {assets,controllers,helpers,mailers,models,views}/▾ config/ routes.rb▾ lib/ ▸ engine_name/ ▸ tasks/ engine_name.rb Gemfile Rakefile engine_name.gemspec
![Page 17: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/17.jpg)
Engine Configurationmodule Chat class Engine < ::Rails::Engine isolate_namespace Chat # !important initializer 'chat.settings' do |app| app.config.x.chat = OpenStruct.new(room: 'newbies') end endend
Regular config/initilializers/* can also be used for configuration
![Page 18: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/18.jpg)
Engine RoutesSetting the routesChat::Engine.routes.draw do root 'rooms#index'
resources :rooms do resources :messages, only: %i[create] endend
![Page 19: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/19.jpg)
Engine RoutesMounting from main appRails.application.routes.draw do mount Chat::Engine, at: '/chat'
# other routesend
main_app.resource_path
![Page 20: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/20.jpg)
Engine DevelopmentStarting a consolecd ./spec/dummy./bin/rails console
Bundling on the host appgem 'engine_name', path: '../path/to/engine'
![Page 21: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/21.jpg)
Engines Best PracticesControllers
class Chat::RoomsController < ApplicationController # default inheritance Chat::ApplicationController # actions..end
![Page 22: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/22.jpg)
Engines Best PracticesModels
Chat.user_class = 'User' # configurable
class Chat::Message < ActiveRecord::Base belongs_to :user, class_name: Chat.user_classend
![Page 23: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/23.jpg)
Engines on ProductionCase:
![Page 24: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/24.jpg)
![Page 25: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/25.jpg)
Engines on ProductionCase:
![Page 26: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/26.jpg)
Resourceshttp://guides.rubyonrails.org/engines.htmlhttp://api.rubyonrails.org/classes/Rails/Engine.html
![Page 27: Rails Engines - A presentation for the 22nd Athens Ruby Meetup](https://reader031.vdocuments.net/reader031/viewer/2022030214/588b1cf21a28ab78398b58b9/html5/thumbnails/27.jpg)
Questions?
__END__