Created
October 17, 2012 22:48
-
-
Save justinko/3908831 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
class PostsController | |
def index | |
@posts = Post.fetch_all | |
rescue Post::FetchError | |
redirect_to root_url | |
end | |
def recent | |
@posts = Post.fetch_recent | |
render 'index' | |
rescue Post::FetchError | |
redirect_to :back | |
end | |
end | |
# VS | |
class PostsController | |
# Since the error happens in template rendering, have to use `rescue_from` | |
# This results in a loss of context, IMO | |
rescue_from Post::FetchError do | |
case action_name | |
when 'index' | |
redirect_to root_url | |
when 'recent' | |
redirect_to :back | |
end | |
end | |
def index | |
respond_with ViewModel::Posts.new | |
end | |
def recent | |
# Would render the "index" template, somehow | |
respond_with ViewModel::RecentPosts.new | |
end | |
end | |
class ViewModel::Posts | |
def posts | |
Post.fetch_all | |
end | |
end | |
class ViewModel::RecentPosts | |
def posts | |
Post.fetch_recent | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment