Engines

39
COWBOYS & ENGINES Fernand Galiana Tuesday, September 17, 13

description

An intro to rails engines with practice labs.

Transcript of Engines

Page 1: Engines

COWBOYS & ENGINES

Fernand Galiana

Tuesday, September 17, 13

Page 2: Engines

HISTORY

0.14.2 ~ 2005

2.3 ~ 2009 -- Engine support

3.0 ~ 2010 -- Yehuda and crew

Tuesday, September 17, 13

Page 3: Engines

SURPRISE?

> rails c> Rails::Application.ancestors=> [Rails::Application, Rails::Engine,...]

Tuesday, September 17, 13

Page 4: Engines

IMPETUS?

Mini rails apps [MVC|Assets|lib]

Divide conquer

Reusable components

Decoupling

Performance

Tuesday, September 17, 13

Page 5: Engines

ENGINE TYPES

full

mountable

Tuesday, September 17, 13

Page 6: Engines

CREATE!

> rails plugin new engines/blee [opts]

Tuesday, September 17, 13

Page 7: Engines

NOTABLES...

--full

--mountable

-T

-B

-d [mysql|pg|sqlite3|etc...]

-j [jquery|coffeescript]

-dummy_path

Tuesday, September 17, 13

Page 8: Engines

CREATE!

> rails plugin new engines/cowboy \ --mountable -T -B\ --dummy-path=spec/dummy \ -j coffeescript -d mysql

Tuesday, September 17, 13

Page 9: Engines

TEH BROG!

You rock, bro!

Tuesday, September 17, 13

Page 10: Engines

LAB I

git clone https://github.com/derailed/ng_training

git checkout -b lab_1

follow the README!

Tuesday, September 17, 13

Page 11: Engines

ANATOMY

Tuesday, September 17, 13

Page 12: Engines

STRUCTURE

cowboy_engine o app o bin o config o lib o spec

Tuesday, September 17, 13

Page 13: Engines

STRUCTURE

cowboy_engine o app

o assetso controllerso helperso modelso views

o bin o ...

Tuesday, September 17, 13

Page 14: Engines

STRUCTURE

cowboy_engine o app

o controllerso cowboy_engine

o cowboys_controller.rbo ...

o bin o ...

Tuesday, September 17, 13

Page 15: Engines

ENGINE GEMSPEC

Gem::Specification.new do |s| s.name = "pilgrim" s.version = Pilgrim::VERSION s.authors = ["Fernand"] s.email = ["[email protected]"] s.homepage = "http://pilgrim" s.summary = "Pilgrim’s engine" s.description = "Pilgrim’s engine"

s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]

s.add_dependency 'rails' , '~> 4.0.0' s.add_dependency 'bootstrap-sass' , '~> 2.3.2.2' s.add_dependency 'font-awesome-sass-rails', '~> 3.0.2.2' s.add_development_dependency "mysql2" , '~> 0.3.13' s.add_development_dependency 'rspec-rails' , '~> 2.14.0' end

Tuesday, September 17, 13

Page 16: Engines

THE ENGINE

module Cowboy class Engine < ::Rails::Engine isolate_namespace config.autoload_paths += [File.join( self.root, %w[lib blee] )] endend

Tuesday, September 17, 13

Page 17: Engines

BUSINESS AS USUAL

rails g [model|controller|migration]

custom generators

engine initializers

Tuesday, September 17, 13

Page 18: Engines

GEMFILE

gem ‘cowboy’, path: ‘engines/cowboy’

my_app/Gemfile

Tuesday, September 17, 13

Page 19: Engines

CONVENIENCE

Cowboy::Engine.root

Cowboy::Engine.helpers

Cowboy::Engine.routes.url_helpers

Tuesday, September 17, 13

Page 20: Engines

MIGRATIONS

Tuesday, September 17, 13

Page 21: Engines

MIGRATIONS

> rake cowboy:install:migrations

Tuesday, September 17, 13

Page 22: Engines

MIGRATIONS

rake db:migrate SCOPE=Cowboy

Tuesday, September 17, 13

Page 23: Engines

MONKEYMIGS!

module Cowboy class Engine < ::Rails::Engine isolate_namespace Cowboy initializer :append_migrations do |app| next if app.root.to_s.match self.root.to_s config.paths["db/migrate"].expanded.each do |expanded_path| app.config.paths["db/migrate"] << expanded_path end end endend

Tuesday, September 17, 13

Page 24: Engines

ISOLATED MODELS

table names expects engine_name_table_name

self.table_name = :guns

or

table_name_prefix = “”

Tuesday, September 17, 13

Page 25: Engines

ROUTING

Tuesday, September 17, 13

Page 26: Engines

engines/cowboy/config/routes.rb

ENGINE ROUTES

Cowboy::Engine.routes.draw do resources :guns, only: [:create] resources :belt do resources :ammos endend

Tuesday, September 17, 13

Page 27: Engines

APP ROUTES

Rango::Application.routes.draw do root 'welcome#index'

mount Cowboy::Engine => '/pilgrim', as: :cowboy_ng resources :jenkins do resources :bullets, controller: 'cowboy/bullets' endend

rango/config/routes.rb

Tuesday, September 17, 13

Page 28: Engines

CONTEXT MATTERS!

Cowboy engine contextguns_path => /gunsmain_app.welcome_path => /welcome

Main Application contextcowboy_ng.guns_path => /pilgrims/gunswelcome_path => /welcome

Tuesday, September 17, 13

Page 29: Engines

my_app/controllers/guns_controller.rb

HELPERS

class GunsController < ApplicationController helper Cowboy::BulletsHelper

...end

Tuesday, September 17, 13

Page 30: Engines

ENGINE ASSETS

stylesheet_link_tag cowboy/guns.css

javascript_include_tag cowboy/guns.js

or

*=require cowboy/guns

//=require cowboy/guns

Tuesday, September 17, 13

Page 31: Engines

PARTIALS

render gun => render partial: ‘cowboy/...’, object: gun

render :partial ‘fred’ => render :partial ‘cowboy/fred’

Tuesday, September 17, 13

Page 32: Engines

LOADING ORDER

config.railties_order = [Cowboy::Engine, :main_app, :all]

Tuesday, September 17, 13

Page 33: Engines

TESTING

Tuesday, September 17, 13

Page 34: Engines

TESTING (RSPEC)

spec/dummy

Tuesday, September 17, 13

Page 35: Engines

TESTING (RSPEC)

config.include Dummy::Application.routes.url_helpers

use_route :cowboy

Tuesday, September 17, 13

Page 36: Engines

LAB II

Scenario: Other models in the system need to be commentable. Spin out this functionality as a new ‘social’ engine

Tuesday, September 17, 13

Page 37: Engines

CHEAT

> rails plugin new engines/social -T --mountable \ -j coffeescript -B -d mysql \ --dummy-path=spec/dummy

Tuesday, September 17, 13

Page 38: Engines

LAB III

Scenario: PM wants a reporting admin ui to track comments. In light of this new feature commenting behavior needs to be shared across several engines.

Refactor social engine to depend on the new common engine

Tuesday, September 17, 13

Page 39: Engines

Q&A

Get a rope?

Tuesday, September 17, 13