1 class ApplicationController < ActionController::Base
3 before_filter :uncamelcase_params_hash_keys
4 before_filter :find_object_by_uuid, :except => :index
6 unless Rails.application.config.consider_all_requests_local
9 rescue_from ActiveRecord::RecordNotFound,
10 :with => :render_not_found
11 rescue_from ActionController::RoutingError,
12 :with => :render_not_found
13 rescue_from ActionController::UnknownController,
14 :with => :render_not_found
15 rescue_from ActionController::UnknownAction,
16 :with => :render_not_found
20 render json: { errors: @object.errors.full_messages }, status: 422
24 render json: { errors: ["Path not found"] }, status: 401
28 @objects ||= model_class.all
37 @attrs = params[resource_name]
39 raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
41 if @attrs.class == String
42 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
44 @object = model_class.new @attrs
50 @attrs = params[resource_name]
51 if @attrs.is_a? String
52 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
54 @object.update_attributes @attrs
61 controller_name.classify.constantize
64 def resource_name # params[] key used by client
65 controller_name.singularize
68 def find_object_by_uuid
69 logger.info params.inspect
70 if params[:id] and params[:id].match /\D/
71 params[:uuid] = params.delete :id
73 @object = model_class.where('uuid=?', params[:uuid]).first
76 def self.accept_attribute_as_json(attr, force_class=nil)
77 before_filter lambda { accept_attribute_as_json attr, force_class }
79 def accept_attribute_as_json(attr, force_class)
80 if params[resource_name].is_a? Hash
81 if params[resource_name][attr].is_a? String
82 params[resource_name][attr] = JSON.parse params[resource_name][attr]
83 if force_class and !params[resource_name][attr].is_a? force_class
84 raise TypeError.new("#{resource_name}[#{attr.to_s}] must be a #{force_class.to_s}")
90 def uncamelcase_params_hash_keys
91 self.params = uncamelcase_hash_keys(params)
94 def uncamelcase_hash_keys(h, max_depth=-1)
95 if h.is_a? Hash and max_depth != 0
100 elsif k.class == Symbol
101 nk = k.to_s.underscore.to_sym
105 nh[nk] = uncamelcase_hash_keys(v, max_depth-1)
114 :kind => "orvos##{resource_name}List",
117 :next_page_token => "",
119 :items => @objects.map { |x| x }
121 render json: @object_list