Project

api_logic

0.0
No commit activity in last 3 years
No release in over 3 years
Mixes in to ActionController and generates default RESTful actions automatically in a highly customizable way.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

API Logic

A lightweight mixin for making API controllers

Usage

Extend a controller with ApiLogic and call exposes_model to generate the default REST actions.

class MyApiController < ApplicationController
  extend ApiLogic
  exposes_model Widget
  respond_to :json, :xml
end

or if the controller's name looks like #{resource}ApiController then exposes_model will automatically be called with the resource:

class CalendarApiController < ApplicationController
  extend ApiLogic
  respond_to :json, :xml
end

Customization

Responder

ApiLogic calls respond_with for each REST action, so you can change how it renders by replacing the Responder:

class CalendarApiController < ApplicationController
  extend ApiLogic
  responder = MyResponder
  respond_to :json, :xml
end

Templates

The recommended way of customizing how a model is rendered to JSON or XML is to supply a RABL template for the action.

Overriding methods

You can always override one of ApiLogic's actions by defining your own, but ApiLogic gives a few finer-grained ways of controlling its default behavior.

You can override the method find_models to change how the index action fetches models. By default find_models looks like this:

def find_models
  model_class.all
end

You can override the method find_model to change how the show, update, and destroy actions look up a model for the given URL. By default find_model looks like this:

def find_model
  @model = model_class.find(params[:id])
  instance_variable_set "@#{model_singular}", @model
end

create_attributes and update_attributes return the attributes supplied to model_class.create and @model.update_attributes in the create and update actions. Their default implementations are:

def create_attributes
  model_params
end

def update_attributes
  model_params
end

And, finally, you can change how a model's attributes are pulled out of params by overriding model_params. Its default implementation looks like this:

def model_params
  params[model_singular]
end