Skip to content

Extending an existing api template

fabrik42 edited this page Apr 20, 2011 · 2 revisions

Extending an existing api template

To keep your api templates DRY, you can create a new template based on another template of the same model.

class User < ActiveRecord::Base

  acts_as_api

  api_accessible :only_names do |t|
    t.add :first_name
    t.add :last_name          
  end

  api_accessible :age_and_names, :extend => :only_names do |t|
    t.add :age
  end

end

The api template age_and_names extends the template only_names so it will contain the attributes first_name, last_name and age.

Removing an attribute from a template

It is also possible to remove attributes that were inherited from the api template.

class User < ActiveRecord::Base

  acts_as_api

  api_accessible :only_names do |t|
    t.add :first_name
    t.add :last_name          
  end

  api_accessible :age_and_first_name, :extend => :only_names do |t|
    t.add :age
    t.remove :last_name
  end

end

Now responses with the template age_and_first_name will contain the attributes first_name and age.