SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Ultra Light &
Maintainable
Wizards in Rails
Andy Maleh
VP of Engineering
BIG ASTRONAUT
Overview
• Why Use A Wizard?
• Wizard Example
• Wizard Implementation Goals
• 1001 Wizard Implementations
• Ultra Light & Maintainable Wizard
Why Use A Wizard?
Avoid overwhelming user with a huge form
Why Use A Wizard?
Simplify a workflow into multiple steps
Why Use A Wizard?
Help the user by providing guidance
Wizard Example
EarlyShares Project Idea Submission
Wizard Example
Step 1 – Basic Info
Wizard Example
Step 2 – Details
Wizard Example
Step 3 – Document Content
Wizard Example
Step 4 – Preview
Wizard Example
Steps Done – Project Landing Page
Wizard Implementation
Goals
• Rails Server must persist progress on every step
o JS Client-Side Persistence is Out Of Scope
• REST
• MVC
• OO
• Non-Functional Requirements:
o Productivity
o Maintainability
o Performance
o Security
1001 Wizard
Implementations
1. One Controller Per Wizard Step
• Create a REST resource per wizard step
• One ActiveRecord with conditional validations
• Multiple Controllers
• Multiple sets of Views and Helpers
• Have each wizard step controller redirect to the
next one on create or update
1001 Wizard
Implementations
1. One Controller Per Wizard Step
Step 1
Controller
Step 2
Controller
Step 3
Controller
Step 4
Controller
Create &
Redirect to New
Create &
Redirect to New
Create &
Redirect to New
Step 1
ActiveRecord
1001 Wizard
Implementations
1. One Controller Per Wizard Step
o Critique
• Redundant code across controllers
o Repetitive redirect logic
o Redundant authentication logic
o Similar model loading logic
o Similar REST actions
• Tying database tables to presentation details
1001 Wizard
Implementations
2. One Action/Presenter Per Wizard Step
• Create one ActiveRecord
• Create one controller with a different new and create
action variation per wizard step
• e.g. new_step1, create_step1, new_step2, create_step2, etc…
• Create a different ActiveModel presenter per wizard step
• Have each ActiveModel presenter manage its own step
validation logic
• Bind every wizard step form to corresponding
ActiveModel presenter
• Have each wizard step action redirect to the next one
on create
1001 Wizard
Implementations
2. One Action Per Wizard Step
Controller
Step 1 New
Step 1 Create
Step 2 New
Step 2 Create
Step 3 New
Step 3 Create
Step 4 New
Step 4 Create
Step 1
Presenter
Validation/Persistance
Adapter
Step 2
Presenter
Validation/Persistance
Adapter
Step 3
Presenter
Validation/Persistance
Adapter
Step 4
Presenter
Validation/Persistance Adapter
ActiveRecord
Create Step &
Redirect to New Ste
1001 Wizard
Implementations
2. One Action Per Wizard Step
o Critique
• Not RESTful
• Redundant code across actions
o Repetitive redirect logic
o Repetitive update logic
• Too much presenter management code
1001 Wizard
Implementations
3. Session Accumulation
o Create one ActiveRecord
o Have multiple Controllers or Actions accumulate wizard step data in the
session
o Have ActiveRecord in-memory conditional validations run on every step
o On the final step, create ActiveRecord running all validations
Step 1
Step 2
Step 3
Step 4
Accumulate
in Session
Accumulate
in Session
Accumulate
in Session
Create
ActiveRecord
1001 Wizard
Implementations
3. Session Accumulation
o Critique
• Reliance on session has implications on scalability
• Controller code more complex due to managing session data
storage
• Validations defined twice, once per ActiveModel presenters used for
form validation and once in the actual ActiveRecord
1001 Wizard
Implementations
4. Hidden Value Accumulation
o Same as session value accumulation except the controller manages data
coming from a request parameter
o Same pros and cons as Session Value Accumulation except that it has no
scalability implications
o NOTE: hidden value must be encrypted for security
1001 Wizard
Implementations
5. State Machine
o Create one ActiveRecord
o Make ActiveRecord a state machine managing steps:
• adding a step column
• add current_step, next_step, and prev_step methods
o Different view per step
o Have single ActiveRecord manage each step validations by relying on
conditional validations. For example:
validate :phone,
presence: true,
if: lambda {current_step==“shipping”}
1001 Wizard
Implementations
5. State Machine
o Critique
• Puts presentation concerns in Model (breaks MVC)
o The state machine wizard must be a layer on top of the Model. It
has nothing to do with the business model.
• Stores an extra column in the database for purely presentation-
related reasons
o Can be avoided with session storage of state, opening a different
can of worms (controller complexity)
• More complexity in declaring validations due to conditions and
potentially overloading the Model
1001 Wizard
Implementations
1001. Gems
• Wizardry: state machine in model
• Wicked: clean state machine in controller (better)
but no validation support beyond conditional
validation
• Rails-Wizard-Generator: XML XML XML
• Stepper: Nice support for steps in model and
controller
Wizard Implementation
Goals Review
• Rails Server must persist progress on every step
o JS Client-Side Persistence is Out Of Scope
• REST
• MVC
• OO
• Non-Functional Requirements:
o Productivity
o Maintainability
o Performance
o Security
Ultra Light &
Maintainable Wizard
• Philosophy:
o A wizard is simply a builder of a model
Ultra Light &
Maintainable Wizard
• Philosophy:
o Every step is simply a partial data-view of the model
Ultra Light &
Maintainable Wizard
• Philosophy:
o REST resources are the model and model parts edited during a step.
Ultra Light &
Maintainable Wizard
• Philosophy:
o Models must manage validations without conditions by relying on step-
polymorphism
Ultra Light &
Maintainable Wizard
• Philosophy:
o Step view forms are maintained independently with no conditionals as
well
step1.html.erb
step2.html.erb
step3.html.erb
step4.html.erb
Ultra Light &
Maintainable Wizard
Model
Controller
Create
Show
Step 1 Presenter
Validations/Step Logic
Step 2 Presenter
Validations/Step Logic
Step 3 Presenter
Validations/Step Logic
Model
ActiveRecord
Core Validations
& Shared Logic
Step 4 Presenter
Validations/Step Logic
Update &
Redirect to Edit
Model Part
Controller
Edit
Update
Ultra Light &
Maintainable Wizard
• In a Nutshell:
o Model resource
o Nested model part resource
(e.g. /projects/project1/project_parts/basic_info)
• Step name serves as ID
• Contains validations and before/after hooks for stepping
o Controller for the model resource:
• Begins wizard by creating model ActiveRecord
• Shows produced model at the end of the wizard
o Controller for the model part resource:
• Every step represents an Edit action of a model part
• Every step submission represents an Update action of a model part
o View for model resource show page
o View for every model part presenter edit page
Ultra Light &
Maintainable Wizard
Routes
Ultra Light &
Maintainable Wizard
Model (main REST resource)
Ultra Light &
Maintainable Wizard
Step Sub-Models aka Model Parts
Ultra Light &
Maintainable Wizard
Project::BasicInfo Step Sub-Model
Ultra Light &
Maintainable Wizard
Project::BasicInfo Step Sub-Model (cont’d)
Ultra Light &
Maintainable Wizard
Project::Detail Step Sub-Model
Ultra Light &
Maintainable Wizard
ProjectsController
ProjectPartsController
Ultra Light &
Maintainable Wizard
ProjectPartsController (cont’d)
Ultra Light &
Maintainable Wizard
Views
Ultra Light &
Maintainable Wizard
View form template
Ultra Light &
Maintainable Wizard
Step View Template Example
Review
• Why Use A Wizard?
• Wizard Example
• Wizard Implementation Goals
• 1001 Wizard Implementations
• Ultra Light & Maintainable Wizard
github.com/AndyMaleh/u
ltra_light_wizard
Andy Maleh – VP of Engineering – Big Astronaut
• WEBSITE: http://www.bigastronaut.com
• BLOG: http://andymaleh.blogspot.com
• TWITTER: @AndyMaleh

Weitere ähnliche Inhalte

Was ist angesagt?

Using microsoft application insights to implement a build, measure, learn loop
Using microsoft application insights to implement a build, measure, learn loopUsing microsoft application insights to implement a build, measure, learn loop
Using microsoft application insights to implement a build, measure, learn loop
Marcel de Vries
 

Was ist angesagt? (20)

Splunk conf2014 - Using Selenium and Splunk for Transaction Monitoring Insight
Splunk conf2014 - Using Selenium and Splunk for Transaction Monitoring InsightSplunk conf2014 - Using Selenium and Splunk for Transaction Monitoring Insight
Splunk conf2014 - Using Selenium and Splunk for Transaction Monitoring Insight
 
Supervise your Akka actors
Supervise your Akka actorsSupervise your Akka actors
Supervise your Akka actors
 
Continuous Delivery - Voxxed Days Thessaloniki 21.10.2016
Continuous Delivery - Voxxed Days Thessaloniki 21.10.2016Continuous Delivery - Voxxed Days Thessaloniki 21.10.2016
Continuous Delivery - Voxxed Days Thessaloniki 21.10.2016
 
Continuous Delivery - Devoxx Morocco 2016
Continuous Delivery - Devoxx Morocco 2016Continuous Delivery - Devoxx Morocco 2016
Continuous Delivery - Devoxx Morocco 2016
 
Azure Functions in Action #CodePaLOUsa
Azure Functions in Action #CodePaLOUsaAzure Functions in Action #CodePaLOUsa
Azure Functions in Action #CodePaLOUsa
 
[UC4] Version and Automate Everything
[UC4] Version and Automate Everything[UC4] Version and Automate Everything
[UC4] Version and Automate Everything
 
How Gear4Music Went from 0-1000+ API Tests
How Gear4Music Went from 0-1000+ API TestsHow Gear4Music Went from 0-1000+ API Tests
How Gear4Music Went from 0-1000+ API Tests
 
Introduction to GOCD - Amulya Sharma
Introduction to GOCD - Amulya SharmaIntroduction to GOCD - Amulya Sharma
Introduction to GOCD - Amulya Sharma
 
C# Async/Await Explained
C# Async/Await ExplainedC# Async/Await Explained
C# Async/Await Explained
 
Asp.net event handler
Asp.net event handlerAsp.net event handler
Asp.net event handler
 
Using microsoft application insights to implement a build, measure, learn loop
Using microsoft application insights to implement a build, measure, learn loopUsing microsoft application insights to implement a build, measure, learn loop
Using microsoft application insights to implement a build, measure, learn loop
 
Mock Servers - Fake All the Things!
Mock Servers - Fake All the Things!Mock Servers - Fake All the Things!
Mock Servers - Fake All the Things!
 
Salesforce Process builder Vs Workflows
Salesforce Process builder Vs WorkflowsSalesforce Process builder Vs Workflows
Salesforce Process builder Vs Workflows
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot apps
 
DRAKON Visual Language: Tutorial. Part 2
DRAKON Visual Language: Tutorial. Part 2DRAKON Visual Language: Tutorial. Part 2
DRAKON Visual Language: Tutorial. Part 2
 
Introduce flux & react in practice
Introduce flux & react in practiceIntroduce flux & react in practice
Introduce flux & react in practice
 
Si fa presto a dire serverless
Si fa presto a dire serverlessSi fa presto a dire serverless
Si fa presto a dire serverless
 
Durable functions
Durable functionsDurable functions
Durable functions
 
Monitoring your API
Monitoring your APIMonitoring your API
Monitoring your API
 
Optimize your delivery and quality with the right release methodology and too...
Optimize your delivery and quality with the right release methodology and too...Optimize your delivery and quality with the right release methodology and too...
Optimize your delivery and quality with the right release methodology and too...
 

Ähnlich wie Ultra Light and Maintainable Rails Wizards at RailsConf 2014

Cerberus_Presentation1
Cerberus_Presentation1Cerberus_Presentation1
Cerberus_Presentation1
CIVEL Benoit
 
Hanselman lipton asp_connections_ams304_mvc
Hanselman lipton asp_connections_ams304_mvcHanselman lipton asp_connections_ams304_mvc
Hanselman lipton asp_connections_ams304_mvc
denemedeniz
 
How kubernetes operators can rescue dev secops in midst of a pandemic updated
How kubernetes operators can rescue dev secops in midst of a pandemic updatedHow kubernetes operators can rescue dev secops in midst of a pandemic updated
How kubernetes operators can rescue dev secops in midst of a pandemic updated
Shikha Srivastava
 

Ähnlich wie Ultra Light and Maintainable Rails Wizards at RailsConf 2014 (20)

Refactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test AutomationRefactoring Legacy Web Forms for Test Automation
Refactoring Legacy Web Forms for Test Automation
 
PAC 2019 virtual Bruno Audoux
PAC 2019 virtual Bruno Audoux PAC 2019 virtual Bruno Audoux
PAC 2019 virtual Bruno Audoux
 
Neoload
Neoload Neoload
Neoload
 
Automated Acceptance Testing Example
Automated Acceptance Testing ExampleAutomated Acceptance Testing Example
Automated Acceptance Testing Example
 
Cerberus : Framework for Manual and Automated Testing (Web Application)
Cerberus : Framework for Manual and Automated Testing (Web Application)Cerberus : Framework for Manual and Automated Testing (Web Application)
Cerberus : Framework for Manual and Automated Testing (Web Application)
 
Cerberus_Presentation1
Cerberus_Presentation1Cerberus_Presentation1
Cerberus_Presentation1
 
vRealize Operation 7.5 What's new
vRealize Operation 7.5 What's newvRealize Operation 7.5 What's new
vRealize Operation 7.5 What's new
 
Continuous Delivery: How RightScale Releases Weekly
Continuous Delivery: How RightScale Releases WeeklyContinuous Delivery: How RightScale Releases Weekly
Continuous Delivery: How RightScale Releases Weekly
 
.NET microservices with Azure Service Fabric
.NET microservices with Azure Service Fabric.NET microservices with Azure Service Fabric
.NET microservices with Azure Service Fabric
 
Hanselman lipton asp_connections_ams304_mvc
Hanselman lipton asp_connections_ams304_mvcHanselman lipton asp_connections_ams304_mvc
Hanselman lipton asp_connections_ams304_mvc
 
Introduction to Angular JS
Introduction to Angular JSIntroduction to Angular JS
Introduction to Angular JS
 
Spring 1 day program
Spring 1 day programSpring 1 day program
Spring 1 day program
 
End to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSEnd to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJS
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuĹĄt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuĹĄtInfinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuĹĄt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuĹĄt
 
Validation for APIs in Laravel 4
Validation for APIs in Laravel 4Validation for APIs in Laravel 4
Validation for APIs in Laravel 4
 
How kubernetes operators can rescue dev secops in midst of a pandemic updated
How kubernetes operators can rescue dev secops in midst of a pandemic updatedHow kubernetes operators can rescue dev secops in midst of a pandemic updated
How kubernetes operators can rescue dev secops in midst of a pandemic updated
 
VMworld 2013: Part 2: How to Build a Self-Healing Data Center with vCenter Or...
VMworld 2013: Part 2: How to Build a Self-Healing Data Center with vCenter Or...VMworld 2013: Part 2: How to Build a Self-Healing Data Center with vCenter Or...
VMworld 2013: Part 2: How to Build a Self-Healing Data Center with vCenter Or...
 
Introduction to selenium
Introduction to seleniumIntroduction to selenium
Introduction to selenium
 
How Mature is Your Infrastructure?
How Mature is Your Infrastructure?How Mature is Your Infrastructure?
How Mature is Your Infrastructure?
 

Mehr von Andy Maleh

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
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
 
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
Andy Maleh
 

Mehr von Andy Maleh (16)

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
 
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
 
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
 
Software Craftsmanship VS Software Engineering
Software Craftsmanship VS Software EngineeringSoftware Craftsmanship VS Software Engineering
Software Craftsmanship VS Software Engineering
 
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine Patterns
 
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
 
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
 
The Rails Engine That Could
The Rails Engine That CouldThe Rails Engine That Could
The Rails Engine That Could
 
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

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

KĂźrzlich hochgeladen (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
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
 
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...
 
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...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Ultra Light and Maintainable Rails Wizards at RailsConf 2014

  • 1. Ultra Light & Maintainable Wizards in Rails Andy Maleh VP of Engineering BIG ASTRONAUT
  • 2. Overview • Why Use A Wizard? • Wizard Example • Wizard Implementation Goals • 1001 Wizard Implementations • Ultra Light & Maintainable Wizard
  • 3. Why Use A Wizard? Avoid overwhelming user with a huge form
  • 4. Why Use A Wizard? Simplify a workflow into multiple steps
  • 5. Why Use A Wizard? Help the user by providing guidance
  • 7. Wizard Example Step 1 – Basic Info
  • 8. Wizard Example Step 2 – Details
  • 9. Wizard Example Step 3 – Document Content
  • 10. Wizard Example Step 4 – Preview
  • 11. Wizard Example Steps Done – Project Landing Page
  • 12. Wizard Implementation Goals • Rails Server must persist progress on every step o JS Client-Side Persistence is Out Of Scope • REST • MVC • OO • Non-Functional Requirements: o Productivity o Maintainability o Performance o Security
  • 13. 1001 Wizard Implementations 1. One Controller Per Wizard Step • Create a REST resource per wizard step • One ActiveRecord with conditional validations • Multiple Controllers • Multiple sets of Views and Helpers • Have each wizard step controller redirect to the next one on create or update
  • 14. 1001 Wizard Implementations 1. One Controller Per Wizard Step Step 1 Controller Step 2 Controller Step 3 Controller Step 4 Controller Create & Redirect to New Create & Redirect to New Create & Redirect to New Step 1 ActiveRecord
  • 15. 1001 Wizard Implementations 1. One Controller Per Wizard Step o Critique • Redundant code across controllers o Repetitive redirect logic o Redundant authentication logic o Similar model loading logic o Similar REST actions • Tying database tables to presentation details
  • 16. 1001 Wizard Implementations 2. One Action/Presenter Per Wizard Step • Create one ActiveRecord • Create one controller with a different new and create action variation per wizard step • e.g. new_step1, create_step1, new_step2, create_step2, etc… • Create a different ActiveModel presenter per wizard step • Have each ActiveModel presenter manage its own step validation logic • Bind every wizard step form to corresponding ActiveModel presenter • Have each wizard step action redirect to the next one on create
  • 17. 1001 Wizard Implementations 2. One Action Per Wizard Step Controller Step 1 New Step 1 Create Step 2 New Step 2 Create Step 3 New Step 3 Create Step 4 New Step 4 Create Step 1 Presenter Validation/Persistance Adapter Step 2 Presenter Validation/Persistance Adapter Step 3 Presenter Validation/Persistance Adapter Step 4 Presenter Validation/Persistance Adapter ActiveRecord Create Step & Redirect to New Ste
  • 18. 1001 Wizard Implementations 2. One Action Per Wizard Step o Critique • Not RESTful • Redundant code across actions o Repetitive redirect logic o Repetitive update logic • Too much presenter management code
  • 19. 1001 Wizard Implementations 3. Session Accumulation o Create one ActiveRecord o Have multiple Controllers or Actions accumulate wizard step data in the session o Have ActiveRecord in-memory conditional validations run on every step o On the final step, create ActiveRecord running all validations Step 1 Step 2 Step 3 Step 4 Accumulate in Session Accumulate in Session Accumulate in Session Create ActiveRecord
  • 20. 1001 Wizard Implementations 3. Session Accumulation o Critique • Reliance on session has implications on scalability • Controller code more complex due to managing session data storage • Validations defined twice, once per ActiveModel presenters used for form validation and once in the actual ActiveRecord
  • 21. 1001 Wizard Implementations 4. Hidden Value Accumulation o Same as session value accumulation except the controller manages data coming from a request parameter o Same pros and cons as Session Value Accumulation except that it has no scalability implications o NOTE: hidden value must be encrypted for security
  • 22. 1001 Wizard Implementations 5. State Machine o Create one ActiveRecord o Make ActiveRecord a state machine managing steps: • adding a step column • add current_step, next_step, and prev_step methods o Different view per step o Have single ActiveRecord manage each step validations by relying on conditional validations. For example: validate :phone, presence: true, if: lambda {current_step==“shipping”}
  • 23. 1001 Wizard Implementations 5. State Machine o Critique • Puts presentation concerns in Model (breaks MVC) o The state machine wizard must be a layer on top of the Model. It has nothing to do with the business model. • Stores an extra column in the database for purely presentation- related reasons o Can be avoided with session storage of state, opening a different can of worms (controller complexity) • More complexity in declaring validations due to conditions and potentially overloading the Model
  • 24. 1001 Wizard Implementations 1001. Gems • Wizardry: state machine in model • Wicked: clean state machine in controller (better) but no validation support beyond conditional validation • Rails-Wizard-Generator: XML XML XML • Stepper: Nice support for steps in model and controller
  • 25. Wizard Implementation Goals Review • Rails Server must persist progress on every step o JS Client-Side Persistence is Out Of Scope • REST • MVC • OO • Non-Functional Requirements: o Productivity o Maintainability o Performance o Security
  • 26. Ultra Light & Maintainable Wizard • Philosophy: o A wizard is simply a builder of a model
  • 27. Ultra Light & Maintainable Wizard • Philosophy: o Every step is simply a partial data-view of the model
  • 28. Ultra Light & Maintainable Wizard • Philosophy: o REST resources are the model and model parts edited during a step.
  • 29. Ultra Light & Maintainable Wizard • Philosophy: o Models must manage validations without conditions by relying on step- polymorphism
  • 30. Ultra Light & Maintainable Wizard • Philosophy: o Step view forms are maintained independently with no conditionals as well step1.html.erb step2.html.erb step3.html.erb step4.html.erb
  • 31. Ultra Light & Maintainable Wizard Model Controller Create Show Step 1 Presenter Validations/Step Logic Step 2 Presenter Validations/Step Logic Step 3 Presenter Validations/Step Logic Model ActiveRecord Core Validations & Shared Logic Step 4 Presenter Validations/Step Logic Update & Redirect to Edit Model Part Controller Edit Update
  • 32. Ultra Light & Maintainable Wizard • In a Nutshell: o Model resource o Nested model part resource (e.g. /projects/project1/project_parts/basic_info) • Step name serves as ID • Contains validations and before/after hooks for stepping o Controller for the model resource: • Begins wizard by creating model ActiveRecord • Shows produced model at the end of the wizard o Controller for the model part resource: • Every step represents an Edit action of a model part • Every step submission represents an Update action of a model part o View for model resource show page o View for every model part presenter edit page
  • 33. Ultra Light & Maintainable Wizard Routes
  • 34. Ultra Light & Maintainable Wizard Model (main REST resource)
  • 35. Ultra Light & Maintainable Wizard Step Sub-Models aka Model Parts
  • 36. Ultra Light & Maintainable Wizard Project::BasicInfo Step Sub-Model
  • 37. Ultra Light & Maintainable Wizard Project::BasicInfo Step Sub-Model (cont’d)
  • 38. Ultra Light & Maintainable Wizard Project::Detail Step Sub-Model
  • 39. Ultra Light & Maintainable Wizard ProjectsController
  • 41. Ultra Light & Maintainable Wizard ProjectPartsController (cont’d)
  • 43. Ultra Light & Maintainable Wizard View form template
  • 44. Ultra Light & Maintainable Wizard Step View Template Example
  • 45. Review • Why Use A Wizard? • Wizard Example • Wizard Implementation Goals • 1001 Wizard Implementations • Ultra Light & Maintainable Wizard
  • 46. github.com/AndyMaleh/u ltra_light_wizard Andy Maleh – VP of Engineering – Big Astronaut • WEBSITE: http://www.bigastronaut.com • BLOG: http://andymaleh.blogspot.com • TWITTER: @AndyMaleh