際際滷

際際滷Share a Scribd company logo
Rails Engines
Hi, Im 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.
Heres 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 applications layouts
But how?


Rails Engines
  obviously, otherwise this talk wouldnt
  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 its components
   (controllers, models, etc.)

   Mountable - the engines 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 parents 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 arent 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
Ad

Recommended

Rails engine
Rails engine
Jyaasa Technologies
Cocoa on-rails-3rd
Cocoa on-rails-3rd
Xiaochun Shen
Lightening a component based Rails architecture
Lightening a component based Rails architecture
Enrico Teotti
My journey and learnings using mule esb part 1
My journey and learnings using mule esb part 1
Alex Fernandez
Pundit
Pundit
Bruce White
Building a WordPress plugin
Building a WordPress plugin
Keanan Koppenhaver
Integrate to retrieve data microsoft azure
Integrate to retrieve data microsoft azure
Son Nguyen
Java spring mysql without hibernate(2) (1)
Java spring mysql without hibernate(2) (1)
AmedJacobReza
Devise and Rails
Devise and Rails
William Leeper
Mule maven
Mule maven
Thang Loi
How to build Client Side Applications with WordPress and WP-API | #wcmia
How to build Client Side Applications with WordPress and WP-API | #wcmia
Roy Sivan
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Roy Sivan
Deploy with maven
Deploy with maven
Son Nguyen
Expose web service
Expose web service
Son Nguyen
Client Side Applications with WP-API WordPress - WCMTL 2015
Client Side Applications with WP-API WordPress - WCMTL 2015
Roy Sivan
Web Controls Set-1
Web Controls Set-1
sunmitraeducation
Rails Plugin Development 101 (...and some...)
Rails Plugin Development 101 (...and some...)
Jim Myhrberg
Apache serversideincludes(ssi)inae mpages
Apache serversideincludes(ssi)inae mpages
Deepak Bhardwaj
SharePoint 2010 Training Session 5
SharePoint 2010 Training Session 5
Usman Zafar Malik
Python Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - Appengine
Python 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...
Katy Slemon
Neoito Routing and navigation in Angular
Neoito Routing and navigation in Angular
Neoito
WP REST API - Building a simple Web Application
WP REST API - Building a simple Web Application
Edmund Chan
AEM 6.0 - Author UI Customization & Features
AEM 6.0 - Author UI Customization & Features
Abhinit Bhatnagar
Installing mule
Installing mule
JavierMarRas
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 The Adobe Digital Marketing Community
PAAS Lightning Talk
PAAS Lightning Talk
Shimi (Sam) Halperin
Integrate with facebook connector
Integrate with facebook connector
Son Nguyen
"K舒从 仗亳舒 仗舒于亳仍仆亠 仗亳仄舒" by Anton Zolotaryov
"K舒从 仗亳舒 仗舒于亳仍仆亠 仗亳仄舒" by Anton Zolotaryov
EPAM Systems
Pengantar Ruby on Rails
Pengantar Ruby on Rails
Ashari Juang

More Related Content

What's hot (20)

Devise and Rails
Devise and Rails
William Leeper
Mule maven
Mule maven
Thang Loi
How to build Client Side Applications with WordPress and WP-API | #wcmia
How to build Client Side Applications with WordPress and WP-API | #wcmia
Roy Sivan
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Roy Sivan
Deploy with maven
Deploy with maven
Son Nguyen
Expose web service
Expose web service
Son Nguyen
Client Side Applications with WP-API WordPress - WCMTL 2015
Client Side Applications with WP-API WordPress - WCMTL 2015
Roy Sivan
Web Controls Set-1
Web Controls Set-1
sunmitraeducation
Rails Plugin Development 101 (...and some...)
Rails Plugin Development 101 (...and some...)
Jim Myhrberg
Apache serversideincludes(ssi)inae mpages
Apache serversideincludes(ssi)inae mpages
Deepak Bhardwaj
SharePoint 2010 Training Session 5
SharePoint 2010 Training Session 5
Usman Zafar Malik
Python Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - Appengine
Python 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...
Katy Slemon
Neoito Routing and navigation in Angular
Neoito Routing and navigation in Angular
Neoito
WP REST API - Building a simple Web Application
WP REST API - Building a simple Web Application
Edmund Chan
AEM 6.0 - Author UI Customization & Features
AEM 6.0 - Author UI Customization & Features
Abhinit Bhatnagar
Installing mule
Installing mule
JavierMarRas
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 The Adobe Digital Marketing Community
PAAS Lightning Talk
PAAS Lightning Talk
Shimi (Sam) Halperin
Integrate with facebook connector
Integrate with facebook connector
Son Nguyen
Mule maven
Mule maven
Thang Loi
How to build Client Side Applications with WordPress and WP-API | #wcmia
How to build Client Side Applications with WordPress and WP-API | #wcmia
Roy Sivan
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Building WordPress Client Side Applications with WP and WP-API - #wcmia
Roy Sivan
Deploy with maven
Deploy with maven
Son Nguyen
Expose web service
Expose web service
Son Nguyen
Client Side Applications with WP-API WordPress - WCMTL 2015
Client Side Applications with WP-API WordPress - WCMTL 2015
Roy Sivan
Rails Plugin Development 101 (...and some...)
Rails Plugin Development 101 (...and some...)
Jim Myhrberg
Apache serversideincludes(ssi)inae mpages
Apache serversideincludes(ssi)inae mpages
Deepak Bhardwaj
SharePoint 2010 Training Session 5
SharePoint 2010 Training Session 5
Usman Zafar Malik
Python Ireland Nov 2009 Talk - Appengine
Python Ireland Nov 2009 Talk - Appengine
Python 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...
Katy Slemon
Neoito Routing and navigation in Angular
Neoito Routing and navigation in Angular
Neoito
WP REST API - Building a simple Web Application
WP REST API - Building a simple Web Application
Edmund Chan
AEM 6.0 - Author UI Customization & Features
AEM 6.0 - Author UI Customization & Features
Abhinit Bhatnagar
Installing mule
Installing mule
JavierMarRas
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 The Adobe Digital Marketing Community
Integrate with facebook connector
Integrate with facebook connector
Son Nguyen

Viewers also liked (9)

"K舒从 仗亳舒 仗舒于亳仍仆亠 仗亳仄舒" by Anton Zolotaryov
"K舒从 仗亳舒 仗舒于亳仍仆亠 仗亳仄舒" by Anton Zolotaryov
EPAM Systems
Pengantar Ruby on Rails
Pengantar Ruby on Rails
Ashari Juang
仍亠从亠亶 弌亠亶 "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 Dili
Serdar Dogruyol
Object-oriented in ruby
Object-oriented in ruby
Ng畛c Nguy畛n
Pardus ve Yenilikleri
Pardus ve Yenilikleri
pinguar
Kadinlar ve Linux
Kadinlar ve Linux
pinguar
Ruby Programlama Dili'nin Temelleri
Ruby Programlama Dili'nin Temelleri
pinguar
Ruby Programlama Dili
Ruby Programlama Dili
pinguar
"K舒从 仗亳舒 仗舒于亳仍仆亠 仗亳仄舒" by Anton Zolotaryov
"K舒从 仗亳舒 仗舒于亳仍仆亠 仗亳仄舒" by Anton Zolotaryov
EPAM Systems
Pengantar Ruby on Rails
Pengantar Ruby on Rails
Ashari Juang
仍亠从亠亶 弌亠亶 "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 Dili
Serdar Dogruyol
Object-oriented in ruby
Object-oriented in ruby
Ng畛c Nguy畛n
Pardus ve Yenilikleri
Pardus ve Yenilikleri
pinguar
Kadinlar ve Linux
Kadinlar ve Linux
pinguar
Ruby Programlama Dili'nin Temelleri
Ruby Programlama Dili'nin Temelleri
pinguar
Ruby Programlama Dili
Ruby Programlama Dili
pinguar
Ad

Similar to Rails engines (20)

Rails Engine | Modular application
Rails Engine | Modular application
mirrec
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Andy Maleh
The Rails Engine That Could - In Motion
The Rails Engine That Could - In Motion
Andy Maleh
Rails Engine Patterns
Rails Engine Patterns
Andy Maleh
Useful Rails Plugins
Useful Rails Plugins
navjeet
Plug it on!... with railties
Plug it on!... with railties
rails.mx
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoas
Zeid Hassan
Rails review
Rails review
Alan Hecht
1 app 2 developers 3 servers
1 app 2 developers 3 servers
Mark Myers
Rails::Engine
Rails::Engine
Flavian Missi
Create a new project in ROR
Create a new project in ROR
akankshita satapathy
feature flagging with rails engines v0.2
feature flagging with rails engines v0.2
Enrico Teotti
Implementing a file manager in ASP.NET Core 8.0
Implementing a file manager in ASP.NET Core 8.0
GleamTech Dev
TorqueBox
TorqueBox
bobmcwhirter
Dev streams2
Dev streams2
David Mc Donagh
RoR 101: Session 2
RoR 101: Session 2
Rory Gianni
React JS CONCEPT AND DETAILED EXPLANATION
React JS CONCEPT AND DETAILED EXPLANATION
harshavardhanjuttika
Build and deploy Python Django project
Build and deploy Python Django project
Xiaoqi Zhao
How to set up and test a Rails 3 Engine
How to set up and test a Rails 3 Engine
nicholasf
vitepress-en.pdf
vitepress-en.pdf
ssuser65180a
Rails Engine | Modular application
Rails Engine | Modular application
mirrec
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Andy Maleh
The Rails Engine That Could - In Motion
The Rails Engine That Could - In Motion
Andy Maleh
Rails Engine Patterns
Rails Engine Patterns
Andy Maleh
Useful Rails Plugins
Useful Rails Plugins
navjeet
Plug it on!... with railties
Plug it on!... with railties
rails.mx
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoas
Zeid Hassan
Rails review
Rails review
Alan Hecht
1 app 2 developers 3 servers
1 app 2 developers 3 servers
Mark Myers
feature flagging with rails engines v0.2
feature flagging with rails engines v0.2
Enrico Teotti
Implementing a file manager in ASP.NET Core 8.0
Implementing a file manager in ASP.NET Core 8.0
GleamTech Dev
RoR 101: Session 2
RoR 101: Session 2
Rory Gianni
React JS CONCEPT AND DETAILED EXPLANATION
React JS CONCEPT AND DETAILED EXPLANATION
harshavardhanjuttika
Build and deploy Python Django project
Build and deploy Python Django project
Xiaoqi Zhao
How to set up and test a Rails 3 Engine
How to set up and test a Rails 3 Engine
nicholasf
vitepress-en.pdf
vitepress-en.pdf
ssuser65180a
Ad

Rails engines

  • 2. Hi, Im 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. Heres 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 applications layouts
  • 10. But how? Rails Engines obviously, otherwise this talk wouldnt 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 its components (controllers, models, etc.) Mountable - the engines 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 parents 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 arent 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