Views

Summarized using AI

Extracting Logic From Templates With Hanami Views

Sebastian Wilgosz • May 15, 2024 • Wrocław, Poland

In the video titled "Extracting Logic From Templates With Hanami Views" presented by Sebastian Wilgosz at the wroc_love.rb 2024 event, the speaker addresses a common challenge in the Ruby ecosystem related to maintaining logic-free views. The presentation focuses on the Hanami framework, an alternative to Ruby on Rails, and highlights its modular architecture that promotes cleaner views and better logic encapsulation.

Key points discussed include:

  • Introduction to Hanami: Hanami is introduced as a flexible Ruby framework with a small market share compared to Rails, yet it showcases minimal competition for developers wanting to write about it.
  • Comparison with Rails: While Rails dominates the Ruby web development market, it can be inflexible due to its monolithic nature and complexity in replacing core components. In contrast, Hanami allows for a more fine-grained approach where each functionality can be added or removed as needed.
  • Logic Extraction: The main thrust of the talk centers on the importance of extracting logic from templates and how Hanami provides the tools to achieve this. For instance, the speaker illustrates how a simple registration form can hide a lot of logic, which can be extracted into Ruby objects for better readability and maintainability.
  • Encapsulation Techniques: Specific techniques are provided for managing field errors, refining input logic using reusable partials, and leveraging scopes for improved organization. This method promotes a clear separation of concerns, ensuring that logic does not clutter the core templates.
  • Use of Decorators and Scopes: The speaker discusses utilizing decorators and scopes to simplify the implementation while keeping the views intuitive and organized. This also aids in managing the data passed to partials, fostering a clean development process.

In conclusion, Hanami offers a robust set of tools tailored for improving development workflows in Ruby projects. The community is encouraged to engage in discussions around various approaches to logic extraction, ensuring continuous improvement in codebases. Wilgosz emphasizes that adopting innovative practices from beyond the Ruby ecosystem can further benefit developers.

Overall, the talk effectively supports the notion that while creating and managing templates can be arduous, Hanami provides a comprehensive framework that enhances cleanliness and testability in web development workflows.

Extracting Logic From Templates With Hanami Views
Sebastian Wilgosz • May 15, 2024 • Wrocław, Poland

wroclove.rb 2024

00:00:10.120 Hi, thanks for being here. Today, I will talk about a very common problem in the Ruby ecosystem.
00:00:15.000 This is something that I haven't really experienced in my long career as a Ruby developer: logic-free views.
00:00:20.279 When preparing for today’s presentation, I tried to put myself in the shoes of the audience.
00:00:24.720 I realized how many of you actually use Ruby on a daily basis. So, I want to cater to 99% of the Ruby audience.
00:00:30.640 I don't just want to mention that there are problems, but I also want to present some kind of solution.
00:00:37.960 This image came to mind immediately: I felt like a sheep among wolves. However, I realized that if I introduced myself that way, I would just be eaten.
00:00:45.920 So, I asked an AI generator for another image to present myself, hoping to gain some confidence.
00:00:50.520 I'm Sebastian, and I have already introduced myself, so I won't repeat that. Now, let's delve into the topic.
00:00:55.000 I will be discussing Hanami today. But what is Hanami? Hanami is a Ruby framework, an alternative to Rails.
00:01:02.640 Some cool facts about Hanami are that when you represent the View layer in a diagram, it appears as a Ruby shape.
00:01:09.959 The architecture is so well thought out, yet Ruby's market share is quite small in the web ecosystem.
00:01:13.879 We, in this room, represent maybe 2-3% of the entire web application market.
00:01:21.360 Ruby on Rails has about 95% of the Ruby market in web development, while Hanami, Sinatra, Bridgetown, and others make up only around 5%.
00:01:30.160 Though these are small projects, they are very important.
00:01:35.000 Hanami is a framework that, if you decide to write about or blog, you will face little competition.
00:01:42.120 There are companies making millions of dollars with Hanami, proving it's viable.
00:01:45.000 While the community has developed sophisticated approaches and invented additional building blocks such as service objects, view objects, form objects, and decorators, nothing is strongly backed by Ruby’s philosophy.
00:01:55.000 In Rails, we have the classic MVC pattern. However, the structures are not as extensive as they could be.
00:02:01.479 There is indeed a lot of ongoing debate regarding architecture.
00:02:06.959 Rails is also somewhat a one-size-fits-all solution; it took years to allow API-only applications.
00:02:11.360 With Rails, you can choose to disable certain key components, but you still have to install them.
00:02:17.480 Because of Ruby's monkey patching capabilities, it can be hard to replace major components like Active Record.
00:02:25.479 In contrast, Hanami allows much more flexibility.
00:02:30.000 Its philosophy lets you install each functionality separately. If you don't need a controller, validations, or views, you can completely remove them.
00:02:39.440 Because of this modularity, I brought this topic to the Ruby community.
00:02:43.520 Recently, the 2.1 release of Hanami has been published, and now we officially have the Hanami View module gem.
00:02:49.880 This module is framework-agnostic and can be used in any Ruby application. I will show it to inspire you.
00:03:03.279 I don't expect all of you to replace your existing templates with Hanami views, but I hope some concepts can improve your current projects.
00:03:07.600 For example, the simple registration form I have here is quite straightforward: a few text fields, one checkbox, two buttons, a few icons, and some placeholders. But still, if you check the HTML, you'll find it's quite bloated to make it look good.
00:03:27.160 This simple form hides a lot of logic; I’ll show you how even though some aspects may not seem obvious. Hanami addresses this by providing additional tooling to extract some logic into Ruby.
00:03:39.160 The goal is to encapsulate logic in Ruby objects that are easily testable, thus keeping your views clean.
00:03:57.760 For instance, rendering field errors can be managed through creating a view that exposes a form object and decorating a hash of values and errors.
00:04:11.200 With this, you can highlight input fields and present specific field errors or information at the top.
00:04:15.600 Even with such a simple form, if you dig deeper, you'll see how much logic can be hidden beneath. Hanami's approach of creating a clean view architecture promotes better encapsulation.
00:04:28.800 For example, we can abstract the rendering of field errors. Instead of managing this logic directly in the template, we can return a structured error array from a dedicated method, allowing us to avoid nil values and ensuring we're only getting back the expected array of error messages.
00:05:01.280 This logical separation allows our form fields to remain more neatly organized and focused on the visual presentation.
00:05:07.760 Next, let’s talk about refining the form input logic. We can extract the logic concerning a single field by turning it into a reusable partial.
00:05:14.560 The objective is to create a partial applicable for multiple use cases. All three fields in my example share similar requirements, so we can identify dynamic variables like field names and labels to be passed in.
00:05:29.760 Elements such as icons can also be factored out depending on their use, ensuring that our templates remain tidy.
00:05:38.680 As we extract these elements, we can streamline the rendering logic, ensuring that classes for error messages render appropriately based on the presence of certain dynamic attributes.
00:05:57.920 The idea is to ensure variable names and error functions are passed to the relevant partials seamlessly, maintaining a clean and intuitive view.
00:06:19.080 The main challenge here is to ensure that no unnecessary logic ends up in the core templates themselves. This practice promotes clarity and testability for future development.
00:06:33.680 Ultimately, we can leverage scopes within Hanami to minimize the number of locals passed to our partials.
00:06:44.760 Scopes provide a structured means to process locals and execute calculations for our input fields.
00:06:58.040 In this way, a scope can encapsulate all relevant logic related to various fields, ensuring our partial displays only what is necessary with minimal passed locals.
00:07:16.720 However, if you want to simplify further, the use of decorators can really help streamline your implementation.
00:07:31.640 You can define scope methods for functionalities like username and password inputs, keeping everything clean and straightforward.
00:07:42.680 Interestingly, this process illustrates the duality of keeping information about which partial is rendered while making the process user-friendly.
00:07:52.080 The resultant forms maintain clarity while encapsulating the complexities, demonstrating how Hanami organizes structured output effectively.
00:08:05.240 While leveraging Hanami's tools enhances the efficiency of constructing views, it also encourages a design philosophy that takes inspiration from other innovative solutions in and beyond Ruby.
00:08:23.680 In conclusion, working with templates can be challenging. However, Hanami offers complete tooling aimed at enhancing our development workflow.
00:08:40.960 I believe it equips us with one of the most comprehensive sets of tools available on the web today, regardless of the various alternatives out there.
00:08:58.080 As a community, we should maintain a sophisticated dialogue about diverse approaches to logic extraction. This openness can lead to opportunities to enhance our codebases, and we should not hesitate to adopt non-Ruby strategies when appropriate.
00:09:10.960 Thanks.
00:09:16.360 Does anyone have questions?
00:09:19.600 I really liked the part where you critiqued Rails, that was nice!
00:09:24.399 Alright, if there are no questions, then once again, thank you, Sebastian.
Explore all talks recorded at wroclove.rb 2024
+6