Created
February 10, 2010 20:24
-
-
Save dcparker/300797 to your computer and use it in GitHub Desktop.
A versionable API routing-controller-action strategy.
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
# Routing | |
map.with_options(:namespace => 'api/', :path_prefix => "/api/:version", :version => /v\d+/) do |api| | |
api.resources :photos | |
end | |
# API Controller superclass | |
class ApiController < ApplicationController | |
append_view_path 'views/api' | |
# this will happen before anything else that cares about | |
# what params[:action] is, even before_filters. | |
def perform_action | |
params[:action] = @action_name = "#{params[:action]}_#{params[:version]}" | |
super | |
end | |
end | |
# Sample Api controller | |
class API::PhotosController < ApiController | |
def index_v1 | |
@photos = Photo.all | |
end | |
def index_v2 | |
@photos = current_person.photos | |
end | |
def show_v1 | |
@photo = Photo.find(params[:id]) | |
end | |
def show_v2 | |
@photo = current_person.photos.find(params[:id]) | |
end | |
end | |
# API views directory structure: | |
views/ | |
api/ | |
photos/ | |
index_v1.json.erb | |
index_v2.json.erb | |
show_v1.json.erb | |
videos/ | |
index_v1.json.erb | |
show_v1.json.erb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment