-
Notifications
You must be signed in to change notification settings - Fork 68
Keep your api templates out of your models
fabrik42 edited this page Jul 12, 2011
·
2 revisions
Especially if you have multiple API templates or models with many fields, it's a good idea to put your API template definitions in their own file. Here is a possible solution:
First create a directory /app/representations/api_v1
. This is were we will keep our API templates.
Now add the following line in your application configuration in the /config/application.rb
file:
config.autoload_paths += %W(#{config.root}/app/representations)
Given we have a model named User
we will now create the file /app/representations/api_v1/user.rb
where we define the API templates for the User
model:
module ApiV1::User
extend ActiveSupport::Concern
included do
api_accessible :name_only do |t|
t.add :first_name
end
end
end
In the User
model itself we just include the module:
class User < ActiveRecord::Base
acts_as_api
include ApiV1::User
end
That's it! :)