Talks

10 Things You Never Wanted To Know About Reform 3

wroc_love.rb 2022

00:00:16.320 Thank you so much for this warm welcome. I'm actually really positively surprised by this turnout. I wasn't sure if I would be speaking in front of five or ten people, but apparently lots of people came to this conference on a Friday night, which is great. I almost didn't make it because my train got canceled, and I had to take another train, and another train, and another train to get here. I literally just arrived five minutes ago, but hey, everything is cool. The projector is working, my laptop is working, and you're all here. This is great!
00:00:55.360 Who's staying until Monday? The same eight people who don't know me? Okay, so I don't know if you have noticed, but this room is at a 45-degree angle. I will start sweating, and it's not because I'm nervous, it's just very warm in here. Keep your beanies on, like this one guy with a red beanie sitting there and not sweating. I'm impressed.
00:01:28.000 To make this talk something new, I actually had to go through a rather unpleasant experience. I had to watch my own talks, and while the content was okay, the way they were presented was always a mixed experience. I remember one talk I gave at WrocLove in 2018 called 'Super Ain't Super,' and I had to ensure that I wasn’t repeating myself. I also watched another talk titled 'Reform: What Went Wrong?' which was an exploration of the mistakes I made while designing the Reform gem. I gave that talk in Odessa back in 2017 or 2018 when there was already a small conflict in the country, with the Russians only claiming Crimea back then. It was a bit unsettling, but there were no bombs flying.
00:02:38.000 The conference organizers had two major concerns: whether they should hold the conference with the ongoing conflict and whether it would be appropriate to have a white male-only conference. It was bizarre to see people complaining about this while the organizers were struggling to make a decision amid these tense circumstances. It’s even more bizarre in retrospect considering that they are now escaping the country due to the ongoing conflict.
00:03:24.959 But let's not get too political; we are here to discuss beautiful things like code and software concepts, and of course, enjoying some drinks. In case you haven't noticed, I'm talking about the Reform gem, not politics. The Reform gem is a Ruby library that brings you form objects. It provides a declarative API to define forms, render them, validate incoming data, and persist data into a database or other forms. The beauty of Reform is that it can work with APIs as well as HTML views, making it versatile across various use cases.
00:04:20.799 When we conceptualized Reform, I was working at a publishing company in Australia using Rails features like accepts_nested_attributes_for. This is a feature of Active Record that simplifies form handling. Let’s take an example: If you have a form to create a product, which may also have a linked model called a variant, it allows for automatic creation of nested objects through form submission.
00:05:00.560 However, the problem arises when dealing with more complex structures, such as having multiple nested attributes. While Rails can handle nested attributes, it only provides a single form per model, which can become limiting as applications grow. The initial excitement over this feature can quickly turn to frustration as developers realize the need to handle various states of forms more flexibly.
00:05:59.060 We were thrilled by how Rails handled these attributes, but as the use cases varied and evolved, we began to introduce a series of conditional checks in our code to accommodate different logic paths for form submissions. This overcomplicated our models and validation, leading to messy and unmanageable code.
00:06:28.240 Thus, we undertook the task of creating our own gem to handle forms in Rails, which became part of the Trailblazer framework. While it started as a standalone gem, it gradually garnered a community of contributors, and it's available on GitHub. Now, you can check it out for yourself.
00:07:20.080 Reform employs object-oriented programming principles, where each form corresponds to a single class. For example, we define a ProductCreateForm, enabling clear organization of form fields and validations. This way, validations can exist in one centralized location rather than scattered across the model, promoting code clarity and separation of concerns. One of the key features is that it allows for easy inheritance to create sub-forms for different use cases, such as updates. By extending a create form, adjustments and additions can be made without duplicating code.
00:08:35.360 The concept is that we needed a way to keep the complexity at bay while allowing for needed variations based on use cases. Thus, we focused on standardizing by providing a robust structure for forms while retaining the flexibility for custom validations or additional fields that may be required based on user roles, for example.
00:09:25.119 As our needs evolved, it was clear that the API needed to change to accommodate this flexibility. We started noticing how users were misusing features like populators, which were designed to aid in form handling but were instead becoming a source of confusion and complexity. The design was intended to make things simpler, but instead, it often resulted in unexpected behavior and difficult debugging.
00:10:12.720 We realized that by using populators, we could create nested objects automatically, which turned out to be problematic for users. Many did not fully understand how this feature operated, leading to issues in debugging when exceptions arose.
00:10:42.560 Over time, this frustration with coping and struggling with exceptions in the parsing process became more pronounced, impacting the user base. This drove us to acknowledge that we needed to rethink how we managed interactions with models. The ongoing complexity and difficulty that arose from misusing populators led us to realize that the library's design itself was creating barriers for the developers who were using it.
00:11:51.280 Despite these challenges, people continued using Reform because they recognized its power, even if they were facing struggles with certain features. However, many users ended up overwriting methods in ways that complicated things even further.
00:12:22.000 My take away was that we could not simply fix the symptoms; we had to address the underlying design issues. After a long process of deliberation, we recognized a need for Reform 3. We wanted to modernize the API to provide a better experience overall for the users. The goal became to create a cleaner, more maintainable implementation of the concepts we learned through Reform 2.
00:13:42.000 In Reform 3, we made significant architectural changes. One of the most pivotal changes was ensuring that forms act as transformation layers. The focus shifted from merely serving user inputs to managing data transformations more explicitly.
00:14:56.960 We integrated concepts from the Trailblazer framework to leverage workflows. Parsing, validation, and persistence became steps in a cohesive workflow, allowing us to define how data passes through the system. This way, if one step encounters an error, it dictates the flow and prevents further actions that rely on its success.
00:16:15.200 In conclusion, one of the ten things you never wanted to know about Reform 3 is that it makes the concept of strong parameters nearly obsolete. You don't need them anymore because the form will only pass the parameters it recognizes. This might eliminate a lot of boilerplate code in the controllers.
00:17:25.760 You can utilize both DRY validation and ActiveRecord validation, which brings flexibility into the mix. I understand that many might have had negative experiences with Reform 2, but believe me, Reform 3 comes with lessons learned intent on improvements.
00:18:20.480 I invite everyone to give it another shot. The documentation will improve, so it will be helpful for new users. It's an open-source project, and if you're interested in contributing or learning, there is plenty of value in getting involved.
00:18:55.680 Thank you very much for your attention. If you have questions or would like to chat further, feel free to approach me after this talk. I also have some merchandise available if you're interested.
00:20:05.760 So let’s head to the pub and continue discussing open-source contributions over a beer!