Autoloading ActiveResource schemas
Posted by rick, January 27, 2009 @ 9:03 pm
class PostsController < ApplicationController
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
end
There is one problem though, this probably won't work for protected or nested actions. Ideally a Rails app will set a top level route for the resource, and disable any before filters if `request.format.xml?` is true.
map.route 'tickets/new.:format', :controller => 'tickets', :action => 'new'
You can get around this by modifying the `#schema` hash directly, or calling `#reset_schema` with your own prefixes. Here's a sample using the [Lighthouse API lib](http://github.com/Caged/lighthouse-api/tree/master).
Lighthouse.account = 'entp'
Lighthouse.token = 'monkey'
Lighthouse::Ticket.reset_schema :project_id => 1
# or, use a well known public project
Lighthouse.account = 'rails'
Lighthouse::Ticket.reset_schema :project_id => 8994
Ticket.new # => #<Lighthouse::Ticket:0x1707898 @attributes={"permalink"=>nil, "updated_at"=>nil, "number"=>nil, "title"=>nil, "creator_id"=>nil, "tag"=>nil, "attachments_count"=>0, "priority"=>0, "closed"=>false, "assigned_user_id"=>nil, "user_id"=>nil, "created_at"=>nil, "state"=>nil, "milestone_id"=>nil}, @prefix_options={:project_id=>8994}>
Comments?