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.

57676e2d666be331e1c81fbb1d45a91a?s=128

Andrew Markle

April 25, 2017
Tweet

Transcript

  1. FORM OBJECTS Decouple your models with

  2. None
  3. data model == form

  4. data model == form accepts_nested_attributes_for

  5. store data 
 in the session session[:user_attributes] = @user.all_the_attributes

  6. None
  7. state machine

  8. nested models class Dog::BreedDetails < Dog

  9. view > model VIEW MODEL

  10. Let’s not do it!

  11. Form Objects

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

    3 What is a 
 form object?
  13. 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 %>
  14. F O R M O B J E C T

    3
  15. F O R M O B J E C T

    3
  16. None
  17. 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
  18. 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
  19. 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
  20. F O R M O B J E C T

    3
  21. None
  22. None
  23. None
  24. None
  25. None
  26. None
  27. None
  28. None
  29. None
  30. None
  31. None
  32. None
  33. None
  34. None
  35. None
  36. None
  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. None
  44. None
  45. None
  46. None
  47. None
  48. None
  49. None
  50. None
  51. None
  52. None
  53. None
  54. None
  55. None
  56. None
  57. None
  58. None
  59. None
  60. None
  61. None
  62. None
  63. None
  64. None
  65. None
  66. None
  67. None
  68. None
  69. None
  70. None
  71. None
  72. None
  73. None
  74. None
  75. None
  76. None
  77. None
  78. None
  79. None
  80. None
  81. None
  82. None
  83. None
  84. None
  85. None
  86. None
  87. None
  88. F O R M O B J E C T

    3
  89. F O R M O B J E C T

    3
  90. None
  91. None
  92. None
  93. None
  94. None
  95. None
  96. None
  97. None
  98. F O R M O B J E C T

    3
  99. None
  100. None
  101. None
  102. None
  103. None
  104. None
  105. None
  106. Prepopulators Populators

  107. Prepopulators Populators called manually with .prepopulate! prepares the form for

    rendering can fill out fields in the form :new, :edit
  108. 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
  109. None
  110. None
  111. None
  112. None
  113. 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
  114. None
  115. None
  116. F O R M O B J E C T

    3
  117. None
  118. None
  119. None
  120. None
  121. None
  122. None
  123. None
  124. None
  125. None
  126. None
  127. None
  128. None
  129. None
  130. None
  131. None
  132. None
  133. None
  134. None
  135. None
  136. None
  137. None
  138. None
  139. None
  140. None
  141. F O R M O B J E C T

    3
  142. None
  143. 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
  144. None
  145. None
  146. None
  147. None
  148. None
  149. None
  150. None
  151. None
  152. None
  153. None
  154. None
  155. None
  156. None
  157. None
  158. None
  159. None
  160. None
  161. None
  162. None
  163. None
  164. None
  165. A D VA N TA G E S 4 Advantages

  166. 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
  167. 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
  168. 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
  169. 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
  170. 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
  171. 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
  172. The end.

  173. 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 /
  174. @andrewmarkle H T T P S : / / I

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