0
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 & Maintaina...
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
•
•
•...
1001 Wizard
Implementations
1. One Controller/ActiveRecord Per Wizard Step
Step 1
ActiveRecord

Step 1
Controller

Step 2
...
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
Repetiti...
1001 Wizard
Implementations
1a. One Controller/Presenter Per Wizard Step
•

Create one ActiveRecord

•

Create a controlle...
1001 Wizard
Implementations
1a. One Controller/Presenter Per Wizard Step
Step 1
Controller

Step 1
Presenter

Step 2
Prese...
1001 Wizard
Implementations
1a. One Controller/Presenter Per Wizard Step
Pros
Easy to understand and follow
RESTful
Works ...
1001 Wizard
Implementations
1a. One Controller/Presenter Per Wizard Step
Cons
Redundant code across controllers
Repetitive...
1001 Wizard
Implementations
1b. One Action/Presenter Per Wizard Step
•

Create one ActiveRecord

•

Create one controller ...
1001 Wizard
Implementations
1b. One Action/Presenter Per Wizard Step
Step 1
Presenter
Validation/Persistance
Adapter

Acti...
1001 Wizard
Implementations
1b. One Action/Presenter Per Wizard Step
Pros
Easy to understand and follow
Works with one Act...
1001 Wizard
Implementations
1b. One Action/Presenter Per Wizard Step
Cons
Not RESTful
Redundant code across actions
Repeti...
1001 Wizard
Implementations
2. Session Accumulation

Step 1

Create one ActiveRecord
Have multiple Controllers or Actions ...
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 co...
1001 Wizard
Implementations
3. Hidden Value Accumulation
Same as session value accumulation except the
controller manages ...
1001 Wizard
Implementations
4. State Machine
Create one ActiveRecord
Make ActiveRecord a state machine managing steps:
add...
1001 Wizard
Implementations
4. State Machine
Pros
Works with one ActiveRecord
Simpler step management logic in one control...
1001 Wizard
Implementations
4. State Machine
Cons
Puts presentation concerns in Model (breaks MVC)
The state machine wizar...
1001 Wizard
Implementations
5. Gems
Wizardry: state machine in model
Wicked: clean state machine in controller (better)
bu...
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-...
Ultra Light & Maintainable
Wizard
Details:
Create one main ActiveRecord model
Create “step submodels” subclassing the main...
Ultra Light & Maintainable
Wizard
Update &
Redirect to Edit
Step 1
ActiveRecord
Validations/Step Logic

Controller
Step 2
...
Ultra Light & Maintainable
Wizard
Code Review in Editor
Review
Why Use A Wizard?
Wizard Example
Wizard Implementation Goals
1001 Wizard Implementations

Ultra Light & Maintainabl...
Contact
Andy Maleh
BLOG: http://andymaleh.blogspot.com
TWITTER: @AndyMaleh
WEBSITE:http://www.softbuzz.ca
Upcoming SlideShare
Loading in...5
×

Ultra Light and Maintainable Wizards in Rails at Montreal.rb

8,231

Published on

You may have learned how to write proper Rails MVC features with skinny controllers and fat models. You may have learned how to properly compose your features in terms of RESTful resources. But, how do you write a proper easy to maintain Wizard without resulting in a fat controller, violating REST, or writing complicated Wizard step data management code? Do you use multiple controllers? Multiple actions in one controller? A state machine? An existing Wizard gem?

These are all questions that will get answered in this talk, in which I plan to share a simple Wizard approach that I stumbled upon while working on my last two Rails projects. It made it super easy to add Wizard steps by both junior and senior Rails developers on the projects.

Attendees should walk out of this talk with solid knowledge on how to build a light maintainable Rails Wizard, achieving proper separation of responsibilities between controllers and models, and maintaining a RESTful paradigm without the complexity of Wizard step management.

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,231
On Slideshare
0
From Embeds
0
Number of Embeds
55
Actions
Shares
0
Downloads
7
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Ultra Light and Maintainable Wizards in Rails at Montreal.rb"

  1. 1. Ultra Light & Maintainable Wizards in Rails Andy Maleh President and Chief Journeyman Soft Buzz
  2. 2. Overview Why Use A Wizard? Wizard Example Wizard Implementation Goals 1001 Wizard Implementations Ultra Light & Maintainable Wizard
  3. 3. Why Use A Wizard? Avoid overwhelming user with a huge form
  4. 4. Why Use A Wizard? Simplify a workflow into multiple steps
  5. 5. Why Use A Wizard? Help the user by providing guidance
  6. 6. Wizard Example Event Registration Wizard
  7. 7. Wizard Example Step 1 – Personal Info
  8. 8. Wizard Example Step 2 – Background Info
  9. 9. Wizard Example Step 3 – Review
  10. 10. Wizard Example Step 4 – Payment
  11. 11. Wizard Example Steps Done – Order Confirmation
  12. 12. Wizard Implementation Goals REST MVC OO Non-Functional Requirements: Productivity Maintainability Performance Security
  13. 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. 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. 15. 1001 Wizard Implementations 1. One Controller/ActiveRecord Per Wizard Step Pros Easy to understand and follow RESTful
  16. 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. 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. 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. 19. 1001 Wizard Implementations 1a. One Controller/Presenter Per Wizard Step Pros Easy to understand and follow RESTful Works with one ActiveRecord
  20. 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. 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. 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. 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. 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. 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. 26. 1001 Wizard Implementations 2. Session Accumulation Pros Works with one ActiveRecord
  27. 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. 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. 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. 30. 1001 Wizard Implementations 4. State Machine Pros Works with one ActiveRecord Simpler step management logic in one controller
  31. 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. 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
  33. 33. Wizard Implementation Goals REST MVC OO Non-Functional Requirements: Productivity Maintainability Performance Security
  34. 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. 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. 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. 37. Ultra Light & Maintainable Wizard Code Review in Editor
  38. 38. Review Why Use A Wizard? Wizard Example Wizard Implementation Goals 1001 Wizard Implementations Ultra Light & Maintainable Wizard
  39. 39. Contact Andy Maleh BLOG: http://andymaleh.blogspot.com TWITTER: @AndyMaleh WEBSITE:http://www.softbuzz.ca
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×