Presenter and Decorator in Rails
Introduction to Decorator and Presenter Design Pattern in Rails
Published on: Mar 4, 2016
Transcripts - Presenter and Decorator in Rails
Presenter & Decorator
What is Decorator in Rails?
A decorator is a design pattern. Its intent, as
described in Design Patterns by the Gang of Four
> Attach additional responsibilities to an object
dynamically. Decorators provide a flexible
alternative to subclassing for extending
Why do we use Decorator?
● Refactoring for SRP (Single Responsibility
● Extend behaviors of an object
Try out #1
● I have a coffee class which has #cost method.
coffee = Coffee.new()
● What if I want to add sugar or milk? Assume sugar
price is 0.2 and Milk is 0.4
How to implement Decorator?
There is a great post by Dan Croak on how to
implement Decorator in rails.
When to use Decorator?
● When an object knows too much.
● When a group of methods describe a possible
meaning of behavior
● When want to extend behavior of an object
● When fully understand Decorator
What is Presenter in Rails?
● A form of Decorator
● A bridge between the model and view in
Why do we use Presenter?
● To cleanup views by moving aways all the logic
● To keep all presentation logic in one place
How to implement Presenter?
There is a great post by Harold Giménez on how
to implement Presenter in rails.
Don't confuse with helper with
● Simple logic goes to helper
● Logic that don't relate to object, goes to helper
● Logic relate to object, goes to Presenter
● Too complex logic can also go to Presenter
Q & A