SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Problem
 Difficulty reusing functionality cutting across:
   Models
   Views
   Controllers
   Assets (JS, CSS, Images)
 Duplication across all web application layers.
Courtesy of © 2002-2011 National Collegiate Scouting Association - All Rights Reserved
Solution
 Break common behavior into Rails Engines

 Customize models/controllers/helpers in each
 project where needed by reopening classes

 Customize Rails views in each project as needed
 by overriding templates

 Link to Rails Engines in Gemfile via Git repo
Example
                     Common
                     Domain
                       Rails Engine



          Search Map
             Rails Engine


  High School         Public           Athlete
   Recruiting        Profiles         Recruiting
      Rails App         Rails App        Rails App
Courtesy of © 2002-2011 National Collegiate Scouting Association - All Rights Reserved
Courtesy of © 2002-2011 National Collegiate Scouting Association - All Rights Reserved
Overview
 Engines let applications reuse:
   Models / Views / Controllers / Helpers
   Assets (JS, CSS, Images)
   Routes
   Rake tasks
   Initializers
   RSpec / Cucumber
   More (migrations, seeds, libraries)
Engine Definition
 An engine structure is similar to a Rails app
 having app, config, lib, spec, features, etc…
 lib/engine_name.rb (read online instructions)
 lib/engine_name/engine.rb (read online
 instructions)
 To reuse engine, use “jeweler” gem to generate
 gemspec (read online instructions)
lib/engine_name.rb
lib/engine_name/engine.rb
Engine Consumption
    Reference engine via Gemfile as a Ruby gem or
    Git repo hosted gemified project:




Courtesy of © 2002-2011 National Collegiate Scouting Association - All Rights Reserved
Load Order
 Typically Rails app files load first before Engine
 files.

 Strongly recommended to reverse (by patching
 “active_support/dependencies.rb”) so that
 engine’s Ruby code is overrideable in app (see
 next slide)

 ERB files can be overridden in Rails app
Ruby Code Customization
 Model/Helper/Controller behavior can be
 customized be redefining .rb files in Rails app:
   Add new methods/behavior
   Replace existing methods
   Extend existing methods via alias_method_chain
View Customization
 View files (erb, haml, etc…) and Asset files (js,
 css, and images) can be redefined in Rails app to
 override completely for customization purposes
Code Examples
Typical Development
Process
1. Make changes in engine, rake, and commit
   obtaining a new git ref
2. Update Gemfile in app with new git ref, run
   “bundle install” (getting ride of symlink)
3. Rake and commit changes in app.
4. If more changes in engine are needed go back
   to step 1
Improved Productivity via
Symlinking
 Multiple engine dependencies can hamper
 productivity when frequently going back and
 forth between engines and app
 Engines gems installed via bundler can be
 symlinked to allow continuous development until
 done with both app and
 engine:http://andymaleh.blogspot.com/2011/09/
 more-productive-rails-engine.html
Improved Development
Process
1. Open Rails app and symlink all engines “rake
   engine:symlink[engine_name]”
2. Work in app and engine until done WITHOUT running
   “bundle install”
3. Rake and commit changes in engine obtaining a new git
   ref
4. Update Gemfile in app with git ref, run “bundle install”
   (getting ride of symlink)
5. Rake and commit changes in app
Engines Reuse Engines
 Rails engines can reuse other Rails engines

 When multiple levels of depth are involved (e.g.
 App => Engine 1 => Engine 2), commit repos and
 update Gemfile from the bottom up (e.g. Engine
 2 => Engine 1 => App)
Engine Configuration
 Engines can be configured to customize rack
 middleware, load paths, generators, and Rails
 component paths. More details at:
 http://edgeapi.rubyonrails.org/classes/Rails/Engi
 ne.html
Isolated Engines
 To avoid Ruby namespace clash with
 Models/Helpers/Controllers, you can define an
 isolated namespaced engine:
Rails Engine Patterns
 Goals:
  Keep engine code agnostic of app customizations
  Prevent bi-directional coupling to simplify
  reasoning about code
  Avoid app dependent conditionals to improve code
  maintainability
Pattern - Common Domain
 Problem: Multiple applications need to share a
 basic domain model but want to customize
 behavior without mixing concerns across apps
 Solution:
  In engine, please basic domain model definitions
  and common associations only
  In each app, define specialized behavior and extra
  associations for domain models
Pattern - Expose Helper
 Problem: need to customize presentation logic
 for a view in one app only, but keep the same
 logic in others
 Solution:
  In engine, extract helper logic that needs
  customization into its own helper.
  In app, redefine that new helper with
  customizations.
Pattern - Expose Partial
 Problem: need to customize a part of the view in
 one app only, but keep it the same in others

 Solution:
  In engine, extract view part that needs
  customization as a partial.
  In app, redefine that partial with customizations.
Pattern - Extension Partial
 Problem: One app needs to add content to a
 view that is not needed in other apps
 Solution:
  In engine, introduce a new partial with empty
  contents in area that needs extension for the one
  app.
  In app, define that partial with the required
  content.
Pattern - Extension Partial



                                                             SIDEBAR
                                                             Extension
                                                              Partial



     Courtesy of © 2011 Groupon, Inc. All Rights Reserved.
Pattern - Extension Point
 Problem: different apps need to contribute data to a
 view in different places (e.g. contribute
 columns/rows in different spots)
 Solution:
   In engine, add logic that looks up partials in a specific
   ext directory, and based on file name (e.g.
   row_7.html.erb), determine index on where to insert it in
   the view.
   In app, define these partials with the right file names
   and locations.
Pattern - Extension Point

1

2

3


4

5
Pattern - Configurable
Features
 Problem: different apps need different features from
 an engine in different combinations
 Solution:
   In engine, add logic that looks up configuration options
   from a constant hash (e.g. ENGINE_XYZ_CONFIG =
   {:header => “visible”, :footer => “visible”, …}).
   In app, configure engine by overriding configuration
   options (e.g. ENGINE_XYZ_CONFIG[:header] =
   “hidden”)
Rails Engine Benefits
 Code reuse across all application layers

 Better maintainability due to:
   Independent project codebases
   Cleanly defined boundaries between projects and
   reusable components (engines)
 Project tests get smaller and run faster
Rails Engine Costs
 Overhead in establishing a new Rails Engine
 gem project

 Continuous switching between projects and
 engines to get work done

 Upgrade of ref numbers in Gemfile on every
 commit (minimized with symlinking)
More Info
 http://edgeapi.rubyonrails.org/classes/Rails/Engi
 ne.html

 http://andymaleh.blogspot.com/2011/09/more-
 productive-rails-engine.html

 http://stackoverflow.com/questions/2964050/rail
 s-engines-extending-
 functionality/2990539#2990539
Contact
 Andy Maleh
  Code Painter Blog http://andymaleh.blogspot.com
  Twitter: @AndyMaleh

Weitere ähnliche Inhalte

Was ist angesagt?

Hyperion planning what you should know
Hyperion planning what you should knowHyperion planning what you should know
Hyperion planning what you should know
Amit Sharma
 

Was ist angesagt? (20)

Eclipse - Single Source;Three Runtimes
Eclipse - Single Source;Three RuntimesEclipse - Single Source;Three Runtimes
Eclipse - Single Source;Three Runtimes
 
Getting Started with SharePoint 2013 Apps
Getting Started with SharePoint 2013 AppsGetting Started with SharePoint 2013 Apps
Getting Started with SharePoint 2013 Apps
 
1 z0 543-q&a-demo-examarea
1 z0 543-q&a-demo-examarea1 z0 543-q&a-demo-examarea
1 z0 543-q&a-demo-examarea
 
RAML - The architecture
RAML  - The architectureRAML  - The architecture
RAML - The architecture
 
Hyperion planning what you should know
Hyperion planning what you should knowHyperion planning what you should know
Hyperion planning what you should know
 
Angular Routing Tutorial | AngularJS vs Angular Router | Angular Training | E...
Angular Routing Tutorial | AngularJS vs Angular Router | Angular Training | E...Angular Routing Tutorial | AngularJS vs Angular Router | Angular Training | E...
Angular Routing Tutorial | AngularJS vs Angular Router | Angular Training | E...
 
The API Facade Pattern: Common Patterns - Episode 2
The API Facade Pattern: Common Patterns - Episode 2The API Facade Pattern: Common Patterns - Episode 2
The API Facade Pattern: Common Patterns - Episode 2
 
Anypoint access management - Roles
Anypoint access management - RolesAnypoint access management - Roles
Anypoint access management - Roles
 
Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)Plugin architecture (Extensible Application Architecture)
Plugin architecture (Extensible Application Architecture)
 
RAML
RAMLRAML
RAML
 
Meet AWS SAM
Meet AWS SAMMeet AWS SAM
Meet AWS SAM
 
Design API using RAML - basics
Design API using RAML - basicsDesign API using RAML - basics
Design API using RAML - basics
 
Adobe Ask the AEM Community Expert Session Oct 2016
Adobe Ask the AEM Community Expert Session Oct 2016Adobe Ask the AEM Community Expert Session Oct 2016
Adobe Ask the AEM Community Expert Session Oct 2016
 
Anypoint access management
Anypoint access management Anypoint access management
Anypoint access management
 
Rest With Raml
Rest With RamlRest With Raml
Rest With Raml
 
eRCP Overview and Update '06
eRCP Overview  and Update '06eRCP Overview  and Update '06
eRCP Overview and Update '06
 
Continuous Integration and Continuous Delivery for your serverless apps - Seb...
Continuous Integration and Continuous Delivery for your serverless apps - Seb...Continuous Integration and Continuous Delivery for your serverless apps - Seb...
Continuous Integration and Continuous Delivery for your serverless apps - Seb...
 
Create Location Sharing apps using the Ionic framework
Create Location Sharing apps using the Ionic framework					Create Location Sharing apps using the Ionic framework
Create Location Sharing apps using the Ionic framework
 
REST API Doc Best Practices
REST API Doc Best PracticesREST API Doc Best Practices
REST API Doc Best Practices
 
Quickly Build a Native Mobile App for Your Community Using Salesforce Mobile SDK
Quickly Build a Native Mobile App for Your Community Using Salesforce Mobile SDKQuickly Build a Native Mobile App for Your Community Using Salesforce Mobile SDK
Quickly Build a Native Mobile App for Your Community Using Salesforce Mobile SDK
 

Ähnlich wie The Rails Engine That Could - In Motion

Pluggable patterns
Pluggable patternsPluggable patterns
Pluggable patterns
Corey Oordt
 
Reactjs notes.pptx for web development- tutorial and theory
Reactjs  notes.pptx for web development- tutorial and theoryReactjs  notes.pptx for web development- tutorial and theory
Reactjs notes.pptx for web development- tutorial and theory
jobinThomas54
 

Ähnlich wie The Rails Engine That Could - In Motion (20)

The Rails Engine That Could
The Rails Engine That CouldThe Rails Engine That Could
The Rails Engine That Could
 
Rails review
Rails reviewRails review
Rails review
 
Rails engines
Rails enginesRails engines
Rails engines
 
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
 
Angular2 and You
Angular2 and YouAngular2 and You
Angular2 and You
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
 
Pluggable patterns
Pluggable patternsPluggable patterns
Pluggable patterns
 
Rails engine
Rails engineRails engine
Rails engine
 
Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012
 
SPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager SitesSPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager Sites
 
Oleg Bogut - Decoupled Drupal: how to build stable solution with JSON:API, Re...
Oleg Bogut - Decoupled Drupal: how to build stable solution with JSON:API, Re...Oleg Bogut - Decoupled Drupal: how to build stable solution with JSON:API, Re...
Oleg Bogut - Decoupled Drupal: how to build stable solution with JSON:API, Re...
 
Introduction To Angular.js - SpringPeople
Introduction To Angular.js - SpringPeopleIntroduction To Angular.js - SpringPeople
Introduction To Angular.js - SpringPeople
 
Vue3: nuove funzionalità, differenze e come migrare
Vue3: nuove funzionalità, differenze e come migrareVue3: nuove funzionalità, differenze e come migrare
Vue3: nuove funzionalità, differenze e come migrare
 
Adobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsAdobe Experience Manager Core Components
Adobe Experience Manager Core Components
 
Reactjs notes.pptx for web development- tutorial and theory
Reactjs  notes.pptx for web development- tutorial and theoryReactjs  notes.pptx for web development- tutorial and theory
Reactjs notes.pptx for web development- tutorial and theory
 
Fame
FameFame
Fame
 
App Engine On Air: Munich
App Engine On Air: MunichApp Engine On Air: Munich
App Engine On Air: Munich
 
Top 10 Javascript Frameworks For Easy Web Development
Top 10 Javascript Frameworks For Easy Web DevelopmentTop 10 Javascript Frameworks For Easy Web Development
Top 10 Javascript Frameworks For Easy Web Development
 
Metamorphosis from Forms to Java: A technical lead's perspective, part II
Metamorphosis from Forms to Java:  A technical lead's perspective, part IIMetamorphosis from Forms to Java:  A technical lead's perspective, part II
Metamorphosis from Forms to Java: A technical lead's perspective, part II
 
Angularjs2 presentation
Angularjs2 presentationAngularjs2 presentation
Angularjs2 presentation
 

Mehr von Andy Maleh

Software Craftsmanship VS Software Engineering
Software Craftsmanship VS Software EngineeringSoftware Craftsmanship VS Software Engineering
Software Craftsmanship VS Software Engineering
Andy Maleh
 
Software Design Trilogy Part I - Responsibility Driven Design for Rubyists
Software Design Trilogy Part I - Responsibility Driven Design for RubyistsSoftware Design Trilogy Part I - Responsibility Driven Design for Rubyists
Software Design Trilogy Part I - Responsibility Driven Design for Rubyists
Andy Maleh
 

Mehr von Andy Maleh (14)

Fukuoka Ruby Award 2023 - Opal
Fukuoka Ruby Award 2023 - OpalFukuoka Ruby Award 2023 - Opal
Fukuoka Ruby Award 2023 - Opal
 
Becoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby Meetup
Becoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby MeetupBecoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby Meetup
Becoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby Meetup
 
Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ...
 Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ... Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ...
Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ...
 
How I Built My Code Editor in Ruby
How I Built My Code Editor in RubyHow I Built My Code Editor in Ruby
How I Built My Code Editor in Ruby
 
Ultra Light and Maintainable Rails Wizards at RailsConf 2014
Ultra Light and Maintainable Rails Wizards at RailsConf 2014Ultra Light and Maintainable Rails Wizards at RailsConf 2014
Ultra Light and Maintainable Rails Wizards at RailsConf 2014
 
RailsConf 2014 Recap at Montreal.rb by Andy Maleh
RailsConf 2014 Recap at Montreal.rb by Andy MalehRailsConf 2014 Recap at Montreal.rb by Andy Maleh
RailsConf 2014 Recap at Montreal.rb by Andy Maleh
 
Ultra Light and Maintainable Wizards in Rails at Montreal.rb
Ultra Light and Maintainable Wizards in Rails at Montreal.rbUltra Light and Maintainable Wizards in Rails at Montreal.rb
Ultra Light and Maintainable Wizards in Rails at Montreal.rb
 
Software Craftsmanship VS Software Engineering
Software Craftsmanship VS Software EngineeringSoftware Craftsmanship VS Software Engineering
Software Craftsmanship VS Software Engineering
 
Software Craftsmanship vs Software Engineering (Lightning Talk)
Software Craftsmanship vs Software Engineering (Lightning Talk)Software Craftsmanship vs Software Engineering (Lightning Talk)
Software Craftsmanship vs Software Engineering (Lightning Talk)
 
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
 
Software Design Trilogy Part II - Design Patterns for Rubyists
Software Design Trilogy Part II - Design Patterns for RubyistsSoftware Design Trilogy Part II - Design Patterns for Rubyists
Software Design Trilogy Part II - Design Patterns for Rubyists
 
Software Design Trilogy Part I - Responsibility Driven Design for Rubyists
Software Design Trilogy Part I - Responsibility Driven Design for RubyistsSoftware Design Trilogy Part I - Responsibility Driven Design for Rubyists
Software Design Trilogy Part I - Responsibility Driven Design for Rubyists
 
How I Learned To Apply Design Patterns
How I Learned To Apply Design PatternsHow I Learned To Apply Design Patterns
How I Learned To Apply Design Patterns
 
Simplifying Desktop Development With Glimmer
Simplifying Desktop Development With GlimmerSimplifying Desktop Development With Glimmer
Simplifying Desktop Development With Glimmer
 

Kürzlich hochgeladen

+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@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
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
 

Kürzlich hochgeladen (20)

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...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
+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...
 
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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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...
 
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
 
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
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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, ...
 
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
 

The Rails Engine That Could - In Motion

  • 1.
  • 2. Problem Difficulty reusing functionality cutting across: Models Views Controllers Assets (JS, CSS, Images) Duplication across all web application layers.
  • 3. Courtesy of © 2002-2011 National Collegiate Scouting Association - All Rights Reserved
  • 4. Solution Break common behavior into Rails Engines Customize models/controllers/helpers in each project where needed by reopening classes Customize Rails views in each project as needed by overriding templates Link to Rails Engines in Gemfile via Git repo
  • 5. Example Common Domain Rails Engine Search Map Rails Engine High School Public Athlete Recruiting Profiles Recruiting Rails App Rails App Rails App
  • 6. Courtesy of © 2002-2011 National Collegiate Scouting Association - All Rights Reserved
  • 7. Courtesy of © 2002-2011 National Collegiate Scouting Association - All Rights Reserved
  • 8. Overview Engines let applications reuse: Models / Views / Controllers / Helpers Assets (JS, CSS, Images) Routes Rake tasks Initializers RSpec / Cucumber More (migrations, seeds, libraries)
  • 9. Engine Definition An engine structure is similar to a Rails app having app, config, lib, spec, features, etc… lib/engine_name.rb (read online instructions) lib/engine_name/engine.rb (read online instructions) To reuse engine, use “jeweler” gem to generate gemspec (read online instructions)
  • 12. Engine Consumption Reference engine via Gemfile as a Ruby gem or Git repo hosted gemified project: Courtesy of © 2002-2011 National Collegiate Scouting Association - All Rights Reserved
  • 13. Load Order Typically Rails app files load first before Engine files. Strongly recommended to reverse (by patching “active_support/dependencies.rb”) so that engine’s Ruby code is overrideable in app (see next slide) ERB files can be overridden in Rails app
  • 14.
  • 15. Ruby Code Customization Model/Helper/Controller behavior can be customized be redefining .rb files in Rails app: Add new methods/behavior Replace existing methods Extend existing methods via alias_method_chain
  • 16. View Customization View files (erb, haml, etc…) and Asset files (js, css, and images) can be redefined in Rails app to override completely for customization purposes
  • 18. Typical Development Process 1. Make changes in engine, rake, and commit obtaining a new git ref 2. Update Gemfile in app with new git ref, run “bundle install” (getting ride of symlink) 3. Rake and commit changes in app. 4. If more changes in engine are needed go back to step 1
  • 19. Improved Productivity via Symlinking Multiple engine dependencies can hamper productivity when frequently going back and forth between engines and app Engines gems installed via bundler can be symlinked to allow continuous development until done with both app and engine:http://andymaleh.blogspot.com/2011/09/ more-productive-rails-engine.html
  • 20. Improved Development Process 1. Open Rails app and symlink all engines “rake engine:symlink[engine_name]” 2. Work in app and engine until done WITHOUT running “bundle install” 3. Rake and commit changes in engine obtaining a new git ref 4. Update Gemfile in app with git ref, run “bundle install” (getting ride of symlink) 5. Rake and commit changes in app
  • 21. Engines Reuse Engines Rails engines can reuse other Rails engines When multiple levels of depth are involved (e.g. App => Engine 1 => Engine 2), commit repos and update Gemfile from the bottom up (e.g. Engine 2 => Engine 1 => App)
  • 22. Engine Configuration Engines can be configured to customize rack middleware, load paths, generators, and Rails component paths. More details at: http://edgeapi.rubyonrails.org/classes/Rails/Engi ne.html
  • 23. Isolated Engines To avoid Ruby namespace clash with Models/Helpers/Controllers, you can define an isolated namespaced engine:
  • 24. Rails Engine Patterns Goals: Keep engine code agnostic of app customizations Prevent bi-directional coupling to simplify reasoning about code Avoid app dependent conditionals to improve code maintainability
  • 25. Pattern - Common Domain Problem: Multiple applications need to share a basic domain model but want to customize behavior without mixing concerns across apps Solution: In engine, please basic domain model definitions and common associations only In each app, define specialized behavior and extra associations for domain models
  • 26. Pattern - Expose Helper Problem: need to customize presentation logic for a view in one app only, but keep the same logic in others Solution: In engine, extract helper logic that needs customization into its own helper. In app, redefine that new helper with customizations.
  • 27. Pattern - Expose Partial Problem: need to customize a part of the view in one app only, but keep it the same in others Solution: In engine, extract view part that needs customization as a partial. In app, redefine that partial with customizations.
  • 28. Pattern - Extension Partial Problem: One app needs to add content to a view that is not needed in other apps Solution: In engine, introduce a new partial with empty contents in area that needs extension for the one app. In app, define that partial with the required content.
  • 29. Pattern - Extension Partial SIDEBAR Extension Partial Courtesy of © 2011 Groupon, Inc. All Rights Reserved.
  • 30. Pattern - Extension Point Problem: different apps need to contribute data to a view in different places (e.g. contribute columns/rows in different spots) Solution: In engine, add logic that looks up partials in a specific ext directory, and based on file name (e.g. row_7.html.erb), determine index on where to insert it in the view. In app, define these partials with the right file names and locations.
  • 31. Pattern - Extension Point 1 2 3 4 5
  • 32. Pattern - Configurable Features Problem: different apps need different features from an engine in different combinations Solution: In engine, add logic that looks up configuration options from a constant hash (e.g. ENGINE_XYZ_CONFIG = {:header => “visible”, :footer => “visible”, …}). In app, configure engine by overriding configuration options (e.g. ENGINE_XYZ_CONFIG[:header] = “hidden”)
  • 33. Rails Engine Benefits Code reuse across all application layers Better maintainability due to: Independent project codebases Cleanly defined boundaries between projects and reusable components (engines) Project tests get smaller and run faster
  • 34. Rails Engine Costs Overhead in establishing a new Rails Engine gem project Continuous switching between projects and engines to get work done Upgrade of ref numbers in Gemfile on every commit (minimized with symlinking)
  • 35. More Info http://edgeapi.rubyonrails.org/classes/Rails/Engi ne.html http://andymaleh.blogspot.com/2011/09/more- productive-rails-engine.html http://stackoverflow.com/questions/2964050/rail s-engines-extending- functionality/2990539#2990539
  • 36. Contact Andy Maleh Code Painter Blog http://andymaleh.blogspot.com Twitter: @AndyMaleh