Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Start Your (Rails) Engines! (RailsRemoteConf 2015)

Ariel Caplan
November 05, 2015

Start Your (Rails) Engines! (RailsRemoteConf 2015)

Want to split up your Rails app into pieces but not sure where to begin? Wish you could share controller code among microservices but don't know how? Do you work on lots of projects and have boilerplate Rails code repeated in each?

Rails Engines may be your answer.

We will build a simple Rails engine and understand how this app-within-an-app architecture can help you write more modular code which can be gemified and reused across multiple projects.

The video will be made public... eventually.

In the meantime, you can check out the code from the "live code" portion at https://github.com/amcaplan/forget and https://github.com/amcaplan/cacher. I also edited my notes from the live code portion, and published them at https://gist.github.com/amcaplan/0841065fdb69966b860f

For your convenience, here are the links from the talk.

Links throughout the presentation:

http://guides.rubyonrails.org/engines.html
https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb#L79
http://weblog.rubyonrails.org/2005/11/11/why-engines-and-components-are-not-evil-but-distracting/
http://article.gmane.org/gmane.comp.lang.ruby.rails/29166
https://techblog.livingsocial.com/blog/2014/01/24/open-sourcing-with-rails-engines/

Further information links:

GoGaRuCo 2012, Erik Michaels-Ober - “Writing a Rails Engine” - https://www.youtube.com/watch?v=MsRPxS7Cu_Q
Ben Smith - “Leave your migrations in your Rails engines” - http://blog.pivotal.io/labs/labs/leave-your-migrations-in-your-rails-engines
RailsConf 2015, Stephan Hagemann - “Get started with Component-based Rails applications!” - https://www.youtube.com/watch?v=MsRPxS7Cu_Q
Brian Leonard, “Rails 4 Engines” (Blog post about splitting up an application) - http://tech.taskrabbit.com/blog/2014/02/11/rails-4-engines/
Rocky Mountain Ruby 2013, Ben Smith - “How I architected my big Rails app for success!” - https://www.youtube.com/watch?v=uDaBtqEYNBo
Stephan Hagemann, “Migrating from a single Rails app to a suite of Rails engines” - http://blog.pivotal.io/labs/labs/migrating-from-a-single-rails-app-to-a-suite-of-rails-engines
Will Read, “Experience Report: Engine Usage That Didn't Work” - http://blog.pivotal.io/labs/labs/experience-report-engine-usage-that-didn-t-work

Ariel Caplan

November 05, 2015
Tweet

More Decks by Ariel Caplan

Other Decks in Technology

Transcript

  1. OUR ITINERARY 1. What are Rails Engines? 2. Live Code

    an Engine! 3. A Brief History
 of Rails Engines
  2. OUR ITINERARY 1. What are Rails Engines? 2. Live Code

    an Engine! 3. A Brief History
 of Rails Engines 4. The When and Why
 of Rails Engines
  3. - RailsGuides: Getting Started with Engines
 (http://guides.rubyonrails.org/engines.html) Engines can be

    considered miniature applications that provide functionality to their host applications.
  4. - me Engines let you build gems with access to

    autoloading magic, and the full Rails MVC architecture.
  5. - RailsGuides: Getting Started with Engines
 (http://guides.rubyonrails.org/engines.html) A Rails application

    is actually just a "supercharged" engine, with the Rails::Application class inheriting a lot of its behavior from Rails::Engine.
  6. module MyRailsEngine class Engine < ::Rails::Engine isolate_namespace MyRailsEngine ! initializer

    :append_migrations do |app| unless app.root.to_s.match root.to_s + File::SEPARATOR config.paths["db/migrate"].expanded.each do |path| app.config.paths["db/migrate"] << path end end end end end KEEPING MIGRATIONS IN YOUR ENGINE
  7. Copy Migrations Add Migrations to the Load Path Simplified Upgrades

    ❌ ✅ Migrations Stored in Host App ✅ ❌
  8. - DHH, “Why engines and components are not evil but

    distracting”
 (http://weblog.rubyonrails.org/2005/11/11/why-engines-and- components-are-not-evil-but-distracting/) I think these developments are basically another name for high-level components. And you all know how I feel about those. $ ! The short summary is that high-level components are a mirage: By the time they become interesting, their fitting will require more work than creating something from scratch.
  9. Engines have not recieved (sic) the blessing of the RoR

    core team, and I$ wouldn't expect any different, because it would be madness to include them in the core Rails. It's a mechanism far too easily [ab]used for things it's not actually suitable for. - James Adam, from the Ruby on Rails mailing list
 (http://article.gmane.org/gmane.comp.lang.ruby.rails/29166)
  10. - James Adam, from the Ruby on Rails mailing list


    (http://article.gmane.org/gmane.comp.lang.ruby.rails/29166) Engines fill a fairly specific need that we
 (my team) have, which is the development of multiple distinct Rails applications which share common components (authentication, reporting, importing data from hellish Excel), and ensuring that a means to apply patches & bug fixes across each of these applications, including models, controllers and views with the *minimum* of developer time spend (sic) on managing those updates.
  11. COMMON USE CASES FOR ENGINES - Trent Albright “Open Sourcing

    with Rails Engines”, LivingSocial tech blog
 (https://techblog.livingsocial.com/blog/2014/01/24/open-sourcing-with-rails-engines/)
  12. COMMON USE CASES FOR ENGINES • Mini-applications (resque-web) - Trent

    Albright “Open Sourcing with Rails Engines”, LivingSocial tech blog
 (https://techblog.livingsocial.com/blog/2014/01/24/open-sourcing-with-rails-engines/)
  13. COMMON USE CASES FOR ENGINES • Mini-applications (resque-web) • Plug-in

    for other Rails applications (Devise) - Trent Albright “Open Sourcing with Rails Engines”, LivingSocial tech blog
 (https://techblog.livingsocial.com/blog/2014/01/24/open-sourcing-with-rails-engines/)
  14. COMMON USE CASES FOR ENGINES • Mini-applications (resque-web) • Plug-in

    for other Rails applications (Devise) • Composition (SOA components) - Trent Albright “Open Sourcing with Rails Engines”, LivingSocial tech blog
 (https://techblog.livingsocial.com/blog/2014/01/24/open-sourcing-with-rails-engines/)
  15. COMMON USE CASES FOR ENGINES • Mini-applications (resque-web) • Plug-in

    for other Rails applications (Devise) • Composition (SOA components) • Stand-Alone Application (Rearview) - Trent Albright “Open Sourcing with Rails Engines”, LivingSocial tech blog
 (https://techblog.livingsocial.com/blog/2014/01/24/open-sourcing-with-rails-engines/)
  16. DEFINING CHARACTERISTICS OF ENGINES • Which parts of MVC are

    required? • Database Management? Migrations?
  17. DEFINING CHARACTERISTICS OF ENGINES • Which parts of MVC are

    required? • Database Management? Migrations? • Internal Tool (full control)
 or Open Source ( ¯\_(ツ)_/¯)?
  18. DEFINING CHARACTERISTICS OF ENGINES • Independent MVC architecture • Engine

    depends on app (e.g., admin tool) What direction(s) do your dependencies run?
  19. DEFINING CHARACTERISTICS OF ENGINES • Independent MVC architecture • Engine

    depends on app (e.g., admin tool) • App depends on engine
 (cross-cutting concern, e.g. authentication) What direction(s) do your dependencies run?
  20. DEFINING CHARACTERISTICS OF ENGINES • Independent MVC architecture • Engine

    depends on app (e.g., admin tool) • App depends on engine
 (cross-cutting concern, e.g. authentication) • Dependencies run in both directions
 (potentially the case with components) What direction(s) do your dependencies run?
  21. RECOMMENDED USE CASES
 FOR ENGINES • Mini-applications or tiny utilities

    • Distributing pieces of /M?VC/ and/or asset pipeline
  22. RECOMMENDED USE CASES
 FOR ENGINES • Mini-applications or tiny utilities

    • Distributing pieces of /M?VC/ and/or asset pipeline • Cross-cutting concerns?
  23. RECOMMENDED USE CASES
 FOR ENGINES • Mini-applications or tiny utilities

    • Distributing pieces of /M?VC/ and/or asset pipeline • Cross-cutting concerns? • PLEASE avoid migrations if possible
  24. RECOMMENDED USE CASES
 FOR ENGINES • Mini-applications or tiny utilities

    • Distributing pieces of /M?VC/ and/or asset pipeline • Cross-cutting concerns? • PLEASE avoid migrations if possible • Component-based applications?
  25. RECOMMENDED USE CASES
 FOR ENGINES • Mini-applications or tiny utilities

    • Distributing pieces of /M?VC/ and/or asset pipeline • Cross-cutting concerns? • PLEASE avoid migrations if possible • Component-based applications? • Alternatively, a step towards multiple apps
  26. FURTHER INFORMATION • History of Rails Engines$ • GoGaRuCo 2012,

    Erik Michaels-Ober - “Writing a Rails Engine” - https:// www.youtube.com/watch?v=MsRPxS7Cu_Q
  27. FURTHER INFORMATION • Migrations$ • Ben Smith - “Leave your

    migrations in your Rails engines” - http://blog.pivotal.io/labs/ labs/leave-your-migrations-in-your-rails- engines
  28. FURTHER INFORMATION • Components$ • RailsConf 2015, Stephan Hagemann -

    “Get started with Component-based Rails applications!” - https://www.youtube.com/ watch?v=MsRPxS7Cu_Q
  29. FURTHER INFORMATION • Components$ • Brian Leonard, “Rails 4 Engines”

    (Blog post about splitting up an application) - http:// tech.taskrabbit.com/blog/2014/02/11/rails-4- engines/
  30. FURTHER INFORMATION • Components$ • Rocky Mountain Ruby 2013, Ben

    Smith - “How I architected my big Rails app for success!” - https://www.youtube.com/watch? v=uDaBtqEYNBo
  31. FURTHER INFORMATION • Components$ • Stephan Hagemann, “Migrating from a

    single Rails app to a suite of Rails engines” - http:// blog.pivotal.io/labs/labs/migrating-from-a- single-rails-app-to-a-suite-of-rails-engines
  32. FURTHER INFORMATION • Components$ • Will Read, “Experience Report: Engine

    Usage That Didn't Work” - http:// blog.pivotal.io/labs/labs/experience-report- engine-usage-that-didn-t-work
  33. IMAGE CREDITS • “The Little Engine That Could” by Cliff:

    https://www.flickr.com/photos/nostri-imago/ 2851664965 / CC BY 2.0$ • “Official New York City Subway Map” by Metropolitan Transportation Authority: https:// commons.wikimedia.org/wiki/File:Official_New_York_City_Subway_Map_vc.jpg / CC BY 2.0$ • “James Bond 007 13” by Themeplus: https://www.flickr.com/photos/ 85217387@N04/8445205301 / CC BY-SA 2.0$ • “Hurdles (Scenes from a Track Meet)” by Phil Roeder: https://www.flickr.com/photos/tabor- roeder/5835126514/ / CC BY 2.0$ • “Bent Screw Hole Backyard Metal Macros April 01, 20103” by Steven Depolo: https:// www.flickr.com/photos/stevendepolo/4482491047 / CC BY 2.0