EmFi is right. I didn't answer the question merely expressed my opinion.
Put the following code into an initializer file within the config initializers directory inside your Rails app. What you name the file is does not matter to the framework as all files in this directory are in the load path. I suggest that you call it actioncontroller_resource_monkeypatch.rb
in order to make the intent clear.
ActionController::Resources.module_eval do
def map_resource_routes(map, resource, action, route_path, route_name = nil, method = nil, resource_options = {} )
if resource.has_action?(action)
action_options = action_options_for(action, resource, method, resource_options)
formatted_route_path = route_path.match(/\/:format\//) ? route_path : "#{route_path}.:format"
if route_name && @set.named_routes[route_name.to_sym].nil?
map.named_route(route_name, formatted_route_path, action_options)
else
map.connect(formatted_route_path, action_options)
end
end
end
end
My answer uses the same method as EmFi's i.e. by monkeypatching ActionController::Resources#map_resource_routes
. I decided to throw my hat into the ring because it did not offer a full implementation, that was left as an exercise for yourself. I also feel that a ternary assignment of formatted_route_path
is much cleaner and more concise than an if-else/unless-else block. One additional line of code instead of five! That's the least I can do for a 200 bounty!
Now run rake routes
new_api_v1_company GET /api/v1/:format/company/new {:action=>"new", :controller=>"api/v1/companies"}
edit_api_v1_company GET /api/v1/:format/company/edit {:action=>"edit", :controller=>"api/v1/companies"}
api_v1_company GET /api/v1/:format/company {:action=>"show", :controller=>"api/v1/companies"}
PUT /api/v1/:format/company {:action=>"update", :controller=>"api/v1/companies"}
DELETE /api/v1/:format/company {:action=>"destroy", :controller=>"api/v1/companies"}
POST /api/v1/:format/company {:action=>"create", :controller=>"api/v1/companies"}
TADA!