SlideShare une entreprise Scribd logo
1  sur  32
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”
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

Contenu connexe

Tendances

How to build Client Side Applications with WordPress and WP-API | #wcmia
How to build Client Side Applications with WordPress and WP-API | #wcmiaHow to build Client Side Applications with WordPress and WP-API | #wcmia
How to build Client Side Applications with WordPress and WP-API | #wcmiaRoy Sivan
 
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmiaBuilding WordPress Client Side Applications with WP and WP-API - #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmiaRoy Sivan
 
Deploy with maven
Deploy with mavenDeploy with maven
Deploy with mavenSon Nguyen
 
Expose web service
Expose web serviceExpose web service
Expose web serviceSon Nguyen
 
Client Side Applications with WP-API WordPress - WCMTL 2015
Client Side Applications with WP-API WordPress - WCMTL 2015Client Side Applications with WP-API WordPress - WCMTL 2015
Client Side Applications with WP-API WordPress - WCMTL 2015Roy Sivan
 
Rails Plugin Development 101 (...and some...)
Rails Plugin Development 101 (...and some...)Rails Plugin Development 101 (...and some...)
Rails Plugin Development 101 (...and some...)Jim Myhrberg
 
Apache serversideincludes(ssi)inae mpages
Apache serversideincludes(ssi)inae mpagesApache serversideincludes(ssi)inae mpages
Apache serversideincludes(ssi)inae mpagesDeepak Bhardwaj
 
SharePoint 2010 Training Session 5
SharePoint 2010 Training Session 5SharePoint 2010 Training Session 5
SharePoint 2010 Training Session 5Usman Zafar Malik
 
Python Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - AppenginePython Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - AppenginePython Ireland
 
Markdown tutorial how to add markdown to rails app using redcarpet and codera...
Markdown tutorial how to add markdown to rails app using redcarpet and codera...Markdown tutorial how to add markdown to rails app using redcarpet and codera...
Markdown tutorial how to add markdown to rails app using redcarpet and codera...Katy Slemon
 
Neoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito — Routing and navigation in Angular
Neoito — Routing and navigation in AngularNeoito
 
WP REST API - Building a simple Web Application
WP REST API - Building a simple Web ApplicationWP REST API - Building a simple Web Application
WP REST API - Building a simple Web ApplicationEdmund Chan
 
AEM 6.0 - Author UI Customization & Features
AEM 6.0 - Author UI Customization & FeaturesAEM 6.0 - Author UI Customization & Features
AEM 6.0 - Author UI Customization & FeaturesAbhinit Bhatnagar
 
Integrate with facebook connector
Integrate with facebook connectorIntegrate with facebook connector
Integrate with facebook connectorSon Nguyen
 

Tendances (20)

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

En vedette

"Kак писать правильные письма" by Anton Zolotaryov
"Kак писать правильные письма" by Anton Zolotaryov"Kак писать правильные письма" by Anton Zolotaryov
"Kак писать правильные письма" by Anton ZolotaryovEPAM Systems
 
Pengantar Ruby on Rails
Pengantar Ruby on RailsPengantar Ruby on Rails
Pengantar Ruby on RailsAshari Juang
 
Алексей Серый "Ruby on Rails for dummies"
Алексей Серый "Ruby on Rails for dummies"Алексей Серый "Ruby on Rails for dummies"
Алексей Серый "Ruby on Rails for dummies"EPAM Systems
 
Ruby - Dünyanın En Güzel Programlama Dili
Ruby - Dünyanın En Güzel Programlama DiliRuby - Dünyanın En Güzel Programlama Dili
Ruby - Dünyanın En Güzel Programlama DiliSerdar Dogruyol
 
Pardus ve Yenilikleri
Pardus ve YenilikleriPardus ve Yenilikleri
Pardus ve Yenilikleripinguar
 
Kadinlar ve Linux
Kadinlar ve LinuxKadinlar ve Linux
Kadinlar ve Linuxpinguar
 
Ruby Programlama Dili'nin Temelleri
Ruby Programlama Dili'nin TemelleriRuby Programlama Dili'nin Temelleri
Ruby Programlama Dili'nin Temelleripinguar
 
Ruby Programlama Dili
Ruby Programlama DiliRuby Programlama Dili
Ruby Programlama Dilipinguar
 

En vedette (9)

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

Similaire à Rails engines

Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular applicationmirrec
 
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012Andy Maleh
 
The Rails Engine That Could - In Motion
The Rails Engine That Could - In MotionThe Rails Engine That Could - In Motion
The Rails Engine That Could - In MotionAndy Maleh
 
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine PatternsAndy Maleh
 
Useful Rails Plugins
Useful Rails PluginsUseful Rails Plugins
Useful Rails Pluginsnavjeet
 
Plug it on!... with railties
Plug it on!... with railtiesPlug it on!... with railties
Plug it on!... with railtiesrails.mx
 
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoasRest web service_with_spring_hateoas
Rest web service_with_spring_hateoasZeid Hassan
 
1 app 2 developers 3 servers
1 app 2 developers 3 servers1 app 2 developers 3 servers
1 app 2 developers 3 serversMark Myers
 
feature flagging with rails engines v0.2
feature flagging with rails engines v0.2 feature flagging with rails engines v0.2
feature flagging with rails engines v0.2 Enrico Teotti
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2Rory Gianni
 
Build and deploy Python Django project
Build and deploy Python Django projectBuild and deploy Python Django project
Build and deploy Python Django projectXiaoqi Zhao
 
How to set up and test a Rails 3 Engine
How to set up and test a Rails 3 EngineHow to set up and test a Rails 3 Engine
How to set up and test a Rails 3 Enginenicholasf
 
Laravel 5 New Features
Laravel 5 New FeaturesLaravel 5 New Features
Laravel 5 New FeaturesJoe Ferguson
 
Ruby on rails integration testing with minitest and capybara
Ruby on rails integration testing with minitest and capybaraRuby on rails integration testing with minitest and capybara
Ruby on rails integration testing with minitest and capybaraAndolasoft Inc
 

Similaire à Rails engines (20)

Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
 
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
 
The Rails Engine That Could - In Motion
The Rails Engine That Could - In MotionThe Rails Engine That Could - In Motion
The Rails Engine That Could - In Motion
 
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine Patterns
 
Useful Rails Plugins
Useful Rails PluginsUseful Rails Plugins
Useful Rails Plugins
 
Plug it on!... with railties
Plug it on!... with railtiesPlug it on!... with railties
Plug it on!... with railties
 
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoasRest web service_with_spring_hateoas
Rest web service_with_spring_hateoas
 
Rails review
Rails reviewRails review
Rails review
 
1 app 2 developers 3 servers
1 app 2 developers 3 servers1 app 2 developers 3 servers
1 app 2 developers 3 servers
 
Rails::Engine
Rails::EngineRails::Engine
Rails::Engine
 
Create a new project in ROR
Create a new project in RORCreate a new project in ROR
Create a new project in ROR
 
feature flagging with rails engines v0.2
feature flagging with rails engines v0.2 feature flagging with rails engines v0.2
feature flagging with rails engines v0.2
 
TorqueBox
TorqueBoxTorqueBox
TorqueBox
 
Dev streams2
Dev streams2Dev streams2
Dev streams2
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Build and deploy Python Django project
Build and deploy Python Django projectBuild and deploy Python Django project
Build and deploy Python Django project
 
How to set up and test a Rails 3 Engine
How to set up and test a Rails 3 EngineHow to set up and test a Rails 3 Engine
How to set up and test a Rails 3 Engine
 
vitepress-en.pdf
vitepress-en.pdfvitepress-en.pdf
vitepress-en.pdf
 
Laravel 5 New Features
Laravel 5 New FeaturesLaravel 5 New Features
Laravel 5 New Features
 
Ruby on rails integration testing with minitest and capybara
Ruby on rails integration testing with minitest and capybaraRuby on rails integration testing with minitest and capybara
Ruby on rails integration testing with minitest and capybara
 

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”
  • 18.
  • 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