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

Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Dernier (20)

Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View 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