Patterns in Rails


Published on

In this talk from a Tampa 8th Light University, Senior Craftsman Cory Foy details the design patterns used in Rails, and shows their use and implementation while reference Fowler's PoEAA and Alexander's Timeless Way of Building

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Patterns in Rails

  1. 1. Patterns in RailsCory Foy@cory_foycory@8thlight.comwww.8thlight.comFriday, June 21, 13
  2. 2. PatternsFriday, June 21, 13
  3. 3. Friday, June 21, 13
  4. 4. “The specific patterns out of which a building or a townis made may be alive or dead. To the extent they arealive, they let our inner forces loose, and set us free; butwhen they are dead, they keep us locked in innerconflict.”Christopher Alexander - “The Timeless Way of Building”Friday, June 21, 13
  5. 5. “And it turns out...behind all processes which allow us tomake buildings live, there is a single common process.But though this method is precise, it cannot be usedmechanically.Indeed it turns out, in the end, that what this methoddoes is simply free us from all method.”Christopher Alexander - “The Timeless Way of Building”Friday, June 21, 13
  6. 6. “...we have so far beset ourselves with rules, andconcepts, and ideas of what must be done to make abuilding or a town alive, that we have become afraid ofwhat will happen naturally, and convinced that we mustwork within a “system” and with “methods” sincewithout them our surroundings will come tumbling downin chaos.”Christopher Alexander - “The Timeless Way of Building”Friday, June 21, 13
  7. 7. Naming something the name of a pattern doesnot make it that patternFriday, June 21, 13
  8. 8. Friday, June 21, 13
  9. 9. Pattern NameIntentMotivationConsequencesImplementationElements of a patternFriday, June 21, 13
  10. 10. RailsFriday, June 21, 13
  11. 11. Friday, June 21, 13
  12. 12. “From the beginning, the Rails frameworkturned web development on its head withthe insight that the vast majority of timespent on projects amounted to meaninglesssit-ups.Instead of having the time to think yourdomain-specific code, you’d spend the firstfew weeks of a project deciding meaninglessdetails.”- Yahuda Katz, The Rails 3 WayFriday, June 21, 13
  13. 13. “[Rails is] not a blank slate equally tolerantof every kind of expression. On thecontrary, it trades that flexibility for theconvenience of ‘what most people needmost of the time to do most things’”- David Heinmeier Hansson -The Rails 3 WayFriday, June 21, 13
  14. 14. Not to be confused with Ruby(programming language).- WikipediaFriday, June 21, 13
  15. 15. Ruby on Rails, often simply Rails, is an opensource web application framework whichruns on the Ruby programming language. Itis a full-stack framework: it allows creatingpages and applications that gatherinformation from the web server, talk to orquery the database, and render templatesout of the box. As a result, Rails features arouting system that is independent of theweb server.Friday, June 21, 13
  16. 16. Ruby on Rails emphasizes the use of well-known software engineering patterns andprinciples, such as active record pattern,convention over configuration (CoC), dontrepeat yourself (DRY), and model-view-controller (MVC).Friday, June 21, 13
  17. 17. RESTRoutingValidationsControllersAction ViewAction MailerActive RecordAuthenticationSession ManagementBackground ProcessingCaching and PerformanceExtending Rails with PluginsFriday, June 21, 13
  18. 18. Model-View ControllerActive RecordTemplate ViewFront ControllerSingle Table InheritanceFriday, June 21, 13
  19. 19. Friday, June 21, 13
  20. 20. Pattern NameIntentMotivationConsequencesImplementationElements of a patternFriday, June 21, 13
  21. 21. Name Model-View ControllerIntentSeparate the presentation of the modelfrom the view, and separate the wiringof the model to the viewMotivationA web application which has manymodels that need to be interacted withby end usersImplementationView, Model and Controller should bein separate classes.View should dependon model, but not vice versaConsequencesMay be overkill for simple systemswhere the model has very littlebehaviorFriday, June 21, 13
  22. 22. Friday, June 21, 13
  23. 23. Name Front ControllerIntentConsolidate request handling bychanneling requests through a singlehandler objectMotivationCentralize the logic and handling for themany things that need to happen for acomplex web siteImplementationCreate a class to handle all calls for aweb site, and determines the correctclasses to dispatch the request toConsequencesMore complicated than a PageController. Can turn into a God classFriday, June 21, 13
  24. 24. /rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13
  25. 25. Name MapperIntentSet up communications between twosubsystems that need to stay ignorantof each otherMotivationAllow two independent systems to be ableto talk to each other without them beingdependent on each other or the mapperImplementationInitialize the mapper (maybe using aFront Controller) and configure thecommunication between systemsConsequencesMay be difficult to initialize andconfigure the mapper since thesubsystems can’t know about itFriday, June 21, 13
  26. 26. /rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13
  27. 27. Name Active RecordIntentAn object that wraps a row in adatabase table, encapsulates the access,and adds domain logic on that dataMotivationSimplify the need to persist data byputting the data access logic in thedomain objectImplementationEach class is responsible for saving andloading to the database, and for anydomain logic that acts on the dataConsequencesBest when domain logic isn’t too complex,and when model corresponds directly to thedatabase table. Couples system to DBFriday, June 21, 13
  28. 28. Friday, June 21, 13
  29. 29. Friday, June 21, 13
  30. 30. Friday, June 21, 13
  31. 31. Name Single Table InheritanceIntentRepresent an inheritance hierarchy of classesas a single table that has columns for all ofthe fields of the classesMotivationMinimize the number of joins whenrepresenting an inheritance structure ina relational databaseImplementationA single table contains all of the fieldsfor the subclasses. Classes may or maynot use all of the fields.ConsequencesSince not all columns are used, may beconfusing. May cause large tables. Onlyhave a single namespace for fields.Friday, June 21, 13
  32. 32. Friday, June 21, 13
  33. 33. Name TemplateViewIntentRender information into HTML byembedding markers in an HTML pageMotivationAllow HTML pages to be created as ifthey were static, but still be able toinsert dynamic dataImplementationEmbed placeholders into a static HTMLpage when it’s written. Replaceplaceholders with content at runtime.ConsequencesEasy to put complicated logic in theview. Harder to test since generallydesigned to work within a web serverFriday, June 21, 13
  34. 34. Friday, June 21, 13
  35. 35. Software is our craft TM8th LightCory Foy (@cory_foy)cory@8thlight.comwww.8thlight.comFriday, June 21, 13