SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
Rails engines in
large apps
Created by /Enrico Teotti @agenteo
Start off with a small
application
and the app complexity will
grow over time
Start off with a big complex
application
ignoring complexity will hurt
you
In Ruby on Rails that is:
too many responsibilities in
active record models
long controller methods
helpers modules doing a
gazillion things
concerns
These poor decisions are sometime justified as:
"This is the Rails way!"
"Those are the Rails
conventions!"
"This is what a Rails developer
expect to see!"
bullshit
Rails gives conventions that fit small application domains but doesn't
have any for larger problems
How can Ruby on Rails engines
help?
Engines can drop in functionality in your Rails monolithic app
Devise
Kaminari
But they can do more that that
"Rails::Engine allows you to wrap a specific Rails application or subset of
functionality and share it with other applications or within a larger
packaged application. Since Rails 3.0, every Rails::Application is just an
engine, which allows for simple feature and application sharing."
Rails API
we will create very specific engine only used in this app
Domain Model
This is a domain model taken from Evan's book DDD
It is a simplified version of a real live problem
use Engines as building bricks
of the application!
First, create an integration test
in the main app
spec/features/ship_cargo_spec.rb
require 'spec_helper'
feature 'As a staff member I want to ship a cargo' do
scenario %{
Given I am on the cargo shipping page
When I fill in a customer
And I fill in a origin and destination
Then I want to see an itinerary } do
visit '/ship_cargo'
end
end
rspec
F
Failures:
1) As a staff member I want to ship a cargo
Given I am on the cargo shipping page
When I fill in a customer
And I fill in a origin and destination
Then I want to see an itinerary
Failure/Error: visit '/ship_cargo'
ActionController::RoutingError:
No route matches [GET] "/ship_cargo"
# ./spec/features/ship_cargo_spec.rb:10:in `block (2 levels) in <top (required)="">'
Finished in 0.00523 seconds
1 example, 1 failure
</top>
Create an engine
rails plugin new cargo_shipping --mountable -T --dummy-path=spec/dummy
You can create a nested folder
structure
If you want to be more formal about the separation of
responsibilities between the engines
The engine name will match ubiquitous language you share with
stakeholders
engines/
├── application_layer
├── domain_layer
│ ├── billing
│ ├── customer
│ └── shipping
└── presentation_layer
└── cargo_shipping
Main app Gemfile
gem 'cargo_shipping', path: 'engines/presentation_layer/cargo_shipping'
Cargo Shipping Engine
structure
engines/presentation_layer/cargo_shipping/
├── Gemfile
├── Gemfile.lock
├── MIT-LICENSE
├── README.rdoc
├── Rakefile
├── app
│ ├── assets
│ ├── controllers
│ ├── helpers
│ ├── mailers
│ └── views
├── cargo_shipping.gemspec
├── config
│ └── routes.rb
├── lib
│ ├── cargo_shipping
│ ├── cargo_shipping.rb
│ └── tasks
└── spec
└── dummy
CargoShipping Engine routes
engines/presentation_layer/cargo_shipping/config/routes.rb
module PresentationLayer
CargoShipping::Engine.routes.draw do
get 'ship_cargo', to: 'ship_cargo#new'
end
end
CargoShipping Engine
controller
engines/presentation_layer/cargo_shipping/app/controllers/cargo_shipping/ship_cargo_controller.rb
module CargoShipping
class ShipCargoController < ActionController::Base
def new
end
end
end
Run the test again!
F
Failures:
1) As a staff member I want to ship a cargo
Given I am on the cargo shipping page
When I fill in a customer
And I fill in a origin and destination
Then I want to see an itinerary
Failure/Error: visit '/ship_cargo'
ActionController::RoutingError:
No route matches [GET] "/ship_cargo"
# ./spec/features/ship_cargo_spec.rb:10:in `block (2 levels) in <top (required)="">'
Finished in 0.00523 seconds
1 example, 1 failure
</top>
We need to change the main
app routes!
config/routes.rb
Maersk::Application.routes.draw do
mount CargoShipping::Engine, at: "/"
end
rspec
.
Finished in 0.05205 seconds
1 example, 0 failures
CargoShipping Engine
engines/presentation_layer/cargo_shipping/app/controllers/cargo_shipping/ship_cargo_controller.rb
module CargoShipping
class ShipCargoController < ActionController::Base
def new
@customers = Customers::CustomerRepository.all
end
end
end
rspec
F
Failures:
1) As a staff member I want to ship a cargo
Given I am on the cargo shipping page
When I fill in a customer
And I fill in a origin and destination
Then I want to see an itinerary
Failure/Error: visit '/ship_cargo'
NameError:
uninitialized constant CargoShipping::ShipCargoController::Customers
# ./engines/presentation_layer/cargo_shipping/app/controllers/cargo_shipping/ship_cargo_controller.r
# ./spec/features/ship_cargo_spec.rb:10:in `block (2 levels) in <top (required)="">'
Finished in 0.04333 seconds
1 example, 1 failure
</top>
Create a domain layer engine
rails plugin new customers --mountable -T --dummy-path=spec/dummy
Main app Gemfile
gem 'cargo_shipping', path: 'engines/presentation_layer/cargo_shipping'
gem 'customers', path: 'engines/domain_layer/customers'
Generating models in
Customers Engine
engines/domain_layer/customers/app/models/customers/customer_repository.rb
rails generate model CustomerRepository email name
invoke active_record
create db/migrate/20130921154844_create_customers_customer_repositories.rb
create app/models/customers/customer_repository.rb
invoke rspec
create spec/models/customers/customer_repository_spec.rb
rspec
.
Finished in 0.05205 seconds
1 example, 0 failures
Mailing list:
https://groups.google.com/forum/?hl=en#!forum/components-in-rails
References:
Wrangling Large Rails Codebases
Architecting your Rails app for success!
http://pivotallabs.com/leave-your-migrations-in-your-rails-
engines/
http://pivotallabs.com/experience-report-engine-usage-that-
didn-t-work/
THE END
Enrico Teotti / @agenteo / teotti.com

Weitere Àhnliche Inhalte

Was ist angesagt?

Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
Xiaochun Shen
 
Ruby conf 2011, Create your own rails framework
Ruby conf 2011, Create your own rails frameworkRuby conf 2011, Create your own rails framework
Ruby conf 2011, Create your own rails framework
Pankaj Bhageria
 

Was ist angesagt? (20)

Rails::Engine
Rails::EngineRails::Engine
Rails::Engine
 
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
 
SPA using Rails & Backbone
SPA using Rails & BackboneSPA using Rails & Backbone
SPA using Rails & Backbone
 
What's new in Rails 5 - API Mode & Action Cable overview
What's new in Rails 5 - API Mode & Action Cable overviewWhat's new in Rails 5 - API Mode & Action Cable overview
What's new in Rails 5 - API Mode & Action Cable overview
 
Ruby w/o Rails (ОлДĐșŃĐ°ĐœĐŽŃ€ ĐĄŃ–ĐŒĐŸĐœĐŸĐČ)
Ruby w/o Rails (ОлДĐșŃĐ°ĐœĐŽŃ€ ĐĄŃ–ĐŒĐŸĐœĐŸĐČ)Ruby w/o Rails (ОлДĐșŃĐ°ĐœĐŽŃ€ ĐĄŃ–ĐŒĐŸĐœĐŸĐČ)
Ruby w/o Rails (ОлДĐșŃĐ°ĐœĐŽŃ€ ĐĄŃ–ĐŒĐŸĐœĐŸĐČ)
 
Cocoa on-rails-3rd
Cocoa on-rails-3rdCocoa on-rails-3rd
Cocoa on-rails-3rd
 
Ruby on Rails + AngularJS + Twitter Bootstrap
Ruby on Rails + AngularJS + Twitter BootstrapRuby on Rails + AngularJS + Twitter Bootstrap
Ruby on Rails + AngularJS + Twitter Bootstrap
 
Making the Most of Modern PHP in Drupal 7
Making the Most of Modern PHP in Drupal 7Making the Most of Modern PHP in Drupal 7
Making the Most of Modern PHP in Drupal 7
 
Ruby conf 2011, Create your own rails framework
Ruby conf 2011, Create your own rails frameworkRuby conf 2011, Create your own rails framework
Ruby conf 2011, Create your own rails framework
 
RoR 101: Session 5
RoR 101: Session 5RoR 101: Session 5
RoR 101: Session 5
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Intro to Ruby on Rails
Intro to Ruby on RailsIntro to Ruby on Rails
Intro to Ruby on Rails
 
How angularjs saves rails
How angularjs saves railsHow angularjs saves rails
How angularjs saves rails
 
Don't worry be API with Slim framework and Joomla
Don't worry be API with Slim framework and JoomlaDon't worry be API with Slim framework and Joomla
Don't worry be API with Slim framework and Joomla
 
RoR 101: Session 3
RoR 101: Session 3RoR 101: Session 3
RoR 101: Session 3
 
Hello World on Slim Framework 3.x
Hello World on Slim Framework 3.xHello World on Slim Framework 3.x
Hello World on Slim Framework 3.x
 
Project Fedena and Why Ruby on Rails - ArvindArvind G S
Project Fedena and Why Ruby on Rails - ArvindArvind G SProject Fedena and Why Ruby on Rails - ArvindArvind G S
Project Fedena and Why Ruby on Rails - ArvindArvind G S
 
AngularJS meets Rails
AngularJS meets RailsAngularJS meets Rails
AngularJS meets Rails
 
PĂĄginas DinĂąmicas de Erro em Rails com Goalie
PĂĄginas DinĂąmicas de Erro em Rails com GoaliePĂĄginas DinĂąmicas de Erro em Rails com Goalie
PĂĄginas DinĂąmicas de Erro em Rails com Goalie
 
Using Angular with Rails
Using Angular with RailsUsing Angular with Rails
Using Angular with Rails
 

Ähnlich wie Rails engines in large apps

Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010
arif44
 
Rails onCpanel
Rails onCpanelRails onCpanel
Rails onCpanel
Weldys Santos
 
Build Comet applications using Scala, Lift, and &lt;b>jQuery&lt;/b>
Build Comet applications using Scala, Lift, and &lt;b>jQuery&lt;/b>Build Comet applications using Scala, Lift, and &lt;b>jQuery&lt;/b>
Build Comet applications using Scala, Lift, and &lt;b>jQuery&lt;/b>
tutorialsruby
 

Ähnlich wie Rails engines in large apps (20)

Lightening a component based Rails architecture
Lightening a component based Rails architectureLightening a component based Rails architecture
Lightening a component based Rails architecture
 
Viridians on Rails
Viridians on RailsViridians on Rails
Viridians on Rails
 
Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010Ruby On Rails Seminar Basis Softexpo Feb2010
Ruby On Rails Seminar Basis Softexpo Feb2010
 
Rails onCpanel
Rails onCpanelRails onCpanel
Rails onCpanel
 
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The Enterprise
 
Railties
RailtiesRailties
Railties
 
Rails engines
Rails enginesRails engines
Rails engines
 
TorqueBox
TorqueBoxTorqueBox
TorqueBox
 
Selenium Training in Amritsar
Selenium Training in AmritsarSelenium Training in Amritsar
Selenium Training in Amritsar
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails Introduction
 
Rails Concept
Rails ConceptRails Concept
Rails Concept
 
Selenium Training in Jalandhar
Selenium Training in JalandharSelenium Training in Jalandhar
Selenium Training in Jalandhar
 
Selenium Training in Mohali
Selenium Training in MohaliSelenium Training in Mohali
Selenium Training in Mohali
 
Selenium Training in Chandigarh
Selenium Training in ChandigarhSelenium Training in Chandigarh
Selenium Training in Chandigarh
 
Selenium Training in Phagwara
Selenium Training in PhagwaraSelenium Training in Phagwara
Selenium Training in Phagwara
 
Selenium Training in Ludhiana
Selenium Training in LudhianaSelenium Training in Ludhiana
Selenium Training in Ludhiana
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Dev streams2
Dev streams2Dev streams2
Dev streams2
 
Rails in the bowels
Rails in the bowelsRails in the bowels
Rails in the bowels
 
Build Comet applications using Scala, Lift, and &lt;b>jQuery&lt;/b>
Build Comet applications using Scala, Lift, and &lt;b>jQuery&lt;/b>Build Comet applications using Scala, Lift, and &lt;b>jQuery&lt;/b>
Build Comet applications using Scala, Lift, and &lt;b>jQuery&lt;/b>
 

Mehr von Enrico Teotti

Mehr von Enrico Teotti (12)

Facilitating an online Agile Retrospective.pdf
Facilitating an online Agile Retrospective.pdfFacilitating an online Agile Retrospective.pdf
Facilitating an online Agile Retrospective.pdf
 
Build and maintain large Ruby applications 2023
Build and maintain large Ruby applications 2023Build and maintain large Ruby applications 2023
Build and maintain large Ruby applications 2023
 
Facilitating online agile retrospectives
Facilitating online agile retrospectivesFacilitating online agile retrospectives
Facilitating online agile retrospectives
 
Measure success in agile retrospectives
Measure success in agile retrospectivesMeasure success in agile retrospectives
Measure success in agile retrospectives
 
Structured retros
Structured retrosStructured retros
Structured retros
 
Build and maintain large ruby applications - LA Ruby Oct meetup
Build and maintain large ruby applications - LA Ruby Oct meetupBuild and maintain large ruby applications - LA Ruby Oct meetup
Build and maintain large ruby applications - LA Ruby Oct meetup
 
3 things about public speaking
3 things about public speaking3 things about public speaking
3 things about public speaking
 
Build and maintain large ruby applications Ruby Conf Australia 2016
Build and maintain large ruby applications Ruby Conf Australia 2016Build and maintain large ruby applications Ruby Conf Australia 2016
Build and maintain large ruby applications Ruby Conf Australia 2016
 
Build and maintain large Ruby apps 0.0.1
Build and maintain large Ruby apps 0.0.1Build and maintain large Ruby apps 0.0.1
Build and maintain large Ruby apps 0.0.1
 
Mindset
MindsetMindset
Mindset
 
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
 
Feature flagging with rails engines
Feature flagging with rails enginesFeature flagging with rails engines
Feature flagging with rails engines
 

KĂŒrzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

KĂŒrzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

Rails engines in large apps