module RRRMatey::CrudController

Public Class Methods

included(base) click to toggle source
# File lib/rrrmatey/crud_controller.rb, line 3
def self.included(base)
    base.extend(ModelMethods)
end

Public Instance Methods

destroy() click to toggle source
# File lib/rrrmatey/crud_controller.rb, line 47
def destroy
    id = params[:id]
    if id.blank?
        return respond_bad_request
    end
    self.class.model_klass.delete(id)
    respond_ok(nil)
end
index() click to toggle source
# File lib/rrrmatey/crud_controller.rb, line 28
def index
    offset = params[:offset] || 0
    limit = params[:limit] || 20
    items = self.class.model_klass.list(offset, limit)
    respond_ok(items)
end
respond_bad_request() click to toggle source
# File lib/rrrmatey/crud_controller.rb, line 69
def respond_bad_request
    respond_to do |format|
        format.json { render :json => nil, :status => 400 }
        format.xml { render :xml => nil, :status => 400 }
    end
end
respond_internal_server_error(e) click to toggle source
# File lib/rrrmatey/crud_controller.rb, line 83
def respond_internal_server_error(e)
    respond_to do |format|
        format.json { render :json => { :mesage => e.message }, :status => 500 }
        format.xml { render :xml => { :message => e.message }, :status => 500 }
    end
end
respond_not_found() click to toggle source
# File lib/rrrmatey/crud_controller.rb, line 76
def respond_not_found
    respond_to do |format|
        format.json { render :json => 'Not Found', :status => 404 }
        format.xml { render :xml => 'Not Found', :status => 404 }
    end
end
respond_ok(item) click to toggle source
# File lib/rrrmatey/crud_controller.rb, line 90
def respond_ok(item)
    respond_to do |format|
        format.json { render :json => item }
        format.xml { render :xml => item  }
    end
end
show() click to toggle source
# File lib/rrrmatey/crud_controller.rb, line 35
def show
    id = params[:id]
    if id.blank?
        return respond_bad_request
    end
    item = self.class.model_klass.get(id)
    if item.nil?
        return respond_not_found
    end
    respond_ok(item)
end
update() click to toggle source
# File lib/rrrmatey/crud_controller.rb, line 56
def update
    # TODO: get and merge with params if a patch-y update is desired
    item = self.class.model_klass.from_params(params)
    begin
        item.save
    rescue RRRMatey::InvalidModelError
        return respond_bad_request
    rescue StandardError => e
        return respond_internal_server_error(e)
    end
    respond_ok(nil)
end