Engines Lightning Talk

28
The Rails Engine That Could? Saturday, April 28, 2012

description

A lightning talk presented to BostonRB in April of 2012

Transcript of Engines Lightning Talk

Page 1: Engines Lightning Talk

The Rails Engine That

Could?

Saturday, April 28, 2012

Page 2: Engines Lightning Talk

Hi!

dpickett on Twitter

www.launchware.com on the InterTubes

Hiring

Saturday, April 28, 2012

Page 3: Engines Lightning Talk

• A Brief History

• A Rails Engine In Rails 3.1

• Implementation Gotchas

• Deployment Gotchas With Heroku

• Conclusions

What I Can Tell You About Rails Engines

Saturday, April 28, 2012

Page 4: Engines Lightning Talk

• Components in Rails Beta and Rails 1

• Components removed in Rails 2

Out With The Old...

Saturday, April 28, 2012

Page 5: Engines Lightning Talk

...In With the New

• Engines in Rails 3

• Enginex as part of Rails 3.1

Saturday, April 28, 2012

Page 6: Engines Lightning Talk

Generating

rails generate plugin thomas --mountable

Saturday, April 28, 2012

Page 7: Engines Lightning Talk

Saturday, April 28, 2012

Page 8: Engines Lightning Talk

Saturday, April 28, 2012

Page 9: Engines Lightning Talk

Manage Dependencies In Your Gemspec

Shift Your Perspective

Saturday, April 28, 2012

Page 10: Engines Lightning Talk

Shifting Perspectives

• Think like an API designer

• Favor composition over inheritance

• Obey SOLID principles for more maintainable code

Saturday, April 28, 2012

Page 11: Engines Lightning Talk

Maintain Perspective

• Do as you would normally:

• Write migrations

• Seed data

• Run rake tasks

• Run generators

Saturday, April 28, 2012

Page 12: Engines Lightning Talk

Gotchas

Saturday, April 28, 2012

Page 13: Engines Lightning Talk

Routes

• Routes are loaded where you mount them

• Only one opportunity for placement

• No flexibility to divide up routes for fine tuning priorities

Saturday, April 28, 2012

Page 14: Engines Lightning Talk

Namespacing• Namespace Your Engine - it will save you lots of

pain.

• If you must in your consuming application, use ActiveSupport’s require_dependency to override:

require_dependency Thomas::Engine.root.join(“app/models/user”).to_s

class User < ActiveRecord::Base#override the engine here

end

Saturday, April 28, 2012

Page 15: Engines Lightning Talk

Routes

• Routes are loaded where you mount them

• Only one opportunity for placement

• No flexibility to divide up routes for fine tuning priorities

Saturday, April 28, 2012

Page 16: Engines Lightning Talk

Consumer::Application do Thomas::Routes::UserRoutes.draw(self) #custom routes here Thomas::Routes::ScheduleRoutes.draw(self)end

module Thomas module Routes module UserRoutes def self.draw(map) map.instance_eval do resources :user... end end end endendSaturday, April 28, 2012

Page 17: Engines Lightning Talk

Named Routes Helpers

• If you don’t namespace, I found these to be incredibly problematic

• You must include helpers in your application controller and in your request specs/tests

Saturday, April 28, 2012

Page 18: Engines Lightning Talk

class ApplicationController < ActionController::Base include Thomas::Engine.routes.url_helpers include ActionDispatch::Routing::PolymorphicRoutesend

You’ll use these two lines a lot.

Apply when you get an undefined *_path or *_url method

Saturday, April 28, 2012

Page 19: Engines Lightning Talk

Saturday, April 28, 2012

Page 20: Engines Lightning Talk

Devise

• Wiring I18n load paths is deferred until the app comes up, disallowing override ability

Saturday, April 28, 2012

Page 21: Engines Lightning Talk

class Thomas::Engine < Rails::Engine config.after_initialize do Rails.application.config.after_initialize do paths = I18n.load_path.delete_if do |p| p =~ /devise\-/ end #put devise’s default locales in its place I18n.load_path = paths + I18n.load_path I18n.reload!

endend

Saturday, April 28, 2012

Page 22: Engines Lightning Talk

Inherited ResourcesAnd Responders

Doesn’t WorkDrops Your Responder

Saturday, April 28, 2012

Page 23: Engines Lightning Talk

Saturday, April 28, 2012

Page 24: Engines Lightning Talk

Wrong Thomas......Go B’s

Saturday, April 28, 2012

Page 25: Engines Lightning Talk

class ApplicationController < ActionController::Base def self.engine_inherit_resources inherit_resources self.responder = MobileResponder end endend

Saturday, April 28, 2012

Page 26: Engines Lightning Talk

Deploying Private Gems To Heroku

• Bundle with your github credentials in plaintext

• Maintain your own gem server

• Write a rake task to vendor the gem and commit when deploying

• NEW: http://www.gemfury.com/

Saturday, April 28, 2012

Page 27: Engines Lightning Talk

Overall, Engines Are Great

• There’s still room for improvement, but they’re awesome once you get going

• They will challenge you as a developer and as a Rails Framework user

Saturday, April 28, 2012

Page 28: Engines Lightning Talk

Thanks!

• I’ll post slides @ www.launchware.com

• Chat me up on Twitter: @dpickett

• Chat me up on IRC: dpickett in #boston.rb

• We’re hiring!

Saturday, April 28, 2012