35
Patterns in Rails Cory Foy @cory_foy [email protected] www.8thlight.com Friday, June 21, 13

Patterns in Rails

Embed Size (px)

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 Building

Citation preview

Page 2: Patterns in Rails

Patterns

Friday, June 21, 13

Page 3: Patterns in Rails

Friday, June 21, 13

Page 4: Patterns in Rails

“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

“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

“...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

Naming something the name of a pattern does not make it that pattern

Friday, June 21, 13

Page 8: Patterns in Rails

Friday, June 21, 13

Page 9: Patterns in Rails

Pattern Name

Intent

Motivation

Consequences

Implementation

Elements of a pattern

Friday, June 21, 13

Page 10: Patterns in Rails

Rails

Friday, June 21, 13

Page 11: Patterns in Rails

Friday, June 21, 13

Page 12: Patterns in Rails

“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

“[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 15: Patterns in Rails

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 17: Patterns in Rails

RESTRouting

ValidationsControllersAction View

Action MailerActive RecordAuthentication

Session ManagementBackground Processing

Caching and PerformanceExtending Rails with Plugins

Friday, June 21, 13

Page 18: Patterns in Rails

Model-View Controller

Active Record

Template View

Front Controller

Single Table Inheritance

Friday, June 21, 13

Page 19: Patterns in Rails

Friday, June 21, 13

Page 20: Patterns in Rails

Pattern Name

Intent

Motivation

Consequences

Implementation

Elements of a pattern

Friday, June 21, 13

Page 21: Patterns in Rails

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

Friday, June 21, 13

Page 23: Patterns in Rails

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

/rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13

Page 25: Patterns in Rails

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

/rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13

Page 27: Patterns in Rails

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

Friday, June 21, 13

Page 29: Patterns in Rails

Friday, June 21, 13

Page 30: Patterns in Rails

Friday, June 21, 13

Page 31: Patterns in Rails

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

Friday, June 21, 13

Page 33: Patterns in Rails

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

Friday, June 21, 13

Page 35: Patterns in Rails

Software is our craft TM8th Light

Cory Foy (@cory_foy)[email protected]

Friday, June 21, 13