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.
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.
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.
Now, let’s see how we can implement a decorator. In this section of the Decorator vs Presenter tutorial, we learn the following topics:
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.
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.
You can implement decorators in the below cases:
Here’s how you can generate a decorator for the above-mentioned problem:
Let’s create a class
Create an app/decorators folder then add convertible_decorator.rb under the decorators folder.
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.
basicMotorCycle1 = MotorCycle.new() basicMotorCycle2 = MotorCycle.new() basicMotorCycle = ConvertibleDecorator.decorate(basicMotorCycle1) puts(basicMotorCycle1.tyres) #=> tubeless puts(basicMotorCycle2.tyres) #=> tabular
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!
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:
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.
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.
The Presenter pattern is helpful for use in the following cases:
Let’s understand using examples with and without using presenter patterns.
<% # /views/employee/show.html.erb %> <% content_for :header do %> <%= @employee.name %> <% end %> <% if @employee.role == “manager” %> <%= @employee.role %> <% end %><%= @employee.contact %>
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 :
<%# /views/posts/show.html.erb %> <% presenter @employee do |employee_presenter| %> <% content_for :header do %> <%= employee_presenter.name %> <% end %> <%= employee_presenter.role%> <%= employee_presenter.contact %>
<% end %>
Postpresenter class looks like as below:
class EmployeePresenter def initialize(employee) @employee = employee end def role @employee.role == “manager” ? @employee.role : nil end end
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.
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.