0
COWBOYS & ENGINES
Fernand Galiana
Tuesday, September 17, 13
HISTORY
0.14.2 ~ 2005
2.3 ~ 2009 -- Engine support
3.0 ~ 2010 --Yehuda and crew
Tuesday, September 17, 13
SURPRISE?
> rails c
> Rails::Application.ancestors
=> [Rails::Application, Rails::Engine,...]
Tuesday, September 17, 13
IMPETUS?
Mini rails apps [MVC|Assets|lib]
Divide conquer
Reusable components
Decoupling
Performance
Tuesday, September 17,...
ENGINE TYPES
full
mountable
Tuesday, September 17, 13
CREATE!
> rails plugin new engines/blee [opts]
Tuesday, September 17, 13
NOTABLES...
--full
--mountable
-T
-B
-d [mysql|pg|sqlite3|etc...]
-j [jquery|coffeescript]
-dummy_path
Tuesday, September ...
CREATE!
> rails plugin new engines/cowboy 
--mountable -T -B
--dummy-path=spec/dummy 
-j coffeescript -d mysql
Tuesday, Se...
TEH BROG!
You rock, bro!
Tuesday, September 17, 13
LAB I
git clone https://github.com/derailed/ng_training
git checkout -b lab_1
follow the README!
Tuesday, September 17, 13
ANATOMY
Tuesday, September 17, 13
STRUCTURE
cowboy_engine
o app
o bin
o config
o lib
o spec
Tuesday, September 17, 13
STRUCTURE
cowboy_engine
o app
o assets
o controllers
o helpers
o models
o views
o bin
o ...
Tuesday, September 17, 13
STRUCTURE
cowboy_engine
o app
o controllers
o cowboy_engine
o cowboys_controller.rb
o ...
o bin
o ...
Tuesday, September 1...
ENGINE GEMSPEC
Gem::Specification.new do |s|
s.name = "pilgrim"
s.version = Pilgrim::VERSION
s.authors = ["Fernand"]
s.ema...
THE ENGINE
module Cowboy
class Engine < ::Rails::Engine
isolate_namespace
config.autoload_paths += [File.join( self.root, ...
BUSINESS AS USUAL
rails g [model|controller|migration]
custom generators
engine initializers
Tuesday, September 17, 13
GEMFILE
gem ‘cowboy’, path:‘engines/cowboy’
my_app/Gemfile
Tuesday, September 17, 13
CONVENIENCE
Cowboy::Engine.root
Cowboy::Engine.helpers
Cowboy::Engine.routes.url_helpers
Tuesday, September 17, 13
MIGRATIONS
Tuesday, September 17, 13
MIGRATIONS
> rake cowboy:install:migrations
Tuesday, September 17, 13
MIGRATIONS
rake db:migrate SCOPE=Cowboy
Tuesday, September 17, 13
MONKEYMIGS!
module Cowboy
class Engine < ::Rails::Engine
isolate_namespace Cowboy
initializer :append_migrations do |app|
...
ISOLATED MODELS
table names expects engine_name_table_name
self.table_name = :guns
or
table_name_prefix = “”
Tuesday, Septe...
ROUTING
Tuesday, September 17, 13
engines/cowboy/config/routes.rb
ENGINE ROUTES
Cowboy::Engine.routes.draw do
resources :guns, only: [:create]
resources :bel...
APP ROUTES
Rango::Application.routes.draw do
root 'welcome#index'
mount Cowboy::Engine => '/pilgrim', as: :cowboy_ng
resou...
CONTEXT MATTERS!
Cowboy engine context
guns_path => /guns
main_app.welcome_path => /welcome
Main Application context
cowbo...
my_app/controllers/guns_controller.rb
HELPERS
class GunsController < ApplicationController
helper Cowboy::BulletsHelper
.....
ENGINE ASSETS
stylesheet_link_tag cowboy/guns.css
javascript_include_tag cowboy/guns.js
or
*=require cowboy/guns
//=requir...
PARTIALS
render gun => render partial:‘cowboy/...’, object: gun
render :partial ‘fred’ => render :partial ‘cowboy/fred’
Tu...
LOADING ORDER
config.railties_order = [Cowboy::Engine, :main_app, :all]
Tuesday, September 17, 13
TESTING
Tuesday, September 17, 13
TESTING (RSPEC)
spec/dummy
Tuesday, September 17, 13
TESTING (RSPEC)
config.include Dummy::Application.routes.url_helpers
use_route :cowboy
Tuesday, September 17, 13
LAB II
Scenario: Other models in the system need to be
commentable. Spin out this functionality as a new
‘social’ engine
T...
CHEAT
> rails plugin new engines/social -T --mountable 
-j coffeescript -B -d mysql 
--dummy-path=spec/dummy
Tuesday, Sept...
LAB III
Scenario: PM wants a reporting admin ui to track
comments. In light of this new feature commenting
behavior needs ...
Q&A
Get a rope?
Tuesday, September 17, 13
Upcoming SlideShare
Loading in...5
×

Engines

1,065

Published on

An intro to rails engines with practice labs.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,065
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Engines"

  1. 1. COWBOYS & ENGINES Fernand Galiana Tuesday, September 17, 13
  2. 2. HISTORY 0.14.2 ~ 2005 2.3 ~ 2009 -- Engine support 3.0 ~ 2010 --Yehuda and crew Tuesday, September 17, 13
  3. 3. SURPRISE? > rails c > Rails::Application.ancestors => [Rails::Application, Rails::Engine,...] Tuesday, September 17, 13
  4. 4. IMPETUS? Mini rails apps [MVC|Assets|lib] Divide conquer Reusable components Decoupling Performance Tuesday, September 17, 13
  5. 5. ENGINE TYPES full mountable Tuesday, September 17, 13
  6. 6. CREATE! > rails plugin new engines/blee [opts] Tuesday, September 17, 13
  7. 7. NOTABLES... --full --mountable -T -B -d [mysql|pg|sqlite3|etc...] -j [jquery|coffeescript] -dummy_path Tuesday, September 17, 13
  8. 8. CREATE! > rails plugin new engines/cowboy --mountable -T -B --dummy-path=spec/dummy -j coffeescript -d mysql Tuesday, September 17, 13
  9. 9. TEH BROG! You rock, bro! Tuesday, September 17, 13
  10. 10. LAB I git clone https://github.com/derailed/ng_training git checkout -b lab_1 follow the README! Tuesday, September 17, 13
  11. 11. ANATOMY Tuesday, September 17, 13
  12. 12. STRUCTURE cowboy_engine o app o bin o config o lib o spec Tuesday, September 17, 13
  13. 13. STRUCTURE cowboy_engine o app o assets o controllers o helpers o models o views o bin o ... Tuesday, September 17, 13
  14. 14. STRUCTURE cowboy_engine o app o controllers o cowboy_engine o cowboys_controller.rb o ... o bin o ... Tuesday, September 17, 13
  15. 15. ENGINE GEMSPEC Gem::Specification.new do |s| s.name = "pilgrim" s.version = Pilgrim::VERSION s.authors = ["Fernand"] s.email = ["fernand.galiana@gmail.com"] 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
  16. 16. THE ENGINE module Cowboy class Engine < ::Rails::Engine isolate_namespace config.autoload_paths += [File.join( self.root, %w[lib blee] )] end end Tuesday, September 17, 13
  17. 17. BUSINESS AS USUAL rails g [model|controller|migration] custom generators engine initializers Tuesday, September 17, 13
  18. 18. GEMFILE gem ‘cowboy’, path:‘engines/cowboy’ my_app/Gemfile Tuesday, September 17, 13
  19. 19. CONVENIENCE Cowboy::Engine.root Cowboy::Engine.helpers Cowboy::Engine.routes.url_helpers Tuesday, September 17, 13
  20. 20. MIGRATIONS Tuesday, September 17, 13
  21. 21. MIGRATIONS > rake cowboy:install:migrations Tuesday, September 17, 13
  22. 22. MIGRATIONS rake db:migrate SCOPE=Cowboy Tuesday, September 17, 13
  23. 23. 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 end end Tuesday, September 17, 13
  24. 24. ISOLATED MODELS table names expects engine_name_table_name self.table_name = :guns or table_name_prefix = “” Tuesday, September 17, 13
  25. 25. ROUTING Tuesday, September 17, 13
  26. 26. engines/cowboy/config/routes.rb ENGINE ROUTES Cowboy::Engine.routes.draw do resources :guns, only: [:create] resources :belt do resources :ammos end end Tuesday, September 17, 13
  27. 27. 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' end end rango/config/routes.rb Tuesday, September 17, 13
  28. 28. CONTEXT MATTERS! Cowboy engine context guns_path => /guns main_app.welcome_path => /welcome Main Application context cowboy_ng.guns_path => /pilgrims/guns welcome_path => /welcome Tuesday, September 17, 13
  29. 29. my_app/controllers/guns_controller.rb HELPERS class GunsController < ApplicationController helper Cowboy::BulletsHelper ... end Tuesday, September 17, 13
  30. 30. 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
  31. 31. PARTIALS render gun => render partial:‘cowboy/...’, object: gun render :partial ‘fred’ => render :partial ‘cowboy/fred’ Tuesday, September 17, 13
  32. 32. LOADING ORDER config.railties_order = [Cowboy::Engine, :main_app, :all] Tuesday, September 17, 13
  33. 33. TESTING Tuesday, September 17, 13
  34. 34. TESTING (RSPEC) spec/dummy Tuesday, September 17, 13
  35. 35. TESTING (RSPEC) config.include Dummy::Application.routes.url_helpers use_route :cowboy Tuesday, September 17, 13
  36. 36. 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
  37. 37. CHEAT > rails plugin new engines/social -T --mountable -j coffeescript -B -d mysql --dummy-path=spec/dummy Tuesday, September 17, 13
  38. 38. 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
  39. 39. Q&A Get a rope? Tuesday, September 17, 13
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×