Table of Contents

Introduction

In recent times, Ruby on rails programming has evolved as a full-fledged web development technology and all credit goes to Ruby developers worldwide for their contribution. Hence, if you are planning to deploy Ruby on Rails developers to create engaging web experiences for your business, you should seek the technical competency of resources of a particular Ruby On Rails Development Company.

The framework of Rails is developed on the principle of MVC (model view controller) at its core. Programmers are blessed with various data structures in this comprehensive Rails library, including strings, figures, date, arrays, hashes. Each construct is in place to resolve various types of problems. However, we will briefly tour two unique concepts in this text; decorator vs presenter.

Even though both these patterns look the same on the surface, they have minor differences under the hood. If you used to be confused about recognizing between decorator and presenter, or are still in the block, trust me, it is normal due to the homogeneous genre observed in both gems.

Tutorial Goal: Decorator vs Presenter

  • What are the Decorator and Presenter?
  • Difference between Decorator and Presenter
  • Decorator
    ● Problem Statement
    ● Solution
    ● When to use it
    ● Example
  • Presenter
    ● Problem Statement
    ● Solution
    ● When to use it
    ● Example

Decorator vs Presenter: Main Difference

The main differences between the decorator and presenter are as follows:
1. The decorator is more about adding additional functionality to the entity whereas the presenter makes connections between the model/backend and view.
2. The decorators are applicable in all areas and the presenters are always connected with view-like functionality.

Let’s discuss them in more detail. So, now we will dive deeper into both the concepts individually and see their implementation as well.

What is a Decorator?

If you don’t know what is a decorator here’s a brief about it.

According to the OOPS concepts, the decorator is a design pattern that lets the functions add a single object dynamically without affecting the functions of other objects. It basically wraps the new functionality with extra objects consisting of new characteristics and adds them to an existing object. This doesn’t demand the definition of new static sub-classes in the code.

Let’s understand more using a problem statement and its solution in which we will implement decorator.

How to Implement a Decorator?

Now, let’s see how we can implement a decorator. In this section of the Decorator vs Presenter tutorial, we learn the following topics:

  • Problem Statement
  • Solution
  • When to use the decorator
  • Example

Problem Statement

Consider below problem statement.
1. You want to build a motorcycle using programming and you need not go into its complexity to create extensive builders for all types of possible motorcycles.
2. You can start by creating an initial motorcycle class with the most fundamental features and characteristics.
3. Now, if you need to modify the bike tyres to be tubeless then you are required to extend the base class to a sub-class and add the tubeless tyres attribute. Extend the base class if required.

Solution

We will use decorators to solve this problem. It will provide a set of interfaces that would take in a base class object and later return an object’s decorated version (wrapping it around with the requested feature)

Because of decorators’ high modularity, the base class will remain clean and all the requirements will be taken care of.

When to Apply a Decorator?

You can implement decorators in the below cases:

  • To change existing objects of the class rather than the class itself.
  • To add new features independently without following a fixed sequence to change the object.

Decorator Pattern Example: How to Implement Decorators?

Here’s how you can generate a decorator for the above-mentioned problem:

Let’s create a class

Copy Text
class MotorCycle
    def tyres
        "tabular"
    end
end

Create an app/decorators folder then add convertible_decorator.rb under the decorators folder.

Copy Text
class ConvertibleDecorator < SimpleDelegator
    def tyres
        "tubeless"
    end

    def self.decorate(motor_cycle)
        new(motor_cycle)
    end

    private
    def model
         __getobj__
    end
end

And this is how you can use it.

Copy Text
basicMotorCycle1 = MotorCycle.new()
basicMotorCycle2 = MotorCycle.new()

basicMotorCycle =  ConvertibleDecorator.decorate(basicMotorCycle1)
puts(basicMotorCycle1.tyres) #=> tubeless
puts(basicMotorCycle2.tyres) #=> tabular

What is a Presenter?

In our Decorator vs Presenter tutorial, let’s see about the presenter now. So, what is a presenter?

The presenter is a design pattern that targets isolating view-rendering logic from the model and controller. It further encapsulates them into reusable code. Basically, it enhances the code readability.

Let’s understand more using one problem and its solution.

Still wondering why Ruby is the best programming language for your enterprise?
Hire Ruby on Rails developers from us and build the next transformational web application for the world!

How to Implement a Presenter?

Moving ahead in the Decorator vs Presenter blog, let’s see how to implement presenter. As we did with decorators, we will learn about the following topics:

  • Problem Statement
  • Solution
  • When to use a presenter?
  • Presenter Example

Problem Statement

1. At the time of programming without proper planning developers tend to overload their views with rendering logic such as cleaning or formatting the data received from external APIs.
2. However, it looks good in the case of smaller applications and not for growing applications.
3. Because of it, the controller and models look complicated with extra rendering logic, and code maintenance becomes complex.

Solution

The resolution of the above problem is to create separate modules or classes called Presenters which will help to render logic from the classes, models, and HTML views. It will directly display the view having final information after data formation and validation.

When To Apply a Presenter?

The Presenter pattern is helpful for use in the following cases:

  • When trying to make a large application with more than one controller and model.
  • When you need to use the same model rendering the same logic.

Presenter Example: How to Implement Presenters?

Let’s understand using examples with and without using presenter patterns.

Copy Text
<% # /views/employee/show.html.erb %>
<% content_for :header do %>
 <title>
     <%= @employee.name %>
 </title>
<% end %>
<% if @employee.role == “manager” %>
 <%= @employee.role %>
<% end %>

<h1>  <%= @employee.contact %> </h1>

In the above code, as you can see, the view appears very messy since the conditional validation and other checks have been executed straightforwardly in the view.

If you used a presenter to render the views it looks like as mentioned below :

Copy Text
<%# /views/posts/show.html.erb %>
<% presenter @employee do |employee_presenter| %>
 <% content_for :header do %>
   <%= employee_presenter.name %>
 <% end %>
 <%= employee_presenter.role%>
 <h1> <%= employee_presenter.contact %> </h1>
 <% end %>

Postpresenter class looks like as below:

Copy Text
class EmployeePresenter
  def initialize(employee)
    @employee = employee
  end

  def role
    @employee.role == “manager” ? @employee.role : nil
  end
end

Conclusion

So, this was all about the Decorator vs Presenter tutorial. We hope it was enough to get you started with their implementation. If you have any queries or feedback, please feel free to connect with us. For more such ROR tutorials, visit our Ruby on Rails tutorials page to learn more.

Outsource Team of Dedicated Ruby on rails Developers

  • Result-Driven Approach
  • Integrity & Transparency
  • Profound Technical Knowledge

BOOK A 30 MIN CALL

Build Your Agile Team

Hire Skilled Developer From Us

[email protected]

Your Success Is Guaranteed !

We accelerate the release of digital product and guaranteed their success

We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.

How Can We Help You?