• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Ultra Light and Maintainable Wizards in Rails at Montreal.rb
 

Ultra Light and Maintainable Wizards in Rails at Montreal.rb

on

  • 3,613 views

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 ...

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.

Statistics

Views

Total Views
3,613
Views on SlideShare
1,748
Embed Views
1,865

Actions

Likes
2
Downloads
3
Comments
0

46 Embeds 1,865

http://andymaleh.blogspot.com 810
http://andymaleh.blogspot.ca 242
http://andymaleh.blogspot.in 182
http://candidosalesg.wordpress.com 115
http://andymaleh.blogspot.co.uk 79
http://andymaleh.blogspot.com.br 45
http://andymaleh.blogspot.dk 31
http://andymaleh.blogspot.fr 30
http://andymaleh.blogspot.ru 30
http://andymaleh.blogspot.de 29
http://andymaleh.blogspot.it 28
http://andymaleh.blogspot.com.au 27
http://andymaleh.blogspot.com.es 26
http://andymaleh.blogspot.nl 23
http://andymaleh.blogspot.se 12
http://andymaleh.blogspot.jp 11
http://andymaleh.blogspot.com.ar 11
http://andymaleh.blogspot.ro 11
http://andymaleh.blogspot.be 10
http://andymaleh.blogspot.ch 10
http://andymaleh.blogspot.tw 9
http://andymaleh.blogspot.mx 9
http://andymaleh.blogspot.co.at 8
http://www.andymaleh.blogspot.ca 8
http://andymaleh.blogspot.fi 6
http://andymaleh.blogspot.no 6
http://andymaleh.blogspot.hu 6
http://andymaleh.blogspot.co.il 6
http://andymaleh.blogspot.cz 5
http://andymaleh.blogspot.gr 5
http://andymaleh.blogspot.ie 5
http://andymaleh.blogspot.sg 5
http://andymaleh.blogspot.com.tr 4
http://andymaleh.blogspot.co.nz 4
http://andymaleh.blogspot.sk 2
http://andymaleh.blogspot.hk 2
http://andymaleh.blogspot.ae 2
http://andymaleh.blogspot.pt 2
http://andymaleh.blogspot.kr 2
http://www.newsblur.com 1
http://cloud.feedly.com 1
http://prlog.ru 1
http://news.google.com 1
http://www.andymaleh.blogspot.com 1
http://flavors.me 1
http://www.google.ca 1
More...

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

    • 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