Form Objects Design Pattern Discussion
Nice episode, I've been using forms like this all over the place, especially when an AR object can be saved via many different use cases with many different "valid" states. One quibble, seem more clear to do something like:
especially since the contact form is not really saving anything.
as far as ideas for further examples: in the realm of interesting forms I'd be curious to see your approach to a multi-step, "Wizard"-style experience where multiple forms are filled out for different records (with or without branching paths depending on earlier data :) )
Yep definitely relevant, the old wizard where you need to progressively build up multiple associated models across a few steps.
An example for the work we do, in our case, we deal with the provision of a lot of information based products, which require the user to enter a lot of their information in, and then they finally submit the "form" at the end, but it might have been 5 - 6 steps/pages updating/creating multiple models.
Currently, we've used Wicked Wizard gem to hide a lot of the mess away from non-RESTfully updating a model (or multiple).
Would be great to get some more insights as to how a Form Object could be applied in the context of a wizard. Currently, we're using a liberal application of nested_attributes_for, but it's quite brittle, and particularly, requires a lot of condition validation for each step that saves back to the model (because they are all pretty much only ever partially valid at each step until the final step is saved)..
An example: say our primary model is an CarInsuranceQuote, and associated to this CarInsuranceQuote model would Addresses, Names, Cars.
Form Step 1: Car Details & Name of Insured (creates a new CarInsuranceQuote, and build associated Name model on the quote, eg CarInsuranceQuote.cars.new(car_attrs), CarInsuranceQuote.names.create(names_attrs).
Form Step 2: Address Details for Quote,
ie: CarInsuranceQuote.addresses.create(address_attrs). And maybe save a few other attributes on the CarInsuranceQuote.update_attributes(ciq_attrs).
Form Step 3: Finalise Quote by Creating a User for the quote
As you can see, each step would build different associations, but might also partially update the parent model in the Wizard.
Am I right in understanding that that works only for wizards dealing with _single models_, broken into steps?
This repo is an updated version of Ryan Bates screencast for Rails 5. I've used it on a project and it's great but the model had no associations so I'm not sure if it would work for nested resources
Wow, I am struggling with multi-steps forms as well. I definitely would support the creation of an episode for multi-step forms!
Awesome episode! Building something like this in an existing project as we speak (or as I type) :)
how do you handle Active Record Callbacks in form objects design patterns?
Hey, Chris!) 10Q for awesome episode=) What do you think about "Interactor Pattern"? It could be very interesting and useful for community to build it from scratch and use 'interactor' gem for organizers!) Something like this:
Thank You for attention)
Do we really still need strong parameters after implementing a form object ? Because the form object only has the fields that are relevant for the form and i don't see any way in injecting data back to the original object.