Rails code patterns Decorator/Presenter (Refacotring)

In my previous article i explained when we should refactor code and how to refactor. Lets take our discussion further to decorator/presenter pattern. Decorator/Presenter pattern have been overly used in rails community. There is a thin line between decorator and presenter. I started my efforts to understand the idea behind this. I read few blogs/articles which explained the details. I have gathered my findings here in an effort to help others. I am thankful to the techies who wrote such nice blogs and explained in details.

While building ruby on rails application we put all of the business logic in our models. However, some of the logic may not be directly related to the models or may be required in our views or some controllers. These fat codes clutter the models and put extra burdens. Here comes the decorator pattern to rescue. We can use decorators to extract fat codes from models.

The idea of decorators seem to come from excessive methods being used in our view helpers. Helpers are modules which keeps our views related methods. However helpers are not object oriented. We have to pass the object in the helper methods. Keeping logic in views was never a good idea. For example: calling active record queries in views

A decorator is a class that adds functionality to a specific object by wrapping it, without affecting other instances of that class. Decorator takes object in its constructor. Using decorator we can encapsulate methods intended for use by the views. This way we can keep the models separate but still access the properties.

Sometimes a presenter is a decorator. A presenter is a class that adds presentation functionality to another class object.

We can define decorators using couple of ways, lets use rails SimpleDelegator class:

Note: What we noticed, decorators do not modify the objects, it adds functionality by wrapping the object inside.

Let's take a good example to see in details. Suppose we order a product from some e-commerce website. The buyer have options to take insurance on a product by taking some insurance plan. The product price changes with the insurance plan.

NOTE: In above example we could have used inheritance to create subclasses instead of using multiple decorators. However, there are some downside for using inheritance in this particular example. First, the requirement is much related to presenter/view then ActiveRecord classes. There will be a tight coupling between classes, also any changes in superclass internals will affect subclasses.

1 Comment(s)

Comment on it

AttachFiles,Zip etc

RecordFrom Desktop

RecordFrom Webcam

Preview

Enter captcha to proceed

Unable to start Java!! Mr. Nerd figure out why...

We have detected you are using Google Chrome and might be unable to use the Java plugin from this browser. Starting with Version 42 (released April 2015), Chrome has disabled the standard way in which browsers support plugins. More info

Chrome Version Support

Starting with Chrome version 45, NPAPI is no longer supported for Google Chrome. For more information, see Chrome and NPAPI (blog.chromium.org).

Firefox and Microsoft Internet Explorer are recommended browsers for websites using java applets.

Chrome Version Support

Starting with Chrome version 45, NPAPI is no longer supported for Google Chrome. For more information, see Chrome and NPAPI (blog.chromium.org).

Firefox and Microsoft Internet Explorer are recommended browsers for websites using java applets.

Chrome Version Support

Starting with Chrome version 45, NPAPI is no longer supported for Google Chrome. For more information, see Chrome and NPAPI (blog.chromium.org).

Firefox and Microsoft Internet Explorer are recommended browsers for websites using java applets.

Unable to start Java!! Mr. Nerd figure out why..

We have detected that Java plugin is not installed/enabled on your browser. Unfortunately some of our below listed tools require Java plugin:

Desktop recorder.

Error Screen capture.

Time Tracker.

To use our product please click on the link to install/Detect Java plugin.

You have exceeded the maximum number of characters allowed for a comment without sign in.Either enter Content Or Record

Your Post will be visible in the listing once it has been approved by the administrator.