SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

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
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
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
 
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
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
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
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
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
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Kürzlich hochgeladen (20)

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
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
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
 
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
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
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
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
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
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

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