Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Presenter & Decorator
In Rails
Decorator
What is Decorator in Rails?
A decorator is a design pattern. Its intent, as
described in Design Patterns by the Gang of Four
is:
> Attach additional r...
Why do we use Decorator?
● Refactoring for SRP (Single Responsibility
Principle)
● Extend behaviors of an object
Try out #1
● I have a coffee class which has #cost method.
Class Coffee
def cost
2
end
end
coffee = Coffee.new()
coffee.co...
How to implement Decorator?
There is a great post by Dan Croak on how to
implement Decorator in rails.
http://robots.thoughtbot.com/post/14825364877/e...
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 b...
Presenter
What is Presenter in Rails?
● A form of Decorator
● A bridge between the model and view in
presentation logic
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.
http://robots.thoughtbot.com/post/13641910...
Don't confuse with helper with
Presenter
● Simple logic goes to helper
● Logic that don't relate to object, goes to helper
● Logic relate to object, goes to Presen...
Q & A
Thanks You
Upcoming SlideShare
Loading in …5
×

Presenter and Decorator in Rails

5,362 views

Published on

Introduction to Decorator and Presenter Design Pattern in Rails

Published in: Education, Technology
  • Be the first to comment

Presenter and Decorator in Rails

  1. 1. Presenter & Decorator In Rails
  2. 2. Decorator
  3. 3. What is Decorator in Rails?
  4. 4. A decorator is a design pattern. Its intent, as described in Design Patterns by the Gang of Four is: > Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
  5. 5. Why do we use Decorator?
  6. 6. ● Refactoring for SRP (Single Responsibility Principle) ● Extend behaviors of an object
  7. 7. Try out #1 ● I have a coffee class which has #cost method. Class Coffee def cost 2 end end coffee = Coffee.new() coffee.cost #=> 2 ● What if I want to add sugar or milk? Assume sugar price is 0.2 and Milk is 0.4
  8. 8. How to implement Decorator?
  9. 9. There is a great post by Dan Croak on how to implement Decorator in rails. http://robots.thoughtbot.com/post/14825364877/e valuating-alternative-decorator-implementations-in
  10. 10. When to use Decorator?
  11. 11. ● 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
  12. 12. Presenter
  13. 13. What is Presenter in Rails?
  14. 14. ● A form of Decorator ● A bridge between the model and view in presentation logic
  15. 15. Why do we use Presenter?
  16. 16. ● To cleanup views by moving aways all the logic ● To keep all presentation logic in one place
  17. 17. How to implement Presenter?
  18. 18. There is a great post by Harold Giménez on how to implement Presenter in rails. http://robots.thoughtbot.com/post/13641910701/ti dy-views-and-beyond-with-decorators
  19. 19. Don't confuse with helper with Presenter
  20. 20. ● 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
  21. 21. Q & A
  22. 22. Thanks You

×