Draper adds an object-oriented layer of presentation logic to your Rails application.
Without Draper, this functionality might have been tangled up in procedural helpers or adding bulk to your models. With Draper decorators, you can wrap your models with presentation-related logic to organise - and test - this layer of your app much more effectively.
Decorator Pattern is a design pattern which allows a developer to add an extra functionality to an object or collection of objects. It is possible to do because a decorator class wraps a decorated model and deals only with presentational issues. Also, it allows moving methods which are not related to a model strictly to separated classes make them cleaner. One of the common application is using it inside views but can be treated like an another layer of abstraction.
Draper is working on a model - view line but the idea of decorator pattern is not limited by any model or view. It is possible to decorate any class whatever you want.
Forwardable
The Forwardable module provides delegation of specified methods to a designated object, using the methods def_delegator and def_delegators. - from the documentation (http://ruby-doc.org/stdlib-2.3.0/libdoc/forwardable/rdoc/Forwardable.html)
IMO the syntax is not clear enough but in some situation has a huge potential and could be used instead of Draper because it is a part of std-lib of ruby and easily accessible.
Decorate an object as well as collection of objects
Draper's methods are not visible in a global scope
Dividing methods into smaller files
The complexity of an app is increasing because we add an extra gem (but IMHO it is worth its price because we receive a cleaner design of an app).
It requires working the activemodel-serializers-XML which was removed from Rails 5
Now (09.05.2016), there is a lot of problems with Rails 5.0-pre
delegate_all or delegate
Add draper gem to Gemfile
Add a decorator class to a given class
[code]
[code]
Use object in order to class an instance of a model which will be decorated by your methods.
Do not forget about decorating your class in a controller by invoking decorate method on an instance of a model. Also, you can do it inside a view but it is not a good approach to a problem.
See example below:
[code]
I have decorated items in a presenter which allows me to do more with a data presentation in future but you can decorate an object in a controller.
I think draper gem is mature and allows a developer to write methods which add extra functionality to its data. Actually, one of the biggest problem of draper gem is that it is not working with Rails 5 correctly and its team has got a problem with a lack of maintainer.
It is a powerful tool, especially when a developer uses it with the presenter pattern. These two things allow a developer to make thinner controllers and models and move a logic related to presenting data in a cleaner way.