Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 71 lines (49 sloc) 2.628 kb
5641a2b Ben Oakes RailsConf 2011 notes, verbatim from back in... May 2011 (how time flies)
benjaminoakes authored
1 Rails 3 Refactoring
2 ===================
3
4 Robert C. Martin (Agile Software Devlopment): SOLID
5
6 S: Single Resp Prin.
7 --------------------
8
9 * Rails 0.x: ActionView::Base had too many responsibilities (details, templates, context, etc)
10 * Rails 1.0: Reduce responsibilities, separate classes out, (Template handlers (builder, rjs, haml, erb): single responsibility)
11 * Rails 2.2: view paths extracted (multiple places for view templates, allows Rails Engines)
12 * Rails 3.0: View paths split into 'view paths' and 'resolvers'
13 * Resolvers make it so views don't have to come from the filesystem (could be web service, database)
14 * Added lookup context too, so less back and forth between view and controller
15 * Rails 3.1: AV::Renderer in from of ActionView -- only single responsibility per class
16 * Allows you to use something other than ActionView (e.g. Merb's views)
17 * Allows you to use the renderer in Sinatra
18
19 class BasicController <actionControlle::Base
20 include ActionView::Context
21 ...
22
23 * Single reponsibilities: easier to separate out pieces and use them, and also replace them
24 * Easier to understand each piece
25 * Not as easy to take in the whole piece (top-down comprehension)
26
27
28 O: Open/closed Prin.
29 --------------------
30
31 * Extend class behavior without modifying the class
32 * Class open for etension, closed for modification
33 * Latter isn't enforced by Ruby
34
35 * Don't break other people that depend on a library (e.g. modifying ActiveRecord::Base)
36 * Lots of poeple do --- makes it hard to run multiple rails apps on a single process
37
38 D: Dependency Inversion Prin.
39 -----------------------------
40
41 "Depend on abstractions, not on concretions"
42
43 * Duck typing, basically. Depend on abstractions (e.g. what methods are available), rather than a single concrete class.
44
45 match '/foo', to: 'posts#index'
46 match '/foo', to: PostsController.action(:index) # Missed this last part, but it's a rack app!
47
48 * Controllers have a middleware stack
49 * Remove hard-coded dependencies (e.g. pass as an optional parameter)
50 * Defining hooks (done in Railties)
51
52 L: Liskov Subs Prin.
53 --------------------
54
55 "Derived classes must be substitutable for their base classes"
56
57 * Need to respond to the same interface
58 * What about a new version of Rails?
59 * Static language: check to interface
60
61 include ActiveModel::Lint::Tests # kind of the same idea
62
63 I: Interface Seg. Prin
64 ----------------------
65
66 * Simplest protocol possible
67 * How to ensure?
68 * When you test, odn't use the conrete class -- use a Mock that only implements what you're expecting! (It's testing the 'concrete' interface, in a way)
69
70 Modularity driven by principles
Something went wrong with that request. Please try again.