Created
August 5, 2009 08:43
-
-
Save josevalim/162596 to your computer and use it in GitHub Desktop.
This file contains 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
class ActionController::Presenter | |
attr_reader :controller, :request, :format, :resource, :resource_location | |
def initialize(controller, resource) | |
@controller = controller | |
@request = controller.request | |
@format = controller.formats.first | |
@resource = resource.is_a?(Array) ? resource.last : resource | |
@resource_location = resource | |
end | |
delegate :head, :render, :redirect_to, :to => :controller | |
delegate :get?, :post?, :put?, :delete?, :to => :request | |
# Undefine :to_json since it's defined on Object | |
undef_method :to_json | |
def to_html | |
if get? | |
render | |
elsif has_errors? | |
render :action => default_action | |
else | |
redirect_to resource_location | |
end | |
end | |
def to_format | |
return render unless resourceful? | |
if get? | |
render format => resource | |
elsif has_errors? | |
render format => resource.errors, :status => :unprocessable_entity | |
elsif post? | |
render format => resource, :status => :created, :location => resource_location | |
else | |
head :ok | |
end | |
end | |
def resourceful? | |
resource.respond_to?(:"to_#{format}") | |
end | |
def has_errors? | |
resource.respond_to?(:errors) && !resource.errors.empty? | |
end | |
def default_action | |
request.post? ? :new : :edit | |
end | |
end |
This file contains 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
# Scaffold with presenter | |
class UsersController < ApplicationController | |
respond_to :html, :xml | |
# GET /users | |
# GET /users.xml | |
def index | |
@users = User.all | |
respond_with(@users) | |
end | |
# GET /users/1 | |
# GET /users/1.xml | |
def show | |
@user = User.find(params[:id]) | |
respond_with(@user) | |
end | |
# GET /users/new | |
# GET /users/new.xml | |
def new | |
@user = User.new | |
respond_with(@user) | |
end | |
# GET /users/1/edit | |
def edit | |
@user = User.find(params[:id]) | |
respond_with(@user) | |
end | |
# POST /users | |
# POST /users.xml | |
def create | |
@user = User.new(params[:user]) | |
flash[:notice] = 'User was successfully created.' if @user.save | |
respond_with(@user) | |
end | |
# PUT /users/1 | |
# PUT /users/1.xml | |
def update | |
@user = User.find(params[:id]) | |
flash[:notice] = 'User was successfully created.' if @user.update_attributes(params[:user]) | |
respond_with(@user) | |
end | |
# DELETE /users/1 | |
# DELETE /users/1.xml | |
def destroy | |
@user = User.find(params[:id]) | |
@user.destroy | |
respond_with(@user) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment