• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Patterns in Rails
 

Patterns in Rails

on

  • 682 views

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

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

Statistics

Views

Total Views
682
Views on SlideShare
681
Embed Views
1

Actions

Likes
1
Downloads
3
Comments
0

1 Embed 1

https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Patterns in Rails Patterns in Rails Presentation Transcript

    • Patterns in RailsCory Foy@cory_foycory@8thlight.comwww.8thlight.comFriday, June 21, 13
    • PatternsFriday, June 21, 13
    • Friday, June 21, 13
    • “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
    • “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
    • “...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
    • Naming something the name of a pattern doesnot make it that patternFriday, June 21, 13
    • Friday, June 21, 13
    • Pattern NameIntentMotivationConsequencesImplementationElements of a patternFriday, June 21, 13
    • RailsFriday, June 21, 13
    • Friday, June 21, 13
    • “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
    • “[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
    • Not to be confused with Ruby(programming language).- WikipediaFriday, June 21, 13
    • 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
    • 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
    • RESTRoutingValidationsControllersAction ViewAction MailerActive RecordAuthenticationSession ManagementBackground ProcessingCaching and PerformanceExtending Rails with PluginsFriday, June 21, 13
    • Model-View ControllerActive RecordTemplate ViewFront ControllerSingle Table InheritanceFriday, June 21, 13
    • Friday, June 21, 13
    • Pattern NameIntentMotivationConsequencesImplementationElements of a patternFriday, June 21, 13
    • 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
    • Friday, June 21, 13
    • 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
    • /rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13
    • 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
    • /rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13
    • 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
    • Friday, June 21, 13
    • Friday, June 21, 13
    • Friday, June 21, 13
    • 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
    • Friday, June 21, 13
    • 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
    • Friday, June 21, 13
    • Software is our craft TM8th LightCory Foy (@cory_foy)cory@8thlight.comwww.8thlight.comFriday, June 21, 13