Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Decouple your models with form objects

Decouple your models with form objects

Forms are a crucial part of every app and Rails has good defaults for building them—unless you need something complicated. Maybe you want a multi-step wizard? Or maybe you'd like to pluck attributes from any model? Validation becomes a pain point. So you introduce a state machine, or nest your models, or do some other calisthenic to get everything working. Thankfully there's a better way! This talk takes a complicated wizard and converts it into a few simple form objects—it's a deep dive on decoupling models and how you can leverage Trailblazer's Reform gem to make it even easier.

Andrew Markle

April 25, 2017
Tweet

More Decks by Andrew Markle

Other Decks in Programming

Transcript

  1. F O R M O B J E C T

    3 What is a 
 form object?
  2. F O R M O B J E C T

    3 What is a 
 form object? <%= form_for(@person) do |f| %> <%= f.input :name %> <%= f.button :submit, 'Submit' %> <% end %>
  3. DATA MODEL Company Phone Address User Account name time_zone number

    address city state zip country_code first_name last_name subscription_type kind Form 1 - Nested Data
  4. DATA MODEL Company Phone Address User Account name time_zone number

    address city state zip country_code first_name last_name subscription_type kind Form 2 - Custom Logic
  5. DATA MODEL Company Phone Address User Account name time_zone number

    address city state zip country_code first_name last_name subscription_type kind Form 3 - Data Scatter
  6. Prepopulators Populators called manually with .prepopulate! prepares the form for

    rendering can fill out fields in the form :new, :edit
  7. Prepopulators Populators called manually with .prepopulate! called every time when

    you .validate prepares the form for rendering prepares the form for validating cares about matching the right data to the right model can fill out fields in the form :new, :edit :create, :update
  8. DATA MODEL Company Phone Address User Account name time_zone number

    address city state zip country_code first_name last_name subscription_type kind Form 1 - Nested Data
  9. DATA MODEL Company Phone Address User Account name time_zone number

    address city state zip country_code first_name last_name subscription_type kind Form 3 - Data Scatter
  10. A D VA N TA G E S 4 Form

    
 Objects ‣ Model agnostic ‣ Validation is context-specific ‣ RESTful ‣ Easy to test ‣ Easy to extend and modify ‣ Use when you need to
  11. A D VA N TA G E S 4 Form

    
 Objects ‣ Model agnostic ‣ Validation is context-specific ‣ RESTful ‣ Easy to test ‣ Easy to extend and modify ‣ Use when you need to
  12. A D VA N TA G E S 4 Form

    
 Objects ‣ Model agnostic ‣ Validation is context-specific ‣ RESTful ‣ Easy to test ‣ Easy to extend and modify ‣ Use when you need to
  13. A D VA N TA G E S 4 Form

    
 Objects ‣ Model agnostic ‣ Validation is context-specific ‣ RESTful ‣ Easy to test ‣ Easy to extend and modify ‣ Use when you need to
  14. A D VA N TA G E S 4 Form

    
 Objects ‣ Model agnostic ‣ Validation is context-specific ‣ RESTful ‣ Easy to test ‣ Easy to extend and modify ‣ Use when you need to
  15. A D VA N TA G E S 4 Form

    
 Objects ‣ Model agnostic ‣ Validation is context-specific ‣ RESTful ‣ Easy to test ‣ Easy to extend and modify ‣ Use when you need to
  16. attributes H T T P S : / / F

    O R U M . U P C A S E . C O M / T / F O R M - O B J E C T S / 2 2 6 7 H T T P S : / / R O B O T S . T H O U G H T B O T. C O M / A C T I V E M O D E L - F O R M - O B J E C T S H T T P : / / C O N F R E A K S . T V / V I D E O S / R A I L S C O N F 2 0 1 4 - U LT R A - L I G H T- A N D - M A I N TA I N A B L E - R A I L S - W I Z A R D S H T T P : / / T R A I L B L A Z E R . T O H T T P : / / T R A I L B L A Z E R . T O / G E M S / R E F O R M H T T P : / / J E R O M E D A L B E RT. C O M / H O W- D H H - O R G A N I Z E S - H I S - R A I L S - C O N T R O L L E R S /
  17. @andrewmarkle H T T P S : / / I

    N D U S T R I A L A G E N C Y. C A