SlideShare a Scribd company logo
1 of 36
Download to read offline
Ruby on Rails
                 Best Practices
                                                                             Vladimir Tkach


                            Wondering why Ruby is so popular?
                            Its fans call it a beautiful, artful language.
                            And yet, they say it’s handy and practical.


http://www.ruby-lang.org/




http://rubyonrails.org
How to?




© SAP 2007 / Page 2
Rails Best Practices




© SAP 2007 / Page 3
Concept: What’s good code?




© SAP 2007 / Page 4
Move finder to named_scope / scope




© SAP 2007 / Page 5
Use model association




© SAP 2007 / Page 6
Use scope access




© SAP 2007 / Page 7
Add model virtual attribute




© SAP 2007 / Page 8
Use model callback




© SAP 2007 / Page 9
Use Model Callback




                                       %>




© SAP 2007 / Page 10
Replace Complex Creation
                         with Factory Method




© SAP 2007 / Page 11
Replace Complex Creation
                         with Factory Method




© SAP 2007 / Page 12
Move Model Logic into the Model




© SAP 2007 / Page 13
model.collection_model_ids
                            (many-to-many)




© SAP 2007 / Page 14
model.collection_model_ids
                            (many-to-many)




© SAP 2007 / Page 15
model.collection_model_ids
                            (many-to-many)




© SAP 2007 / Page 16
Nested Model Forms (one-to-one)




© SAP 2007 / Page 17
Nested Model Forms (one-to-one)




© SAP 2007 / Page 18
Nested Model Forms (one-to-many)




© SAP 2007 / Page 19
Keep Finders on Their Own Model




© SAP 2007 / Page 20
Keep Finders on Their Own Model




© SAP 2007 / Page 21
Love named_scope




© SAP 2007 / Page 22
Love named_scope




© SAP 2007 / Page 23
the Law of Demeter




© SAP 2007 / Page 24
DRY: Metaprogramming




© SAP 2007 / Page 25
DRY: Metaprogramming




© SAP 2007 / Page 26
Extract into Module




© SAP 2007 / Page 27
Extract to composed class




© SAP 2007 / Page 28
Extract to composed class




© SAP 2007 / Page 29
Use Observer




© SAP 2007 / Page 30
Use Observer




© SAP 2007 / Page 31
Move code into controller




© SAP 2007 / Page 32
Move code into model




© SAP 2007 / Page 33
Move code into helper




© SAP 2007 / Page 34
Replace instance variable
                          with local variable




© SAP 2007 / Page 35
Thank you!
  §  http://www.ruby-lang.org/en/
  §  http://www.ruby-lang.org/en/documentation/
  §  http://rubyonrails.org/
  §  http://guides.rubyonrails.org/v2.3.8/index.html


  §     http://teamco-anthill.blogspot.com




© SAP 2007 / Page 36

More Related Content

Similar to Ruby on rails. Best practices

Integrating with Adobe Marketing Cloud - Summit 2014
Integrating with Adobe Marketing Cloud - Summit 2014Integrating with Adobe Marketing Cloud - Summit 2014
Integrating with Adobe Marketing Cloud - Summit 2014Paolo Mottadelli
 
Using a trained model on your mobile device
Using a trained model on your mobile deviceUsing a trained model on your mobile device
Using a trained model on your mobile deviceLars Gregori
 
Using a trained model on your mobile device
Using a trained model on your mobile deviceUsing a trained model on your mobile device
Using a trained model on your mobile deviceLars Gregori
 
Sap fundamentals overview_for_sap_minors
Sap fundamentals overview_for_sap_minorsSap fundamentals overview_for_sap_minors
Sap fundamentals overview_for_sap_minorsCenk Ersoy
 
Introduction to ABAP Core Data Services (CDS).pdf
Introduction to ABAP Core Data Services (CDS).pdfIntroduction to ABAP Core Data Services (CDS).pdf
Introduction to ABAP Core Data Services (CDS).pdfAndreaPellati2
 
2017 sitNL Cloud Foundry Masterclass
2017 sitNL Cloud Foundry Masterclass2017 sitNL Cloud Foundry Masterclass
2017 sitNL Cloud Foundry MasterclassTed Castelijns
 
DSAG Tech Days 2018 - DevOps in SAP ABAP Landscapes
DSAG Tech Days 2018 - DevOps in SAP ABAP LandscapesDSAG Tech Days 2018 - DevOps in SAP ABAP Landscapes
DSAG Tech Days 2018 - DevOps in SAP ABAP LandscapesSascha Junkert
 
SAP Cloud Infrastructure Strategy @ Virtualization Week
SAP Cloud Infrastructure Strategy @ Virtualization WeekSAP Cloud Infrastructure Strategy @ Virtualization Week
SAP Cloud Infrastructure Strategy @ Virtualization WeekFrank Stienhans
 
API World 2017 - Building Large Scale Web Apps and Platforms
API World 2017 - Building Large Scale Web Apps and PlatformsAPI World 2017 - Building Large Scale Web Apps and Platforms
API World 2017 - Building Large Scale Web Apps and PlatformsAnurag Sinha
 
Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Mac...
Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Mac...Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Mac...
Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Mac...elliando dias
 
Dissecting and Attacking RMI Frameworks
Dissecting and Attacking RMI FrameworksDissecting and Attacking RMI Frameworks
Dissecting and Attacking RMI FrameworksOnapsis Inc.
 
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The EnterpriseMatt Aimonetti
 
Sap java connector / Hybris RFC
Sap java connector / Hybris RFCSap java connector / Hybris RFC
Sap java connector / Hybris RFCMonsif Elaissoussi
 
Java 9 New Features | Java Tutorial | What’s New in Java 9 | Java 9 Features ...
Java 9 New Features | Java Tutorial | What’s New in Java 9 | Java 9 Features ...Java 9 New Features | Java Tutorial | What’s New in Java 9 | Java 9 Features ...
Java 9 New Features | Java Tutorial | What’s New in Java 9 | Java 9 Features ...Edureka!
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summarydaniel.mattes
 
Djangocon 09 Presentation - Pluggable Applications
Djangocon 09 Presentation - Pluggable ApplicationsDjangocon 09 Presentation - Pluggable Applications
Djangocon 09 Presentation - Pluggable ApplicationsNowell Strite
 
How SAP uses Flowable as its BPMN engine for SAP CP Workflow
How SAP uses Flowable as its BPMN engine for SAP CP WorkflowHow SAP uses Flowable as its BPMN engine for SAP CP Workflow
How SAP uses Flowable as its BPMN engine for SAP CP WorkflowFlowable
 
Accessibility guideline web_dynpro_external_version
Accessibility guideline web_dynpro_external_versionAccessibility guideline web_dynpro_external_version
Accessibility guideline web_dynpro_external_versionadatta12
 
What’s new in SAP Cloud Platform Workflow – summer edition 2019
What’s new in SAP Cloud Platform Workflow – summer edition 2019What’s new in SAP Cloud Platform Workflow – summer edition 2019
What’s new in SAP Cloud Platform Workflow – summer edition 2019SAP Cloud Platform
 

Similar to Ruby on rails. Best practices (20)

Integrating with Adobe Marketing Cloud - Summit 2014
Integrating with Adobe Marketing Cloud - Summit 2014Integrating with Adobe Marketing Cloud - Summit 2014
Integrating with Adobe Marketing Cloud - Summit 2014
 
Using a trained model on your mobile device
Using a trained model on your mobile deviceUsing a trained model on your mobile device
Using a trained model on your mobile device
 
Using a trained model on your mobile device
Using a trained model on your mobile deviceUsing a trained model on your mobile device
Using a trained model on your mobile device
 
Sap fundamentals overview_for_sap_minors
Sap fundamentals overview_for_sap_minorsSap fundamentals overview_for_sap_minors
Sap fundamentals overview_for_sap_minors
 
Introduction to ABAP Core Data Services (CDS).pdf
Introduction to ABAP Core Data Services (CDS).pdfIntroduction to ABAP Core Data Services (CDS).pdf
Introduction to ABAP Core Data Services (CDS).pdf
 
2017 sitNL Cloud Foundry Masterclass
2017 sitNL Cloud Foundry Masterclass2017 sitNL Cloud Foundry Masterclass
2017 sitNL Cloud Foundry Masterclass
 
Cd168 (3)
Cd168 (3)Cd168 (3)
Cd168 (3)
 
DSAG Tech Days 2018 - DevOps in SAP ABAP Landscapes
DSAG Tech Days 2018 - DevOps in SAP ABAP LandscapesDSAG Tech Days 2018 - DevOps in SAP ABAP Landscapes
DSAG Tech Days 2018 - DevOps in SAP ABAP Landscapes
 
SAP Cloud Infrastructure Strategy @ Virtualization Week
SAP Cloud Infrastructure Strategy @ Virtualization WeekSAP Cloud Infrastructure Strategy @ Virtualization Week
SAP Cloud Infrastructure Strategy @ Virtualization Week
 
API World 2017 - Building Large Scale Web Apps and Platforms
API World 2017 - Building Large Scale Web Apps and PlatformsAPI World 2017 - Building Large Scale Web Apps and Platforms
API World 2017 - Building Large Scale Web Apps and Platforms
 
Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Mac...
Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Mac...Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Mac...
Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Mac...
 
Dissecting and Attacking RMI Frameworks
Dissecting and Attacking RMI FrameworksDissecting and Attacking RMI Frameworks
Dissecting and Attacking RMI Frameworks
 
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The Enterprise
 
Sap java connector / Hybris RFC
Sap java connector / Hybris RFCSap java connector / Hybris RFC
Sap java connector / Hybris RFC
 
Java 9 New Features | Java Tutorial | What’s New in Java 9 | Java 9 Features ...
Java 9 New Features | Java Tutorial | What’s New in Java 9 | Java 9 Features ...Java 9 New Features | Java Tutorial | What’s New in Java 9 | Java 9 Features ...
Java 9 New Features | Java Tutorial | What’s New in Java 9 | Java 9 Features ...
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summary
 
Djangocon 09 Presentation - Pluggable Applications
Djangocon 09 Presentation - Pluggable ApplicationsDjangocon 09 Presentation - Pluggable Applications
Djangocon 09 Presentation - Pluggable Applications
 
How SAP uses Flowable as its BPMN engine for SAP CP Workflow
How SAP uses Flowable as its BPMN engine for SAP CP WorkflowHow SAP uses Flowable as its BPMN engine for SAP CP Workflow
How SAP uses Flowable as its BPMN engine for SAP CP Workflow
 
Accessibility guideline web_dynpro_external_version
Accessibility guideline web_dynpro_external_versionAccessibility guideline web_dynpro_external_version
Accessibility guideline web_dynpro_external_version
 
What’s new in SAP Cloud Platform Workflow – summer edition 2019
What’s new in SAP Cloud Platform Workflow – summer edition 2019What’s new in SAP Cloud Platform Workflow – summer edition 2019
What’s new in SAP Cloud Platform Workflow – summer edition 2019
 

Recently uploaded

Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 

Recently uploaded (20)

Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 

Ruby on rails. Best practices