Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

OUR ITINERARY

Slide 5

Slide 5 text

OUR ITINERARY 1. What are Rails Engines?

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

WHAT ARE ENGINES?

Slide 10

Slide 10 text

- RailsGuides: Getting Started with Engines
 (http://guides.rubyonrails.org/engines.html) Engines can be considered miniature applications that provide functionality to their host applications.

Slide 11

Slide 11 text

- me Engines let you build gems with access to autoloading magic, and the full Rails MVC architecture.

Slide 12

Slide 12 text

OK, BUT$ ARE THEY USEFUL?

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

ENGINES YOU ALREADY USE • bootstrap-sass

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

ENGINES YOU ALREADY USE • bootstrap-sass • jquery-rails • turbolinks • coffee-rails

Slide 18

Slide 18 text

ENGINES YOU ALREADY USE All Your Rails Apps!

Slide 19

Slide 19 text

- 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.

Slide 20

Slide 20 text

https://github.com/rails/rails/blob/master/railties/lib/rails/ application.rb#L79 module Rails # ... class Application < Engine # ... end end

Slide 21

Slide 21 text

LET’S BUILD$ AN ENGINE!

Slide 22

Slide 22 text

USER STORY

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

COMMON ENDPOINT TO CLEAR THE CACHE

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

THERE WILL BE CONFUSION

Slide 27

Slide 27 text

THERE WILL BE HURDLES

Slide 28

Slide 28 text

IT’S A GREAT HAMMER

Slide 29

Slide 29 text

IT’S A GREAT HAMMER … BUT NOT EVERYTHING IS A NAIL

Slide 30

Slide 30 text

ANATOMY OF AN ENGINE

Slide 31

Slide 31 text

ENGINE BUILDING 101

Slide 32

Slide 32 text

LET’S WRITE SOME CODE!

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Copy Migrations Add Migrations to the Load Path Simplified Upgrades ❌ ✅ Migrations Stored in Host App ✅ ❌

Slide 35

Slide 35 text

A HISTORY LESSON

Slide 36

Slide 36 text

IN THE BEGINNING…

Slide 37

Slide 37 text

IN THE BEGINNING… There was Adam.

Slide 38

Slide 38 text

IN THE BEGINNING… There was Adam. James Adam.

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

- 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.

Slide 41

Slide 41 text

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)

Slide 42

Slide 42 text

SO WHY WERE ENGINES CREATED?

Slide 43

Slide 43 text

- 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.

Slide 44

Slide 44 text

FAST-FORWARD…

Slide 45

Slide 45 text

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/)

Slide 46

Slide 46 text

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/)

Slide 47

Slide 47 text

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/)

Slide 48

Slide 48 text

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/)

Slide 49

Slide 49 text

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/)

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

WHY THE SKEPTICISM?

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

PAPERTRAIL::VERSION

Slide 55

Slide 55 text

DEFINING CHARACTERISTICS OF ENGINES

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

DEFINING CHARACTERISTICS OF ENGINES • Which parts of MVC are required? • Database Management? Migrations? • Internal Tool (full control)
 or Open Source ( ¯\_(ツ)_/¯)?

Slide 59

Slide 59 text

DEFINING CHARACTERISTICS OF ENGINES What direction(s) do your dependencies run?

Slide 60

Slide 60 text

DEFINING CHARACTERISTICS OF ENGINES • Independent MVC architecture What direction(s) do your dependencies run?

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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?

Slide 63

Slide 63 text

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?

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

THE LITTLE ENGINE THAT COULD

Slide 66

Slide 66 text

WHAT CHANGED FROM 2005 TO TODAY?

Slide 67

Slide 67 text

COMPONENTS$ ARE NOT ALWAYS INTERESTING!

Slide 68

Slide 68 text

RECOMMENDED USE CASES
 FOR ENGINES

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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?

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

FURTHER INFORMATION • History of Rails Engines$ • GoGaRuCo 2012, Erik Michaels-Ober - “Writing a Rails Engine” - https:// www.youtube.com/watch?v=MsRPxS7Cu_Q

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

FURTHER INFORMATION • Components$ • RailsConf 2015, Stephan Hagemann - “Get started with Component-based Rails applications!” - https://www.youtube.com/ watch?v=MsRPxS7Cu_Q

Slide 78

Slide 78 text

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/

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

THANK YOU! @amcaplan amcaplan.ninja We’re hiring!!! Ariel Caplan