Amos - A Model Only Server.

AMOS is a rails plugin that provides a simple server that uses the url to decide which model to deliver. It is intended for front-end applications that use something like javascriptMVC.

E.g. If the incoming request is /recipe it executes the index action which does a find(:all) on the Recipe model.

Supported actions

:get => "/user" returns array of all users
:get => "/user?fields=name,email" returns only the name and email attributes
:get => "/users/1" returns record for user 1
:get => "/users/1?fields=name,email" returns only the name and email attributes for user 1
:get => "/users/1?associations=posts" returns the record for user plus any post
:delete => "/users/1" deletes record 1 from users, returns success => true / false
:put => "/users/1" + form data. Updates user record 1.  Returns success => true / false
:post => "/users" + form data.  Returns details of new record on success, or errors array on failure

*Amos gem is now available on rubygems.org*

Distributed under the MIT License, all rights reserved 2011 Geoff Drake

Source

The source is available on GitHub github.com/geoffd123/amos

Installation

Create a rails application as normal using:

rails create MyApp

Add the following line to your Gemfile

gem 'amos'

Run

bundle install

Amos uses cancan for its security checking so you need to add the following class in app/models/ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    can :manage, :all
  end
end

Replace the code inside the initialize with your requirements. There is a skeleton class in lib/app/models/ability.rb that disallows any access, so if you are getting authorisation errors you have not overridden that version correctly.

Cancan also needs access to a method called current_user in the controllers. If you are using devise or similar this should automatically be available.

If not you will need to define the following in your ApplicationController class:

class ApplicationController < ActionController::Base
  def current_user
    nil
  end
end

If you are using user authentication replace the nil return with the current user record.

Finally create some models that match the code you have on the front-end.

rails g model recipes name:string description:string

And start the server

rails s

Your models should now be available on /recipe etc.

Take a look at spec/controllers/amos_controller_spec.rb and test/dummy/features/amos.feature for some examples of accessing the data and what is returned.

If you want to have the index and dynamic finder results paginated include this at the top of your model:

User < ActiveRecord::Base
  paginate_results

  .... your code ....

end

This will use will_paginate to paginate the results.

Things to to

  • More tests against a javascriptMVC application

Change list

Edge

In progress

0.0.4

Added test and spec files to gemspec Added functionality to allow use of rails dynamic finders Added ability to paginate index and dynamic finder results

0.0.3

Fixed problem with incomplete file list in gemspec. Fixed problem with cancan methods not being found when using gem in a rails app.

0.0.2

Implemented fields filters, associations. Changed js errors to return correct status codes Implemented pluggable security using CanCan

0.0.1

Basic skeleton and minimal functionality