SlideShare uma empresa Scribd logo
1 de 39
Ultra Light &
Maintainable Wizards in
Rails
Andy Maleh
President and Chief Journeyman
Soft Buzz
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
Event Registration Wizard
Wizard Example
Step 1 – Personal Info
Wizard Example
Step 2 – Background Info
Wizard Example
Step 3 – Review
Wizard Example
Step 4 – Payment
Wizard Example
Steps Done – Order Confirmation
Wizard Implementation Goals
REST
MVC
OO
Non-Functional Requirements:
Productivity
Maintainability
Performance
Security
1001 Wizard
Implementations
1. One Controller/ActiveRecord Per Wizard Step
•

Create a REST resource per wizard step
•
•
•

Multiple ActiveRecords with distinct
validations
Multiple Controllers
Multiple sets of Views and Helpers

•

Bind every wizard step form to corresponding
ActiveRecord

•

Have each wizard step controller redirect to
the next one on create or update
1001 Wizard
Implementations
1. One Controller/ActiveRecord Per Wizard Step
Step 1
ActiveRecord

Step 1
Controller

Step 2
ActiveRecord

Create &
Redirect to New
Step 2
Controller
Create &
Redirect to New

Step 3
ActiveRecord

Step 3
Controller

Step 4
ActiveRecord

Create &
Redirect to New
Step 4
Controller
1001 Wizard
Implementations
1. One Controller/ActiveRecord Per Wizard Step
Pros
Easy to understand and follow
RESTful
1001 Wizard
Implementations
1. One Controller/ActiveRecord Per Wizard Step
Cons
Redundant code across controllers
Repetitive redirect logic
Redundant authentication logic
Similar model loading logic
Similar REST actions

Tying database tables to presentation details
1001 Wizard
Implementations
1a. One Controller/Presenter Per Wizard Step
•

Create one ActiveRecord

•

Create a controller and 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 controller redirect to the
next one on create or update
1001 Wizard
Implementations
1a. One Controller/Presenter Per Wizard Step
Step 1
Controller

Step 1
Presenter

Step 2
Presenter

Create &
Redirect to New
Step 2
Controller
Create &
Redirect to New

Step 3
Presenter

Step 3
Controller

ActiveRecord
Step 4
Presenter

Create &
Redirect to New
Step 4
Controller
1001 Wizard
Implementations
1a. One Controller/Presenter Per Wizard Step
Pros
Easy to understand and follow
RESTful
Works with one ActiveRecord
1001 Wizard
Implementations
1a. One Controller/Presenter Per Wizard Step
Cons
Redundant code across controllers
Repetitive redirect logic
Redundant authentication logic
Similar model loading logic
Similar REST actions

Too much presenter validation/persistence
management code
1001 Wizard
Implementations
1b. 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
1b. One Action/Presenter Per Wizard Step
Step 1
Presenter
Validation/Persistance
Adapter

ActiveRecord

Create Step &
Redirect to New Ste

Controller

Step 2
Presenter
Validation/Persistance
Adapter
Step 3
Presenter
Validation/PersistanceAdapt
er

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 4
Presenter
Validation/Persistance Adapter
1001 Wizard
Implementations
1b. One Action/Presenter Per Wizard Step
Pros
Easy to understand and follow
Works with one ActiveRecord
Lighter controller code
Reused controller hooks (e.g. authentication)
1001 Wizard
Implementations
1b. One Action/Presenter Per Wizard Step
Cons
Not RESTful
Redundant code across actions
Repetitive redirect logic
Repetitive update logic

Too much presenter management code
1001 Wizard
Implementations
2. Session Accumulation

Step 1

Create one ActiveRecord
Have multiple Controllers or Actions accumulate
wizard step data in the session
Have ActiveModel presenters handle
validation/forms
On the final step, create ActiveRecord running all
Create
validations
Step 3

Accumulate
in Session

Step 2

Accumulate
in Session

Accumulate
in Session

Step 4

ActiveRecor
d
1001 Wizard
Implementations
2. Session Accumulation
Pros
Works with one ActiveRecord
1001 Wizard
Implementations
2. Session Accumulation
Cons
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
3. Hidden Value Accumulation
Same as session value accumulation except the
controller manages data coming from a request
parameter
Same pros and cons as Session Value
Accumulation except that it has no scalability
implications
NOTE: hidden value must be encrypted for
security
1001 Wizard
Implementations
4. State Machine
Create one ActiveRecord
Make ActiveRecord a state machine managing steps:
adding a step column
add current_step, next_step, and prev_step methods

Different view per step
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
4. State Machine
Pros
Works with one ActiveRecord
Simpler step management logic in one controller
1001 Wizard
Implementations
4. State Machine
Cons
Puts presentation concerns in Model (breaks MVC)
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
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
5. 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
REST
MVC
OO
Non-Functional Requirements:
Productivity
Maintainability
Performance
Security
Ultra Light & Maintainable
Wizard
Philosophy:
A wizard is simply a builder of a model
Every step is simply a partial data-view of the
model
REST resource is still the model itself and thus
must have a single controller
Models must manage validations without
conditions by relying on step-polymorphism
View forms are presented based on steppolymorphism
Ultra Light & Maintainable
Wizard
Details:
Create one main ActiveRecord model
Create “step submodels” subclassing the main model:
Each presents a partial view of the main model
Each has validations only for its step, thus no
conditionals
Each has before and after initialization for that step

Create a single controller:
It begins wizard by creating main ActiveRecord
Every step represents an Edit action of a “step
submodel”

Create a different view per step, each binding to “step
submodel” to ensure the right validations are triggered
Ultra Light & Maintainable
Wizard
Update &
Redirect to Edit
Step 1
ActiveRecord
Validations/Step Logic

Controller
Step 2
ActiveRecord
Validations/Step Logic
Step 3
ActiveRecord
Validations/Step Logic
ActiveRecord
Step 4
ActiveRecord
Validations/Step Logic

Create
Edit
Update
Ultra Light & Maintainable
Wizard
Code Review in Editor
Review
Why Use A Wizard?
Wizard Example
Wizard Implementation Goals
1001 Wizard Implementations

Ultra Light & Maintainable Wizard
Contact
Andy Maleh
BLOG: http://andymaleh.blogspot.com
TWITTER: @AndyMaleh
WEBSITE:http://www.softbuzz.ca

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (20)

Supervise your Akka actors
Supervise your Akka actorsSupervise your Akka actors
Supervise your Akka actors
 
Azure Functions in Action #CodePaLOUsa
Azure Functions in Action #CodePaLOUsaAzure Functions in Action #CodePaLOUsa
Azure Functions in Action #CodePaLOUsa
 
Avoiding integration hell
Avoiding integration hellAvoiding integration hell
Avoiding integration hell
 
Integration Testing as Validation and Monitoring
 Integration Testing as Validation and Monitoring Integration Testing as Validation and Monitoring
Integration Testing as Validation and Monitoring
 
Asp.net event handler
Asp.net event handlerAsp.net event handler
Asp.net event handler
 
[UC4] Version and Automate Everything
[UC4] Version and Automate Everything[UC4] Version and Automate Everything
[UC4] Version and Automate Everything
 
Developer day - AWS: Fast Environments = Fast Deployments
Developer day - AWS: Fast Environments = Fast DeploymentsDeveloper day - AWS: Fast Environments = Fast Deployments
Developer day - AWS: Fast Environments = Fast Deployments
 
C# Async/Await Explained
C# Async/Await ExplainedC# Async/Await Explained
C# Async/Await Explained
 
POST/CON 2019 Workshop: Testing, Automated Testing, and Reporting APIs with P...
POST/CON 2019 Workshop: Testing, Automated Testing, and Reporting APIs with P...POST/CON 2019 Workshop: Testing, Automated Testing, and Reporting APIs with P...
POST/CON 2019 Workshop: Testing, Automated Testing, and Reporting APIs with P...
 
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
 
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
 
Durable functions
Durable functionsDurable functions
Durable functions
 
Fast Track introduction to ASP.NET MVC
Fast Track introduction to ASP.NET MVCFast Track introduction to ASP.NET MVC
Fast Track introduction to ASP.NET MVC
 
SauceCon 2017: Testing @ the Speed of Concurrency
SauceCon 2017: Testing @ the Speed of ConcurrencySauceCon 2017: Testing @ the Speed of Concurrency
SauceCon 2017: Testing @ the Speed of Concurrency
 
How to fix a bug in production - Rollout.io
How to fix a bug in production - Rollout.ioHow to fix a bug in production - Rollout.io
How to fix a bug in production - Rollout.io
 
TDD & VIPER
TDD & VIPERTDD & VIPER
TDD & VIPER
 
Microstrategy Integrity Manager for QA tests automation
Microstrategy Integrity Manager for QA tests automationMicrostrategy Integrity Manager for QA tests automation
Microstrategy Integrity Manager for QA tests automation
 
Monitoring your API
Monitoring your APIMonitoring your API
Monitoring your API
 
Jeremy Edberg (MinOps ) - How to build a solid infrastructure for a startup t...
Jeremy Edberg (MinOps ) - How to build a solid infrastructure for a startup t...Jeremy Edberg (MinOps ) - How to build a solid infrastructure for a startup t...
Jeremy Edberg (MinOps ) - How to build a solid infrastructure for a startup t...
 

Semelhante a Ultra Light and Maintainable Wizards in Rails at Montreal.rb

6 f13g formation-ibm-sterling-control-center-managing-control-center
6 f13g formation-ibm-sterling-control-center-managing-control-center6 f13g formation-ibm-sterling-control-center-managing-control-center
6 f13g formation-ibm-sterling-control-center-managing-control-center
CERTyou Formation
 
Techdays 2013 managing your hybrid cloud datacenter with scom 2012 and what...
Techdays 2013   managing your hybrid cloud datacenter with scom 2012 and what...Techdays 2013   managing your hybrid cloud datacenter with scom 2012 and what...
Techdays 2013 managing your hybrid cloud datacenter with scom 2012 and what...
CompuTrain. De IT opleider.
 

Semelhante a Ultra Light and Maintainable Wizards in Rails at Montreal.rb (20)

Automated Acceptance Testing Example
Automated Acceptance Testing ExampleAutomated Acceptance Testing Example
Automated Acceptance Testing Example
 
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...
 
Eclipse MVC
Eclipse MVCEclipse MVC
Eclipse MVC
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in Rails
 
ASP.NET MVC Extensibility
ASP.NET MVC ExtensibilityASP.NET MVC Extensibility
ASP.NET MVC Extensibility
 
VMworld 2015: Automating Everything VMware with PowerCLI- Deep Dive
VMworld 2015: Automating Everything VMware with PowerCLI- Deep DiveVMworld 2015: Automating Everything VMware with PowerCLI- Deep Dive
VMworld 2015: Automating Everything VMware with PowerCLI- Deep Dive
 
OpenWhisk Under the Hood -- London Oct 16 2016
OpenWhisk Under the Hood -- London Oct 16 2016OpenWhisk Under the Hood -- London Oct 16 2016
OpenWhisk Under the Hood -- London Oct 16 2016
 
VMworld 2013: Three Quirky Ways to Simplify DR with VMware Site Recovery Manager
VMworld 2013: Three Quirky Ways to Simplify DR with VMware Site Recovery ManagerVMworld 2013: Three Quirky Ways to Simplify DR with VMware Site Recovery Manager
VMworld 2013: Three Quirky Ways to Simplify DR with VMware Site Recovery Manager
 
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
 
Mastering the Lightning Framework - Part 2
Mastering the Lightning Framework - Part 2Mastering the Lightning Framework - Part 2
Mastering the Lightning Framework - Part 2
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
10215 A 12
10215 A 1210215 A 12
10215 A 12
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
Deep dive into new features in v realizeoperations 6.0
Deep dive into new features in v realizeoperations 6.0Deep dive into new features in v realizeoperations 6.0
Deep dive into new features in v realizeoperations 6.0
 
Cloud Networking
Cloud NetworkingCloud Networking
Cloud Networking
 
6 f13g formation-ibm-sterling-control-center-managing-control-center
6 f13g formation-ibm-sterling-control-center-managing-control-center6 f13g formation-ibm-sterling-control-center-managing-control-center
6 f13g formation-ibm-sterling-control-center-managing-control-center
 
OpenShift/Kubernetes to Splunk log integration
OpenShift/Kubernetes to Splunk log integrationOpenShift/Kubernetes to Splunk log integration
OpenShift/Kubernetes to Splunk log integration
 
Techdays 2013 managing your hybrid cloud datacenter with scom 2012 and what’s...
Techdays 2013 managing your hybrid cloud datacenter with scom 2012 and what’s...Techdays 2013 managing your hybrid cloud datacenter with scom 2012 and what’s...
Techdays 2013 managing your hybrid cloud datacenter with scom 2012 and what’s...
 
Techdays 2013 managing your hybrid cloud datacenter with scom 2012 and what...
Techdays 2013   managing your hybrid cloud datacenter with scom 2012 and what...Techdays 2013   managing your hybrid cloud datacenter with scom 2012 and what...
Techdays 2013 managing your hybrid cloud datacenter with scom 2012 and what...
 
Neoload
Neoload Neoload
Neoload
 

Mais de 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
 

Mais de 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
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

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)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
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...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
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...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
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
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
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
 
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...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

Ultra Light and Maintainable Wizards in Rails at Montreal.rb

  • 1. Ultra Light & Maintainable Wizards in Rails Andy Maleh President and Chief Journeyman Soft Buzz
  • 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 – Personal Info
  • 8. Wizard Example Step 2 – Background Info
  • 10. Wizard Example Step 4 – Payment
  • 11. Wizard Example Steps Done – Order Confirmation
  • 12. Wizard Implementation Goals REST MVC OO Non-Functional Requirements: Productivity Maintainability Performance Security
  • 13. 1001 Wizard Implementations 1. One Controller/ActiveRecord Per Wizard Step • Create a REST resource per wizard step • • • Multiple ActiveRecords with distinct validations Multiple Controllers Multiple sets of Views and Helpers • Bind every wizard step form to corresponding ActiveRecord • Have each wizard step controller redirect to the next one on create or update
  • 14. 1001 Wizard Implementations 1. One Controller/ActiveRecord Per Wizard Step Step 1 ActiveRecord Step 1 Controller Step 2 ActiveRecord Create & Redirect to New Step 2 Controller Create & Redirect to New Step 3 ActiveRecord Step 3 Controller Step 4 ActiveRecord Create & Redirect to New Step 4 Controller
  • 15. 1001 Wizard Implementations 1. One Controller/ActiveRecord Per Wizard Step Pros Easy to understand and follow RESTful
  • 16. 1001 Wizard Implementations 1. One Controller/ActiveRecord Per Wizard Step Cons Redundant code across controllers Repetitive redirect logic Redundant authentication logic Similar model loading logic Similar REST actions Tying database tables to presentation details
  • 17. 1001 Wizard Implementations 1a. One Controller/Presenter Per Wizard Step • Create one ActiveRecord • Create a controller and 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 controller redirect to the next one on create or update
  • 18. 1001 Wizard Implementations 1a. One Controller/Presenter Per Wizard Step Step 1 Controller Step 1 Presenter Step 2 Presenter Create & Redirect to New Step 2 Controller Create & Redirect to New Step 3 Presenter Step 3 Controller ActiveRecord Step 4 Presenter Create & Redirect to New Step 4 Controller
  • 19. 1001 Wizard Implementations 1a. One Controller/Presenter Per Wizard Step Pros Easy to understand and follow RESTful Works with one ActiveRecord
  • 20. 1001 Wizard Implementations 1a. One Controller/Presenter Per Wizard Step Cons Redundant code across controllers Repetitive redirect logic Redundant authentication logic Similar model loading logic Similar REST actions Too much presenter validation/persistence management code
  • 21. 1001 Wizard Implementations 1b. 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
  • 22. 1001 Wizard Implementations 1b. One Action/Presenter Per Wizard Step Step 1 Presenter Validation/Persistance Adapter ActiveRecord Create Step & Redirect to New Ste Controller Step 2 Presenter Validation/Persistance Adapter Step 3 Presenter Validation/PersistanceAdapt er 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 4 Presenter Validation/Persistance Adapter
  • 23. 1001 Wizard Implementations 1b. One Action/Presenter Per Wizard Step Pros Easy to understand and follow Works with one ActiveRecord Lighter controller code Reused controller hooks (e.g. authentication)
  • 24. 1001 Wizard Implementations 1b. One Action/Presenter Per Wizard Step Cons Not RESTful Redundant code across actions Repetitive redirect logic Repetitive update logic Too much presenter management code
  • 25. 1001 Wizard Implementations 2. Session Accumulation Step 1 Create one ActiveRecord Have multiple Controllers or Actions accumulate wizard step data in the session Have ActiveModel presenters handle validation/forms On the final step, create ActiveRecord running all Create validations Step 3 Accumulate in Session Step 2 Accumulate in Session Accumulate in Session Step 4 ActiveRecor d
  • 26. 1001 Wizard Implementations 2. Session Accumulation Pros Works with one ActiveRecord
  • 27. 1001 Wizard Implementations 2. Session Accumulation Cons 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
  • 28. 1001 Wizard Implementations 3. Hidden Value Accumulation Same as session value accumulation except the controller manages data coming from a request parameter Same pros and cons as Session Value Accumulation except that it has no scalability implications NOTE: hidden value must be encrypted for security
  • 29. 1001 Wizard Implementations 4. State Machine Create one ActiveRecord Make ActiveRecord a state machine managing steps: adding a step column add current_step, next_step, and prev_step methods Different view per step Have single ActiveRecord manage each step validations by relying on conditional validations. For example: validate :phone, presence: true, if: lambda {current_step==“shipping”}
  • 30. 1001 Wizard Implementations 4. State Machine Pros Works with one ActiveRecord Simpler step management logic in one controller
  • 31. 1001 Wizard Implementations 4. State Machine Cons Puts presentation concerns in Model (breaks MVC) 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 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
  • 32. 1001 Wizard Implementations 5. 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
  • 34. Ultra Light & Maintainable Wizard Philosophy: A wizard is simply a builder of a model Every step is simply a partial data-view of the model REST resource is still the model itself and thus must have a single controller Models must manage validations without conditions by relying on step-polymorphism View forms are presented based on steppolymorphism
  • 35. Ultra Light & Maintainable Wizard Details: Create one main ActiveRecord model Create “step submodels” subclassing the main model: Each presents a partial view of the main model Each has validations only for its step, thus no conditionals Each has before and after initialization for that step Create a single controller: It begins wizard by creating main ActiveRecord Every step represents an Edit action of a “step submodel” Create a different view per step, each binding to “step submodel” to ensure the right validations are triggered
  • 36. Ultra Light & Maintainable Wizard Update & Redirect to Edit Step 1 ActiveRecord Validations/Step Logic Controller Step 2 ActiveRecord Validations/Step Logic Step 3 ActiveRecord Validations/Step Logic ActiveRecord Step 4 ActiveRecord Validations/Step Logic Create Edit Update
  • 37. Ultra Light & Maintainable Wizard Code Review in Editor
  • 38. Review Why Use A Wizard? Wizard Example Wizard Implementation Goals 1001 Wizard Implementations Ultra Light & Maintainable Wizard
  • 39. Contact Andy Maleh BLOG: http://andymaleh.blogspot.com TWITTER: @AndyMaleh WEBSITE:http://www.softbuzz.ca