Engines

  • 967 views
Uploaded on

An intro to rails engines with practice labs.

An intro to rails engines with practice labs.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
967
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
5
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. COWBOYS & ENGINES Fernand Galiana Tuesday, September 17, 13
  • 2. HISTORY 0.14.2 ~ 2005 2.3 ~ 2009 -- Engine support 3.0 ~ 2010 --Yehuda and crew Tuesday, September 17, 13
  • 3. SURPRISE? > rails c > Rails::Application.ancestors => [Rails::Application, Rails::Engine,...] Tuesday, September 17, 13
  • 4. IMPETUS? Mini rails apps [MVC|Assets|lib] Divide conquer Reusable components Decoupling Performance Tuesday, September 17, 13
  • 5. ENGINE TYPES full mountable Tuesday, September 17, 13
  • 6. CREATE! > rails plugin new engines/blee [opts] Tuesday, September 17, 13
  • 7. NOTABLES... --full --mountable -T -B -d [mysql|pg|sqlite3|etc...] -j [jquery|coffeescript] -dummy_path Tuesday, September 17, 13
  • 8. CREATE! > rails plugin new engines/cowboy --mountable -T -B --dummy-path=spec/dummy -j coffeescript -d mysql Tuesday, September 17, 13
  • 9. TEH BROG! You rock, bro! Tuesday, September 17, 13
  • 10. LAB I git clone https://github.com/derailed/ng_training git checkout -b lab_1 follow the README! Tuesday, September 17, 13
  • 11. ANATOMY Tuesday, September 17, 13
  • 12. STRUCTURE cowboy_engine o app o bin o config o lib o spec Tuesday, September 17, 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. STRUCTURE cowboy_engine o app o controllers o cowboy_engine o cowboys_controller.rb o ... o bin o ... Tuesday, September 17, 13
  • 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. 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. BUSINESS AS USUAL rails g [model|controller|migration] custom generators engine initializers Tuesday, September 17, 13
  • 18. GEMFILE gem ‘cowboy’, path:‘engines/cowboy’ my_app/Gemfile Tuesday, September 17, 13
  • 19. CONVENIENCE Cowboy::Engine.root Cowboy::Engine.helpers Cowboy::Engine.routes.url_helpers Tuesday, September 17, 13
  • 20. MIGRATIONS Tuesday, September 17, 13
  • 21. MIGRATIONS > rake cowboy:install:migrations Tuesday, September 17, 13
  • 22. MIGRATIONS rake db:migrate SCOPE=Cowboy Tuesday, September 17, 13
  • 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. ISOLATED MODELS table names expects engine_name_table_name self.table_name = :guns or table_name_prefix = “” Tuesday, September 17, 13
  • 25. ROUTING Tuesday, September 17, 13
  • 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. 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. 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. my_app/controllers/guns_controller.rb HELPERS class GunsController < ApplicationController helper Cowboy::BulletsHelper ... end Tuesday, September 17, 13
  • 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. PARTIALS render gun => render partial:‘cowboy/...’, object: gun render :partial ‘fred’ => render :partial ‘cowboy/fred’ Tuesday, September 17, 13
  • 32. LOADING ORDER config.railties_order = [Cowboy::Engine, :main_app, :all] Tuesday, September 17, 13
  • 33. TESTING Tuesday, September 17, 13
  • 34. TESTING (RSPEC) spec/dummy Tuesday, September 17, 13
  • 35. TESTING (RSPEC) config.include Dummy::Application.routes.url_helpers use_route :cowboy Tuesday, September 17, 13
  • 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. CHEAT > rails plugin new engines/social -T --mountable -j coffeescript -B -d mysql --dummy-path=spec/dummy Tuesday, September 17, 13
  • 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. Q&A Get a rope? Tuesday, September 17, 13