SlideShare a Scribd company logo
Rails Engines
Hi, I’m Josh



I work here ->


                 lvlsvn.com
We Build Ruby
Apps
And a bunch of other stuff
We also do design
We also do design


The designers had NOTHING to do with my
slides.
Here’s the Problem

Even custom applications want to be able
to manage their static pages
  Think Contact Us, Team Bios, etc.
Wordpress, Drupal even Locomotive are
great but heavy weight
What we needed

A mini-cms
Uses our existing rails views
No subdomaining, feels like part of the app
Works with our authentication setup
Great for 4 or 5 pages of content
Options
High Voltage
  https://github.com/thoughtbot/high_voltage
  Great for devs, not great for the customer
Locomotive Engine
  http://locomotivecms.com/
  Awesome but heavyweight, assumes
  their template style
Exstatic
https://github.com/LevelSeven/exstatic
WARNING: Not even close to finished


Works w/ devise
Mounts in another application
Inherits that application’s layouts
But how?


Rails Engines
  obviously, otherwise this talk wouldn’t
  make much sense
What are Rails
Engines?
Lesser known feature, started in Rails 3.0
Mini-application that can be added to a
larger rails app
Replace plugins
Are a subset of Railties, so you can use
railtie stuff (like generators, rake tasks, etc.)
Why?
Code reuse
  Share functional components
  Share business logic
Isolation
Distribution
  Open Source part of a private app
Creating an Engine
2 Ways
  Rails 3.1 or greater
    > rails plugin new
  Rails 3.0
    Use enginex by Jose Valim
         https://github.com/josevalim/enginex
$> rails plugin new depot_engine --dummy-
path=spec/dummy --skip-test-unit --mountable --skip-
bundle




...
$> rails plugin new depot_engine --dummy-path=spec/
dummy --skip-test-unit --mountable --skip-bundle
       Creates a new Rails Engine with the name
                    DepotEngine
$> rails plugin new depot_engine --dummy-path=spec/
 dummy --skip-test-unit --mountable --skip-bundle

       Creates a fake rails app in the RSpec path
$> rails plugin new depot_engine --dummy-path=spec/
 dummy --skip-test-unit --mountable --skip-bundle

    Sets the engine to be “mountable” as opposed
                       to “full”

   Full - the parent inherits routes from the engine
   and can directly access it’s components
   (controllers, models, etc.)

   Mountable - the engine’s namespace is isolated
   and draws its own routes. It mounts at a path:

   mount DepotEngine::Engine => “/store”, :as => “store”
Rails engines
Setting Up RSpec
Add rspec-rails to development
dependencies in gemspec
bundle install
rails g rspec:install
Set rspec as test_framework in engine.rb
  lib/engine_name/engine.rb
Testing through
dummy
rails plugin new automatically creates a
dummy app
Rails.application is available to test
initializers
Rails.application.routes will get you dummy
app routes
  More in a bit
Depot as an Engine


Depot application
github.com/levelseven/depot_engine
Migration Pitfalls
You have to use the engine name
everywhere
  rails g migration
  add_foo_to_engine_name_table_name
To copy migrations into dummy app cd into
dummy and run
  rake engine_name:install:migrations
Asset Pipeline
Pitfalls
Assets have to be preceeded by
engine_name in view helpers
  image_tag(‘engine_name/logo.png’)
Copy assets group in Gemfile from normal
rails app into Gemfile in engine, then
bundle install in dummy app
View Hints

You can reference parent via main_app
helper
  i.e. link_to “Home”, main_app.root_path
Use the parent’s layouts by adding layout
“application” to ApplicationController in
engine
View Hints
In parent app use
engine_name.path_helper to link into
engine
  link_to “Store”, depot_engine.root_path
You can override views by putting them in
  app/views/engine_name/controller/view_
  name
Rake Tasks

Rake tasks can be added in lib/tasks
  Show up in parent application
  Can be run in rails engine via app
  namespace
Other Stuff

Custom Rails Generators
Injecting routes into the parent
Config options
Initializers
etc. etc.
Exstatic -
Authorization

Uses a config option to call an
authentication method
Setup in initializer
spec/controllers/pages_controller_spec.rb
Exstatic -
Generators

Used for migrations but no longer needed
You can however setup a
rails_engine:install to automate setup
Exstatic - Dynamic
Routes
Can add routes directly into parent app
  lib/exstatic/engine.rb
Can also ensure you aren’t overwriting a
route that is defined by parent
  lib/exstatic/validators/nonexistant_path_v
  alidator.rb
Resources

http://edgeguides.rubyonrails.org/engines.htm
http://keithschacht.com/creating-a-rails-3-eng
http://coding.smashingmagazine.com/2011/0
http://viget.com/extend/rails-engine-testing-w
Gems I “borrowed”
from
Devise
  https://github.com/plataformatec/devise
High Voltage
  https://github.com/thoughtbot/high_voltag
  e

More Related Content

PPTX
Rails engine
KEY
Cocoa on-rails-3rd
PDF
Lightening a component based Rails architecture
PPTX
My journey and learnings using mule esb part 1
PDF
Pundit
PDF
Building a WordPress plugin
PPTX
Integrate to retrieve data microsoft azure
PPTX
Java spring mysql without hibernate(2) (1)
Rails engine
Cocoa on-rails-3rd
Lightening a component based Rails architecture
My journey and learnings using mule esb part 1
Pundit
Building a WordPress plugin
Integrate to retrieve data microsoft azure
Java spring mysql without hibernate(2) (1)

What's hot (20)

PPTX
Devise and Rails
PPTX
Mule maven
PDF
How to build Client Side Applications with WordPress and WP-API | #wcmia
PDF
Building WordPress Client Side Applications with WP and WP-API - #wcmia
PPTX
Deploy with maven
PPTX
Expose web service
PDF
Client Side Applications with WP-API WordPress - WCMTL 2015
PPTX
Web Controls Set-1
PDF
Rails Plugin Development 101 (...and some...)
PDF
Apache serversideincludes(ssi)inae mpages
PPTX
SharePoint 2010 Training Session 5
PDF
Python Ireland Nov 2009 Talk - Appengine
PDF
Markdown tutorial how to add markdown to rails app using redcarpet and codera...
PPTX
Neoito — Routing and navigation in Angular
PPTX
WP REST API - Building a simple Web Application
PPTX
AEM 6.0 - Author UI Customization & Features
PPT
Installing mule
PPTX
EVOLVE'16 | Enhance | Paul McMahon | Approaches to Leveraging AEM Within a Si...
KEY
PAAS Lightning Talk
PPTX
Integrate with facebook connector
Devise and Rails
Mule maven
How to build Client Side Applications with WordPress and WP-API | #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Deploy with maven
Expose web service
Client Side Applications with WP-API WordPress - WCMTL 2015
Web Controls Set-1
Rails Plugin Development 101 (...and some...)
Apache serversideincludes(ssi)inae mpages
SharePoint 2010 Training Session 5
Python Ireland Nov 2009 Talk - Appengine
Markdown tutorial how to add markdown to rails app using redcarpet and codera...
Neoito — Routing and navigation in Angular
WP REST API - Building a simple Web Application
AEM 6.0 - Author UI Customization & Features
Installing mule
EVOLVE'16 | Enhance | Paul McMahon | Approaches to Leveraging AEM Within a Si...
PAAS Lightning Talk
Integrate with facebook connector
Ad

Viewers also liked (9)

PPTX
"Kак писать правильные письма" by Anton Zolotaryov
PDF
Pengantar Ruby on Rails
PPTX
Алексей Серый "Ruby on Rails for dummies"
PDF
Ruby - Dünyanın En Güzel Programlama Dili
PPTX
Object-oriented in ruby
ODP
Pardus ve Yenilikleri
ODP
Kadinlar ve Linux
ODP
Ruby Programlama Dili'nin Temelleri
ODP
Ruby Programlama Dili
"Kак писать правильные письма" by Anton Zolotaryov
Pengantar Ruby on Rails
Алексей Серый "Ruby on Rails for dummies"
Ruby - Dünyanın En Güzel Programlama Dili
Object-oriented in ruby
Pardus ve Yenilikleri
Kadinlar ve Linux
Ruby Programlama Dili'nin Temelleri
Ruby Programlama Dili
Ad

Similar to Rails engines (20)

PPTX
Rails Engine :: modularize you app
KEY
How to set up and test a Rails 3 Engine
PDF
A story about gemified engines
PDF
Rails engines
PDF
Rails Engines
PDF
Rails Engines - A presentation for the 22nd Athens Ruby Meetup
PPTX
Rails Engine | Modular application
PPTX
Rails Engine Patterns
PPT
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
PDF
Rails Engines as a way to Micro services
PDF
Rails engines in large apps
PPTX
The Rails Engine That Could - In Motion
PDF
Rails::Engine
KEY
Plug it on!... with railties
PDF
Railties
PDF
feature flagging with rails engines v0.2
PPTX
The Rails Engine That Could
PDF
Ecommerce as an Engine
ODP
DiUS Computing Lca Rails Final
PDF
TorqueBox
Rails Engine :: modularize you app
How to set up and test a Rails 3 Engine
A story about gemified engines
Rails engines
Rails Engines
Rails Engines - A presentation for the 22nd Athens Ruby Meetup
Rails Engine | Modular application
Rails Engine Patterns
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Rails Engines as a way to Micro services
Rails engines in large apps
The Rails Engine That Could - In Motion
Rails::Engine
Plug it on!... with railties
Railties
feature flagging with rails engines v0.2
The Rails Engine That Could
Ecommerce as an Engine
DiUS Computing Lca Rails Final
TorqueBox

Rails engines

  • 2. Hi, I’m Josh I work here -> lvlsvn.com
  • 3. We Build Ruby Apps And a bunch of other stuff
  • 4. We also do design
  • 5. We also do design The designers had NOTHING to do with my slides.
  • 6. Here’s the Problem Even custom applications want to be able to manage their static pages Think Contact Us, Team Bios, etc. Wordpress, Drupal even Locomotive are great but heavy weight
  • 7. What we needed A mini-cms Uses our existing rails views No subdomaining, feels like part of the app Works with our authentication setup Great for 4 or 5 pages of content
  • 8. Options High Voltage https://github.com/thoughtbot/high_voltage Great for devs, not great for the customer Locomotive Engine http://locomotivecms.com/ Awesome but heavyweight, assumes their template style
  • 9. Exstatic https://github.com/LevelSeven/exstatic WARNING: Not even close to finished Works w/ devise Mounts in another application Inherits that application’s layouts
  • 10. But how? Rails Engines obviously, otherwise this talk wouldn’t make much sense
  • 11. What are Rails Engines? Lesser known feature, started in Rails 3.0 Mini-application that can be added to a larger rails app Replace plugins Are a subset of Railties, so you can use railtie stuff (like generators, rake tasks, etc.)
  • 12. Why? Code reuse Share functional components Share business logic Isolation Distribution Open Source part of a private app
  • 13. Creating an Engine 2 Ways Rails 3.1 or greater > rails plugin new Rails 3.0 Use enginex by Jose Valim https://github.com/josevalim/enginex
  • 14. $> rails plugin new depot_engine --dummy- path=spec/dummy --skip-test-unit --mountable --skip- bundle ...
  • 15. $> rails plugin new depot_engine --dummy-path=spec/ dummy --skip-test-unit --mountable --skip-bundle Creates a new Rails Engine with the name DepotEngine
  • 16. $> rails plugin new depot_engine --dummy-path=spec/ dummy --skip-test-unit --mountable --skip-bundle Creates a fake rails app in the RSpec path
  • 17. $> rails plugin new depot_engine --dummy-path=spec/ dummy --skip-test-unit --mountable --skip-bundle Sets the engine to be “mountable” as opposed to “full” Full - the parent inherits routes from the engine and can directly access it’s components (controllers, models, etc.) Mountable - the engine’s namespace is isolated and draws its own routes. It mounts at a path: mount DepotEngine::Engine => “/store”, :as => “store”
  • 19. Setting Up RSpec Add rspec-rails to development dependencies in gemspec bundle install rails g rspec:install Set rspec as test_framework in engine.rb lib/engine_name/engine.rb
  • 20. Testing through dummy rails plugin new automatically creates a dummy app Rails.application is available to test initializers Rails.application.routes will get you dummy app routes More in a bit
  • 21. Depot as an Engine Depot application github.com/levelseven/depot_engine
  • 22. Migration Pitfalls You have to use the engine name everywhere rails g migration add_foo_to_engine_name_table_name To copy migrations into dummy app cd into dummy and run rake engine_name:install:migrations
  • 23. Asset Pipeline Pitfalls Assets have to be preceeded by engine_name in view helpers image_tag(‘engine_name/logo.png’) Copy assets group in Gemfile from normal rails app into Gemfile in engine, then bundle install in dummy app
  • 24. View Hints You can reference parent via main_app helper i.e. link_to “Home”, main_app.root_path Use the parent’s layouts by adding layout “application” to ApplicationController in engine
  • 25. View Hints In parent app use engine_name.path_helper to link into engine link_to “Store”, depot_engine.root_path You can override views by putting them in app/views/engine_name/controller/view_ name
  • 26. Rake Tasks Rake tasks can be added in lib/tasks Show up in parent application Can be run in rails engine via app namespace
  • 27. Other Stuff Custom Rails Generators Injecting routes into the parent Config options Initializers etc. etc.
  • 28. Exstatic - Authorization Uses a config option to call an authentication method Setup in initializer spec/controllers/pages_controller_spec.rb
  • 29. Exstatic - Generators Used for migrations but no longer needed You can however setup a rails_engine:install to automate setup
  • 30. Exstatic - Dynamic Routes Can add routes directly into parent app lib/exstatic/engine.rb Can also ensure you aren’t overwriting a route that is defined by parent lib/exstatic/validators/nonexistant_path_v alidator.rb
  • 32. Gems I “borrowed” from Devise https://github.com/plataformatec/devise High Voltage https://github.com/thoughtbot/high_voltag e