The missing Ruby on Rails ActionController REST API mixin.
A Ruby mixin (module) providing the seven standard resource actions & responses for an ActiveRecord :model_type & :collection.
Requires (tested in) Rails 3.2 & Ruby 1.9.
Add to your Gemfile:
gem 'typical_situation', github: 'mars/typical_situation'
class MockApplePiesController < ApplicationController
include TypicalSituation
# Symbolized, underscored version of the model (class) to use as the resource.
def model_type
:mock_apple_pie
end
# The collection of model instances.
def collection
current_user.mock_apple_pies
end
# Find a model instance by ID.
def find_in_collection(id)
collection.find_by_id(id)
end
end
The seven standard resourceful actions:
- index
- show
- new
- create
- edit
- update
- delete
For the content types:
- HTML
- JSON
With response handling for:
- the collection
- a single instance
- not found
- validation errors (using ActiveModel::Errors format)
- changed
- deleted/gone
TypicalSituation is composed of a library of common functionality, which can all be overridden in individual controllers. Express what is different & special about each controller, instead of repeating boilerplate.
The library is split into modules:
- identity - required definitions of the model & how to find it
- actions - high-level controller actions
- operations - loading, changing, & persisting the model
- responses - HTTP responses & redirects
This project uses MIT-LICENSE.