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

Start Your (Rails) Engines! (RailsRemoteConf 2015)

7b5a451ee25044b9c869e3e98b79425d?s=47 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

7b5a451ee25044b9c869e3e98b79425d?s=128

Ariel Caplan

November 05, 2015
Tweet

More Decks by Ariel Caplan

Other Decks in Technology

Transcript

  1. START YOUR (RAILS) ENGINES! UNDERSTANDING, BUILDING, AND INTEGRATING RAILS ENGINES

  2. Ariel Caplan @amcaplan amcaplan.ninja QUALITY COST $ $ ACCESS

  3. None
  4. OUR ITINERARY

  5. OUR ITINERARY 1. What are Rails Engines?

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

    an Engine!
  7. OUR ITINERARY 1. What are Rails Engines? 2. Live Code

    an Engine! 3. A Brief History
 of Rails Engines
  8. 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
  9. WHAT ARE ENGINES?

  10. - RailsGuides: Getting Started with Engines
 (http://guides.rubyonrails.org/engines.html) Engines can be

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

    autoloading magic, and the full Rails MVC architecture.
  12. OK, BUT$ ARE THEY USEFUL?

  13. ENGINES YOU ALREADY USE • resque-web$ • rollout-ui$ • PaperTrail$

    • RailsAdmin
  14. ENGINES YOU ALREADY USE • bootstrap-sass

  15. ENGINES YOU ALREADY USE • bootstrap-sass • coffee-rails

  16. ENGINES YOU ALREADY USE • bootstrap-sass • jquery-rails • coffee-rails

  17. ENGINES YOU ALREADY USE • bootstrap-sass • jquery-rails • turbolinks

    • coffee-rails
  18. ENGINES YOU ALREADY USE All Your Rails Apps!

  19. - 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.
  20. https://github.com/rails/rails/blob/master/railties/lib/rails/ application.rb#L79 module Rails # ... class Application < Engine

    # ... end end
  21. LET’S BUILD$ AN ENGINE!

  22. USER STORY

  23. None
  24. COMMON ENDPOINT TO CLEAR THE CACHE

  25. None
  26. THERE WILL BE CONFUSION

  27. THERE WILL BE HURDLES

  28. IT’S A GREAT HAMMER

  29. IT’S A GREAT HAMMER … BUT NOT EVERYTHING IS A

    NAIL
  30. ANATOMY OF AN ENGINE

  31. ENGINE BUILDING 101

  32. LET’S WRITE SOME CODE!

  33. 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
  34. Copy Migrations Add Migrations to the Load Path Simplified Upgrades

    ❌ ✅ Migrations Stored in Host App ✅ ❌
  35. A HISTORY LESSON

  36. IN THE BEGINNING…

  37. IN THE BEGINNING… There was Adam.

  38. IN THE BEGINNING… There was Adam. James Adam.

  39. None
  40. - 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.
  41. 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)
  42. SO WHY WERE ENGINES CREATED?

  43. - 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.
  44. FAST-FORWARD…

  45. 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/)
  46. 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/)
  47. 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/)
  48. 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/)
  49. 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/)
  50. None
  51. None
  52. WHY THE SKEPTICISM?

  53. None
  54. PAPERTRAIL::VERSION

  55. DEFINING CHARACTERISTICS OF ENGINES

  56. DEFINING CHARACTERISTICS OF ENGINES • Which parts of MVC are

    required?
  57. DEFINING CHARACTERISTICS OF ENGINES • Which parts of MVC are

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

    required? • Database Management? Migrations? • Internal Tool (full control)
 or Open Source ( ¯\_(ツ)_/¯)?
  59. DEFINING CHARACTERISTICS OF ENGINES What direction(s) do your dependencies run?

  60. DEFINING CHARACTERISTICS OF ENGINES • Independent MVC architecture What direction(s)

    do your dependencies run?
  61. DEFINING CHARACTERISTICS OF ENGINES • Independent MVC architecture • Engine

    depends on app (e.g., admin tool) What direction(s) do your dependencies run?
  62. 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?
  63. 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?
  64. None
  65. THE LITTLE ENGINE THAT COULD

  66. WHAT CHANGED FROM 2005 TO TODAY?

  67. COMPONENTS$ ARE NOT ALWAYS INTERESTING!

  68. RECOMMENDED USE CASES
 FOR ENGINES

  69. RECOMMENDED USE CASES
 FOR ENGINES • Mini-applications or tiny utilities

  70. RECOMMENDED USE CASES
 FOR ENGINES • Mini-applications or tiny utilities

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

    • Distributing pieces of /M?VC/ and/or asset pipeline • Cross-cutting concerns?
  72. 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
  73. 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?
  74. 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
  75. FURTHER INFORMATION • History of Rails Engines$ • GoGaRuCo 2012,

    Erik Michaels-Ober - “Writing a Rails Engine” - https:// www.youtube.com/watch?v=MsRPxS7Cu_Q
  76. 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
  77. FURTHER INFORMATION • Components$ • RailsConf 2015, Stephan Hagemann -

    “Get started with Component-based Rails applications!” - https://www.youtube.com/ watch?v=MsRPxS7Cu_Q
  78. 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/
  79. 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
  80. 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
  81. 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
  82. 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
  83. THANK YOU! @amcaplan amcaplan.ninja We’re hiring!!! Ariel Caplan