SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Dernier

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 

Dernier (20)

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 

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