rails engines - a presentation for the 22nd athens ruby meetup

27
Rails Engines Dimitris Zorbas - Athens Ruby Meetup#22 github.com/zorbash @_zorbash

Upload: skroutz-sa

Post on 27-Jan-2017

414 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

Rails EnginesDimitris Zorbas - Athens Ruby Meetup#22

github.com/zorbash @_zorbash

Page 2: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

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

An Engineis_a?

↳ Gem

↳ Plugin

↳ Railtie

Page 4: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

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

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

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

Popular Engines⍏ devise⍏ refinerycms⍏ spree⍏ kaminari⍏ doorkeeper

Page 8: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

Why create an Engine?⍏ Separation of concerns⍏ Tested independently Can be reused

Page 9: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

Obligatory GIF

Page 10: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

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

Adequate Monolith™

Using Engines for sane application growth

Page 12: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

Adequate Monolith™Step 1: Get an Architectural overview

Monolith

You

Page 13: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

Adequate Monolith™Step 2: Extract smaller components

Page 14: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

Adequate Monolith™Step 3: Compose

Page 15: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

Engineering.initialize!Creating an Engine

rails plugin new ENGINE_NAME --full

Page 16: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

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

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

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

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

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

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

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

Engines on ProductionCase:

Page 24: Rails Engines - A presentation for the 22nd Athens Ruby Meetup
Page 25: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

Engines on ProductionCase:

Page 27: Rails Engines - A presentation for the 22nd Athens Ruby Meetup

Questions?

__END__