Skip to content

Instantly share code, notes, and snippets.

@jonleighton
Created February 7, 2012 18:29
Show Gist options
  • Save jonleighton/1761129 to your computer and use it in GitHub Desktop.
Save jonleighton/1761129 to your computer and use it in GitHub Desktop.
# 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
<!-- 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