SlideShare a Scribd company logo
1 of 22
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 responsibilities to an object
dynamically. Decorators provide a flexible
alternative to subclassing for extending
functionality.
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.cost
#=> 2
● 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.
http://robots.thoughtbot.com/post/14825364877/e
valuating-alternative-decorator-implementations-in
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
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/13641910701/ti
dy-views-and-beyond-with-decorators
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 Presenter
● Too complex logic can also go to Presenter
Q & A
Thanks You

More Related Content

Similar to Presenter and Decorator in Rails

java training in chennai
java training in chennaijava training in chennai
java training in chennaisivasundari6
 
Drupal 8 Sitebuilding with Paragraphs, Display Suite & Config Management - Dr...
Drupal 8 Sitebuilding with Paragraphs, Display Suite & Config Management - Dr...Drupal 8 Sitebuilding with Paragraphs, Display Suite & Config Management - Dr...
Drupal 8 Sitebuilding with Paragraphs, Display Suite & Config Management - Dr...Chandeep Singh Khosa
 
Introduction to Agile and Scrum.pptx
Introduction to Agile and Scrum.pptxIntroduction to Agile and Scrum.pptx
Introduction to Agile and Scrum.pptxAmira Elsayed Ismail
 
Bootstrap + Drupal Commerce in less than 45 minutes!
Bootstrap + Drupal Commerce in less than 45 minutes!Bootstrap + Drupal Commerce in less than 45 minutes!
Bootstrap + Drupal Commerce in less than 45 minutes!Jorge Diaz
 
What two courses of action are available to a designer when encounteri.docx
What two courses of action are available to a designer when encounteri.docxWhat two courses of action are available to a designer when encounteri.docx
What two courses of action are available to a designer when encounteri.docxSUKHI5
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - RefactoringDiaa Al-Salehi
 
Data weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsData weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsManjuKumara GH
 
2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation FundamentalsMichael Heron
 
Exhibits and Presenters
Exhibits and PresentersExhibits and Presenters
Exhibits and Presentersatifzia14
 
Ntd2015_pt_kanban_ppt
Ntd2015_pt_kanban_pptNtd2015_pt_kanban_ppt
Ntd2015_pt_kanban_pptJokin Aspiazu
 
Container Driven Continuous Delivery
Container Driven Continuous DeliveryContainer Driven Continuous Delivery
Container Driven Continuous DeliveryAndrew Block
 
Agile in a Nutshell
Agile in a NutshellAgile in a Nutshell
Agile in a NutshellPortia Tung
 
Mural: Build Experiences Not Features
Mural: Build Experiences Not FeaturesMural: Build Experiences Not Features
Mural: Build Experiences Not FeaturesJohn Murray
 
Introduction to Python decorators
Introduction to Python decoratorsIntroduction to Python decorators
Introduction to Python decoratorsrikbyte
 
Rails Presenters
Rails PresentersRails Presenters
Rails PresentersTom Ridge
 
Ecommerce as an Engine
Ecommerce as an EngineEcommerce as an Engine
Ecommerce as an Enginestephskardal
 

Similar to Presenter and Decorator in Rails (20)

java training in chennai
java training in chennaijava training in chennai
java training in chennai
 
Drupal 8 Sitebuilding with Paragraphs, Display Suite & Config Management - Dr...
Drupal 8 Sitebuilding with Paragraphs, Display Suite & Config Management - Dr...Drupal 8 Sitebuilding with Paragraphs, Display Suite & Config Management - Dr...
Drupal 8 Sitebuilding with Paragraphs, Display Suite & Config Management - Dr...
 
Introduction to OOP in Python
Introduction to OOP in PythonIntroduction to OOP in Python
Introduction to OOP in Python
 
Introduction to Agile and Scrum.pptx
Introduction to Agile and Scrum.pptxIntroduction to Agile and Scrum.pptx
Introduction to Agile and Scrum.pptx
 
Bootstrap + Drupal Commerce in less than 45 minutes!
Bootstrap + Drupal Commerce in less than 45 minutes!Bootstrap + Drupal Commerce in less than 45 minutes!
Bootstrap + Drupal Commerce in less than 45 minutes!
 
What two courses of action are available to a designer when encounteri.docx
What two courses of action are available to a designer when encounteri.docxWhat two courses of action are available to a designer when encounteri.docx
What two courses of action are available to a designer when encounteri.docx
 
Rails best practices
Rails best practicesRails best practices
Rails best practices
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
 
Data weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsData weave 2.0 language fundamentals
Data weave 2.0 language fundamentals
 
Agile ncr ppt
Agile ncr pptAgile ncr ppt
Agile ncr ppt
 
2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals2CPP03 - Object Orientation Fundamentals
2CPP03 - Object Orientation Fundamentals
 
Exhibits and Presenters
Exhibits and PresentersExhibits and Presenters
Exhibits and Presenters
 
Ntd2015_pt_kanban_ppt
Ntd2015_pt_kanban_pptNtd2015_pt_kanban_ppt
Ntd2015_pt_kanban_ppt
 
Container Driven Continuous Delivery
Container Driven Continuous DeliveryContainer Driven Continuous Delivery
Container Driven Continuous Delivery
 
Agile in a Nutshell
Agile in a NutshellAgile in a Nutshell
Agile in a Nutshell
 
Mural: Build Experiences Not Features
Mural: Build Experiences Not FeaturesMural: Build Experiences Not Features
Mural: Build Experiences Not Features
 
Introduction to Python decorators
Introduction to Python decoratorsIntroduction to Python decorators
Introduction to Python decorators
 
Resume
ResumeResume
Resume
 
Rails Presenters
Rails PresentersRails Presenters
Rails Presenters
 
Ecommerce as an Engine
Ecommerce as an EngineEcommerce as an Engine
Ecommerce as an Engine
 

Recently uploaded

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 

Recently uploaded (20)

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

Presenter and Decorator in Rails

  • 3. What is Decorator in Rails?
  • 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. Why do we use Decorator?
  • 6. ● Refactoring for SRP (Single Responsibility Principle) ● Extend behaviors of an object
  • 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. How to implement Decorator?
  • 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. When to use Decorator?
  • 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
  • 13. What is Presenter in Rails?
  • 14. ● A form of Decorator ● A bridge between the model and view in presentation logic
  • 15. Why do we use Presenter?
  • 16. ● To cleanup views by moving aways all the logic ● To keep all presentation logic in one place
  • 17. How to implement Presenter?
  • 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. Don't confuse with helper with Presenter
  • 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. Q & A