DEV Community

Cover image for Decorator Dryer: Keeping Your Ruby On Rails Decorators DRY
Mark Harbison
Mark Harbison

Posted on

Decorator Dryer: Keeping Your Ruby On Rails Decorators DRY

As Ruby on Rails developers, we often find ourselves striving for clean, concise, and maintainable code. The "Don't Repeat Yourself" (DRY) principle is at the core of our coding ethos, yet when it comes to decorator classes, we sometimes encounter the inevitable build-up of repetitive code. This contradiction can make maintaining and updating decorators a challenging task.

Decorators play a crucial role in Rails applications by encapsulating presentation logic, but as our codebase grows, so does the list of attributes that we need our decorators to transform. In our pursuit of cleaner code, leveraging tools like Draper has been a game-changer. Draper simplifies decorator implementation and enhances the maintainability of our code. However, even with Draper, repetitive patterns can emerge within decorators, compromising the DRY principle we hold so dear.

Enter Decorator Dryer, an outstanding companion gem for Draper that addresses these challenges head-on. This gem streamlines the decorator writing process, keeping them DRY and easy to manage, thereby enhancing the efficiency of our Rails applications.

The Problem In More Detail

Take dates as an example. Formatting dates is a very easy thing for a decorator to do. Just choose a date attribute, choose a format, and make a call to strftime. But what if your decorator needs to do this for 10 date attributes? You now have 10 almost identical methods in your decorator.
Even if you centralise your formatting logic into a reusable method, you still have to write 10 methods that call that method, each passing in a different attribute.

With Decorator Dryer, this is a thing of the past.

Why Choose Decorator Dryer?

Seamless Integration with Draper

Decorator Dryer seamlessly integrates with Draper, enhancing its capabilities by reducing redundancy in decorator classes. By using Decorator Dryer alongside Draper, you ensure a unified and efficient approach to managing your decorators.

Code Reduction and Improved Maintainability

One of the key benefits of Decorator Dryer is its ability to significantly reduce repetitive code within decorators. It extracts common decorator functionalities into reusable methods and leverages the power of meta-programming to eliminate the need for repetitive calls to those methods, leading to cleaner and more maintainable codebases.

Faster Development of Decorators

By eliminating redundant code and providing a structured way to organise shared functionalities, Decorator Dryer accelerates the development process. Developers can focus more on the unique aspects of their decorators, leading to faster iteration and deployment.

Getting Started with Decorator Dryer

Implementing Decorator Dryer into your Rails application is a straightforward process:


1. Installation:

Add Decorator Dryer to your application's Gemfile.

gem 'draper'
gem 'decorator_dryer'
And the execute:
bundle install
Enter fullscreen mode Exit fullscreen mode

2. Inclusion:

Add Decorator Dryer to your top-level Draper decorator.

class ApplicationDecorator < Draper::Decorator
  include DecoratorDryer
end
Enter fullscreen mode Exit fullscreen mode

3. Implementation:

class PersonDecorator < ApplicationDecorator
  to_date_format :date_of_birth, :date_of_graduation
  to_datetime_format :moment_of_birth
  to_time_format :lunch_time, :dinner_time
  to_precision_number :salary, :coffee_budget, precision: 2
  to_precision_number :height_in_meters, precision: 3
  to_attachment :profile_picture
end
Enter fullscreen mode Exit fullscreen mode

4. Decorate your models:

date_of_birth = Date.new(2000, 1, 1)
date_of_graduation = Date.new(2018, 6, 6)
person = Person.create(date_of_birth: date_of_birth, date_of_graduation: date_of_graduation)
person.decorate.date_of_birth # ==> '2000-01-01'
person.decorate.date_of_graduation # ==> '2018-06-06'
Enter fullscreen mode Exit fullscreen mode

Conclusion

Decorators are an essential part of Ruby On Rails applications, but they have a tendency to become overly repetitive, especially in larger application. For best results, keep the DRY.

Decorator Dryer stands as a valuable tool in the arsenal of any Ruby on Rails developer seeking cleaner, more maintainable code. By working seamlessly together with Draper, reducing code repetition, and expediting development, it's a gem that truly lives up to its name.

So, let's keep our decorators DRY and our codebases clean!

Check it out on Github: https://github.com/CodeTectonics/decorator_dryer.

Happy coding!

Top comments (0)