Slide 1

Slide 1 text

Mountable Engines Playing well with other apps Patrick Peak @peakpg

Slide 2

Slide 2 text

About Me

Slide 3

Slide 3 text

The Problem: Reuse is hard

Slide 4

Slide 4 text

Duplication is the root of all evil.

Slide 5

Slide 5 text

Start with a Library

Slide 6

Slide 6 text

Plugins

Slide 7

Slide 7 text

Generated code makes upgrades costly

Slide 8

Slide 8 text

Agenda • Why I care about Engines • What are they? • Building one • Learning to configure • Making installation easy • Using the Asset Pipeline • Working with other Engines

Slide 9

Slide 9 text

Evolution of a CMS 2.3 3.0 Engines Introduced 3.1 Mountable Initializers Asset Pipeline 3.2

Slide 10

Slide 10 text

What are Engines? Responsibilities: •  Business Domain •  Selling Stuff •  Managing Users •  Blogging App #2

Slide 11

Slide 11 text

Separation of Concerns Selling Stuff Managing Users Blogging Business Domain App #2

Slide 12

Slide 12 text

Examples Popular Engines

Slide 13

Slide 13 text

E-commerce

Slide 14

Slide 14 text

Authentication

Slide 15

Slide 15 text

Admin Dashboards

Slide 16

Slide 16 text

Building an Engine

Slide 17

Slide 17 text

What the world needs… $ rails plugin new another --mountable

Slide 18

Slide 18 text

Result: A Gem with a Rails App inside Rails App Gem

Slide 19

Slide 19 text

Add a Resource $ rails g resource Page name path content

Slide 20

Slide 20 text

‘Typical’ Controller

Slide 21

Slide 21 text

Isolated Controller

Slide 22

Slide 22 text

Isolated Namespace

Slide 23

Slide 23 text

Routing Engines own their Routes

Slide 24

Slide 24 text

Engine Routes Rails.application.routes.draw

Slide 25

Slide 25 text

Our Awesome UI

Slide 26

Slide 26 text

Mounting the Engine App Root

Slide 27

Slide 27 text

Route Helpers <%= main_app.root_url %> # => / <%= another.root_url %> # => /another <%= another.page_path(@page) %> # => /another/pages/1

Slide 28

Slide 28 text

Get Smarter

Slide 29

Slide 29 text

Inside the Engine

Slide 30

Slide 30 text

In your App

Slide 31

Slide 31 text

Department of Redundancy Department…

Slide 32

Slide 32 text

Migrations Sharing Structure across Projects

Slide 33

Slide 33 text

Namespaced Tables

Slide 34

Slide 34 text

From Gem to Project $ rake another:install:migrations • Copies all files from db/migrate into a project • Won’t overwrite • Retimestamps

Slide 35

Slide 35 text

After installing 2 1

Slide 36

Slide 36 text

For multiple engines $ rake railties:install:migrations

Slide 37

Slide 37 text

Configuratin’ Provide Reasonable Defaults

Slide 38

Slide 38 text

http://edgeguides.rubyonrails.org/configuring.html

Slide 39

Slide 39 text

Initializers

Slide 40

Slide 40 text

Adding behavior to Rails

Slide 41

Slide 41 text

Middleware Stack

Slide 42

Slide 42 text

“Suggested” Generators

Slide 43

Slide 43 text

Make your engine configurable

Slide 44

Slide 44 text

Be Nice Make life easy for new users

Slide 45

Slide 45 text

Write a Readme gem "another" $ bundle install $ rake another:install:migrations $ rake db:migrate mount Another::Engine => "/another"

Slide 46

Slide 46 text

Improve Installation gem "another” $ rail g another:install

Slide 47

Slide 47 text

Generators

Slide 48

Slide 48 text

Asset Pipeline Packaging the view

Slide 49

Slide 49 text

Before /public $ rake copy:assets

Slide 50

Slide 50 text

After Project gem "ckeditor_rails", "3.6.2.2"

Slide 51

Slide 51 text

My Favorite Key

Slide 52

Slide 52 text

Working Together Ups and Downs of multiple engines

Slide 53

Slide 53 text

Common stacks are ok My Site BrowerCMS News Blog

Slide 54

Slide 54 text

Conventions $ rails g cms:install bcms_news

Slide 55

Slide 55 text

Common Installer

Slide 56

Slide 56 text

The vision Your Engine Rails Admin

Slide 57

Slide 57 text

Challenges A. Incompatible dependencies B. Rails has no common security model C. Mismatched user interfaces

Slide 58

Slide 58 text

A. Incompatible Dependencies Devise =2.1.1 ~>2.0.0

Slide 59

Slide 59 text

Solving Dependencies • Minimize dependencies • Follow rational versioning policy: o  Rubygems ‘recommended’ o  Major.Minor.Build (i.e. 3.5.4) §  Major – Backwards incompatible §  Minor – Backwards compatible, new features §  Build – Backwards compatible, details • Robustness Principle – Be liberal in what you accept o  ~> 2.0 better than ~>2.0.0

Slide 60

Slide 60 text

B. No common security model Your App Restful Auth Devise current_user?

Slide 61

Slide 61 text

Authentication Who are you, really?

Slide 62

Slide 62 text

Authorization Are you allowed to do that?

Slide 63

Slide 63 text

I don’t care, just pick something…

Slide 64

Slide 64 text

C. Mismatched UIs

Slide 65

Slide 65 text

Recap What I hope you learned

Slide 66

Slide 66 text

Engines are the Future, and Awesome.

Slide 67

Slide 67 text

Reduce errors from duplication

Slide 68

Slide 68 text

When done right…

Slide 69

Slide 69 text

The End Thanks! Feedback? @peakpg

Slide 70

Slide 70 text

Attributions http://dilbert.com/strips/comic/2009-12-03/ http://trevorlibrarian.wordpress.com/2012/04/04/safety-first/ http://www.flickr.com/photos/rosenfeldmedia/4459977458/sizes/o/in/photostream/ https://www.ruby-toolbox.com/ http://www.flickr.com/photos/7571290@N05/440978769/sizes/m/in/photostream/ http://www.spreecommerce.com http://www.flickr.com/photos/verseguru/168920451/sizes/o/in/photostream/ http://www.flickr.com/photos/richardgiles/5284155911/sizes/o/in/photostream/ http://www.flickr.com/photos/museemccordmuseum/2918567169/sizes/o/in/photostream/ http://www.flickr.com/photos/whisperwolf/4487007569/