Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rails engines


Published on

  • Be the first to comment

Rails engines

  1. 1. Rails Engines
  2. 2. Hi, I’m JoshI work here ->
  3. 3. We Build RubyAppsAnd a bunch of other stuff
  4. 4. We also do design
  5. 5. We also do designThe designers had NOTHING to do with myslides.
  6. 6. Here’s the ProblemEven custom applications want to be ableto manage their static pages Think Contact Us, Team Bios, etc.Wordpress, Drupal even Locomotive aregreat but heavy weight
  7. 7. What we neededA mini-cmsUses our existing rails viewsNo subdomaining, feels like part of the appWorks with our authentication setupGreat for 4 or 5 pages of content
  8. 8. OptionsHigh Voltage Great for devs, not great for the customerLocomotive Engine Awesome but heavyweight, assumes their template style
  9. 9. Exstatic Not even close to finishedWorks w/ deviseMounts in another applicationInherits that application’s layouts
  10. 10. But how?Rails Engines obviously, otherwise this talk wouldn’t make much sense
  11. 11. What are RailsEngines?Lesser known feature, started in Rails 3.0Mini-application that can be added to alarger rails appReplace pluginsAre a subset of Railties, so you can userailtie stuff (like generators, rake tasks, etc.)
  12. 12. Why?Code reuse Share functional components Share business logicIsolationDistribution Open Source part of a private app
  13. 13. Creating an Engine2 Ways Rails 3.1 or greater > rails plugin new Rails 3.0 Use enginex by Jose Valim
  14. 14. $> rails plugin new depot_engine --dummy-path=spec/dummy --skip-test-unit --mountable --skip-bundle...
  15. 15. $> rails plugin new depot_engine --dummy-path=spec/dummy --skip-test-unit --mountable --skip-bundle Creates a new Rails Engine with the name DepotEngine
  16. 16. $> rails plugin new depot_engine --dummy-path=spec/ dummy --skip-test-unit --mountable --skip-bundle Creates a fake rails app in the RSpec path
  17. 17. $> rails plugin new depot_engine --dummy-path=spec/ dummy --skip-test-unit --mountable --skip-bundle Sets the engine to be “mountable” as opposed to “full” Full - the parent inherits routes from the engine and can directly access it’s components (controllers, models, etc.) Mountable - the engine’s namespace is isolated and draws its own routes. It mounts at a path: mount DepotEngine::Engine => “/store”, :as => “store”
  18. 18. Setting Up RSpecAdd rspec-rails to developmentdependencies in gemspecbundle installrails g rspec:installSet rspec as test_framework in engine.rb lib/engine_name/engine.rb
  19. 19. Testing throughdummyrails plugin new automatically creates adummy appRails.application is available to testinitializersRails.application.routes will get you dummyapp routes More in a bit
  20. 20. Depot as an EngineDepot
  21. 21. Migration PitfallsYou have to use the engine nameeverywhere rails g migration add_foo_to_engine_name_table_nameTo copy migrations into dummy app cd intodummy and run rake engine_name:install:migrations
  22. 22. Asset PipelinePitfallsAssets have to be preceeded byengine_name in view helpers image_tag(‘engine_name/logo.png’)Copy assets group in Gemfile from normalrails app into Gemfile in engine, thenbundle install in dummy app
  23. 23. View HintsYou can reference parent via main_apphelper i.e. link_to “Home”, main_app.root_pathUse the parent’s layouts by adding layout“application” to ApplicationController inengine
  24. 24. View HintsIn parent app useengine_name.path_helper to link intoengine link_to “Store”, depot_engine.root_pathYou can override views by putting them in app/views/engine_name/controller/view_ name
  25. 25. Rake TasksRake tasks can be added in lib/tasks Show up in parent application Can be run in rails engine via app namespace
  26. 26. Other StuffCustom Rails GeneratorsInjecting routes into the parentConfig optionsInitializersetc. etc.
  27. 27. Exstatic -AuthorizationUses a config option to call anauthentication methodSetup in initializerspec/controllers/pages_controller_spec.rb
  28. 28. Exstatic -GeneratorsUsed for migrations but no longer neededYou can however setup arails_engine:install to automate setup
  29. 29. Exstatic - DynamicRoutesCan add routes directly into parent app lib/exstatic/engine.rbCan also ensure you aren’t overwriting aroute that is defined by parent lib/exstatic/validators/nonexistant_path_v alidator.rb
  30. 30. Resources
  31. 31. Gems I “borrowed”fromDevise Voltage e