Presenter and Decorator in Rails

4,903 views
4,576 views

Published on

Introduction to Decorator and Presenter Design Pattern in Rails

Published in: Education, Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,903
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
24
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

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

×