Created
February 7, 2012 18:29
-
-
Save jonleighton/1761129 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# People were asking what I meant by "single class per action". Here's an | |
# example. | |
# | |
# I've not tried this out at all, or tried to implement it or use it. I | |
# might be wrong. It's just an idea. Please don't hate on me :) | |
# | |
# Why do I wish for something like this? I can imagine being able to unit | |
# test it without all the ceremony we have to perform currently. Also, I | |
# feel that it adheres better to the single responsibility principle, and | |
# potentially allows more flexibility for code re-use. | |
module PostsController | |
class Action < ApplicationController | |
before_filter :require_admin | |
end | |
class List < Action | |
def posts | |
@posts ||= Post.all | |
end | |
def run | |
render :index | |
end | |
end | |
class Singular < Action | |
def post | |
@post ||= Post.find(params[:id]) | |
end | |
end | |
class Create < Singular | |
def run | |
if post.save | |
render :new | |
else | |
render :create | |
end | |
end | |
end | |
class Show < Singular | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Notice this is a method call, which would then hit the controller. --> | |
<% posts.each do %> | |
bla bla | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment