patterns in rails
DESCRIPTION
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 BuildingTRANSCRIPT
![Page 1: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/1.jpg)
Patterns in Rails
Cory Foy@cory_foy
Friday, June 21, 13
![Page 2: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/2.jpg)
Patterns
Friday, June 21, 13
![Page 3: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/3.jpg)
Friday, June 21, 13
![Page 4: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/4.jpg)
“The specific patterns out of which a building or a town is made may be alive or dead. To the extent they are
alive, they let our inner forces loose, and set us free; but when they are dead, they keep us locked in inner
conflict.”Christopher Alexander - “The Timeless Way of Building”
Friday, June 21, 13
![Page 5: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/5.jpg)
“And it turns out...behind all processes which allow us to make buildings live, there is a single common process.
But though this method is precise, it cannot be used mechanically.
Indeed it turns out, in the end, that what this method does is simply free us from all method.”
Christopher Alexander - “The Timeless Way of Building”
Friday, June 21, 13
![Page 6: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/6.jpg)
“...we have so far beset ourselves with rules, and concepts, and ideas of what must be done to make a
building or a town alive, that we have become afraid of what will happen naturally, and convinced that we must
work within a “system” and with “methods” since without them our surroundings will come tumbling down
in chaos.”Christopher Alexander - “The Timeless Way of Building”
Friday, June 21, 13
![Page 7: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/7.jpg)
Naming something the name of a pattern does not make it that pattern
Friday, June 21, 13
![Page 8: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/8.jpg)
Friday, June 21, 13
![Page 9: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/9.jpg)
Pattern Name
Intent
Motivation
Consequences
Implementation
Elements of a pattern
Friday, June 21, 13
![Page 10: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/10.jpg)
Rails
Friday, June 21, 13
![Page 11: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/11.jpg)
Friday, June 21, 13
![Page 12: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/12.jpg)
“From the beginning, the Rails framework turned web development on its head with the insight that the vast majority of time
spent on projects amounted to meaningless sit-ups.
Instead of having the time to think your domain-specific code, you’d spend the first
few weeks of a project deciding meaningless details.”
- Yahuda Katz, The Rails 3 Way
Friday, June 21, 13
![Page 13: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/13.jpg)
“[Rails is] not a blank slate equally tolerant of every kind of expression. On the
contrary, it trades that flexibility for the convenience of ‘what most people need
most of the time to do most things’”
- David Heinmeier Hansson - The Rails 3 Way
Friday, June 21, 13
![Page 14: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/14.jpg)
Not to be confused with Ruby (programming language).
- Wikipedia
Friday, June 21, 13
![Page 15: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/15.jpg)
Ruby on Rails, often simply Rails, is an open source web application framework which
runs on the Ruby programming language. It is a full-stack framework: it allows creating
pages and applications that gather information from the web server, talk to or query the database, and render templates out of the box. As a result, Rails features a routing system that is independent of the
web server.
Friday, June 21, 13
![Page 16: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/16.jpg)
Ruby on Rails emphasizes the use of well-known software engineering patterns and principles, such as active record pattern,
convention over configuration (CoC), don't repeat yourself (DRY), and model-view-
controller (MVC).
Friday, June 21, 13
![Page 17: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/17.jpg)
RESTRouting
ValidationsControllersAction View
Action MailerActive RecordAuthentication
Session ManagementBackground Processing
Caching and PerformanceExtending Rails with Plugins
Friday, June 21, 13
![Page 18: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/18.jpg)
Model-View Controller
Active Record
Template View
Front Controller
Single Table Inheritance
Friday, June 21, 13
![Page 19: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/19.jpg)
Friday, June 21, 13
![Page 20: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/20.jpg)
Pattern Name
Intent
Motivation
Consequences
Implementation
Elements of a pattern
Friday, June 21, 13
![Page 21: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/21.jpg)
Name Model-View Controller
IntentSeparate the presentation of the model from the view, and separate the wiring
of the model to the view
MotivationA web application which has many
models that need to be interacted with by end users
ImplementationView, Model and Controller should be in separate classes. View should depend
on model, but not vice versa
ConsequencesMay be overkill for simple systems
where the model has very little behavior
Friday, June 21, 13
![Page 22: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/22.jpg)
Friday, June 21, 13
![Page 23: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/23.jpg)
Name Front Controller
IntentConsolidate request handling by
channeling requests through a single handler object
MotivationCentralize the logic and handling for the many things that need to happen for a
complex web site
ImplementationCreate a class to handle all calls for a web site, and determines the correct
classes to dispatch the request to
ConsequencesMore complicated than a Page
Controller. Can turn into a God class
Friday, June 21, 13
![Page 24: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/24.jpg)
/rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13
![Page 25: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/25.jpg)
Name Mapper
IntentSet up communications between two subsystems that need to stay ignorant
of each other
MotivationAllow two independent systems to be able to talk to each other without them being dependent on each other or the mapper
ImplementationInitialize the mapper (maybe using a Front Controller) and configure the communication between systems
ConsequencesMay be difficult to initialize and configure the mapper since the subsystems can’t know about it
Friday, June 21, 13
![Page 26: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/26.jpg)
/rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13
![Page 27: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/27.jpg)
Name Active Record
IntentAn object that wraps a row in a
database table, encapsulates the access, and adds domain logic on that data
MotivationSimplify the need to persist data by putting the data access logic in the
domain object
ImplementationEach class is responsible for saving and
loading to the database, and for any domain logic that acts on the data
ConsequencesBest when domain logic isn’t too complex,
and when model corresponds directly to the database table. Couples system to DB
Friday, June 21, 13
![Page 28: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/28.jpg)
Friday, June 21, 13
![Page 29: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/29.jpg)
Friday, June 21, 13
![Page 30: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/30.jpg)
Friday, June 21, 13
![Page 31: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/31.jpg)
Name Single Table Inheritance
IntentRepresent an inheritance hierarchy of classes
as a single table that has columns for all of the fields of the classes
MotivationMinimize the number of joins when
representing an inheritance structure in a relational database
ImplementationA single table contains all of the fields for the subclasses. Classes may or may
not use all of the fields.
ConsequencesSince not all columns are used, may be confusing. May cause large tables. Only
have a single namespace for fields.
Friday, June 21, 13
![Page 32: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/32.jpg)
Friday, June 21, 13
![Page 33: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/33.jpg)
Name Template View
IntentRender information into HTML by
embedding markers in an HTML page
MotivationAllow HTML pages to be created as if
they were static, but still be able to insert dynamic data
ImplementationEmbed placeholders into a static HTML
page when it’s written. Replace placeholders with content at runtime.
ConsequencesEasy to put complicated logic in the view. Harder to test since generally
designed to work within a web server
Friday, June 21, 13
![Page 34: Patterns in Rails](https://reader033.vdocuments.us/reader033/viewer/2022060110/555c431fd8b42a2c068b4ee2/html5/thumbnails/34.jpg)
Friday, June 21, 13