Successfully reported this slideshow.

More Related Content

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Rails engines

  1. 1. Rails Engines
  2. 2. Hi, I’m Josh I work here ->
  3. 3. We Build Ruby Apps And a bunch of other stuff
  4. 4. We also do design
  5. 5. We also do design The designers had NOTHING to do with my slides.
  6. 6. Here’s the Problem Even custom applications want to be able to manage their static pages Think Contact Us, Team Bios, etc. Wordpress, Drupal even Locomotive are great but heavy weight
  7. 7. What we needed A mini-cms Uses our existing rails views No subdomaining, feels like part of the app Works with our authentication setup Great for 4 or 5 pages of content
  8. 8. Options High Voltage Great for devs, not great for the customer Locomotive Engine Awesome but heavyweight, assumes their template style
  9. 9. Exstatic WARNING: Not even close to finished Works w/ devise Mounts in another application Inherits that application’s layouts
  10. 10. But how? Rails Engines obviously, otherwise this talk wouldn’t make much sense
  11. 11. What are Rails Engines? Lesser known feature, started in Rails 3.0 Mini-application that can be added to a larger rails app Replace plugins Are a subset of Railties, so you can use railtie stuff (like generators, rake tasks, etc.)
  12. 12. Why? Code reuse Share functional components Share business logic Isolation Distribution Open Source part of a private app
  13. 13. Creating an Engine 2 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 RSpec Add rspec-rails to development dependencies in gemspec bundle install rails g rspec:install Set rspec as test_framework in engine.rb lib/engine_name/engine.rb
  19. 19. Testing through dummy rails plugin new automatically creates a dummy app Rails.application is available to test initializers Rails.application.routes will get you dummy app routes More in a bit
  20. 20. Depot as an Engine Depot application
  21. 21. Migration Pitfalls You have to use the engine name everywhere rails g migration add_foo_to_engine_name_table_name To copy migrations into dummy app cd into dummy and run rake engine_name:install:migrations
  22. 22. Asset Pipeline Pitfalls Assets have to be preceeded by engine_name in view helpers image_tag(‘engine_name/logo.png’) Copy assets group in Gemfile from normal rails app into Gemfile in engine, then bundle install in dummy app
  23. 23. View Hints You can reference parent via main_app helper i.e. link_to “Home”, main_app.root_path Use the parent’s layouts by adding layout “application” to ApplicationController in engine
  24. 24. View Hints In parent app use engine_name.path_helper to link into engine link_to “Store”, depot_engine.root_path You can override views by putting them in app/views/engine_name/controller/view_ name
  25. 25. Rake Tasks Rake tasks can be added in lib/tasks Show up in parent application Can be run in rails engine via app namespace
  26. 26. Other Stuff Custom Rails Generators Injecting routes into the parent Config options Initializers etc. etc.
  27. 27. Exstatic - Authorization Uses a config option to call an authentication method Setup in initializer spec/controllers/pages_controller_spec.rb
  28. 28. Exstatic - Generators Used for migrations but no longer needed You can however setup a rails_engine:install to automate setup
  29. 29. Exstatic - Dynamic Routes Can add routes directly into parent app lib/exstatic/engine.rb Can also ensure you aren’t overwriting a route that is defined by parent lib/exstatic/validators/nonexistant_path_v alidator.rb
  30. 30. Resources
  31. 31. Gems I “borrowed” from Devise High Voltage e