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 if @object and @object.errors and @object.errors.full_messages
21 errors = @object.errors.full_messages
25 render json: { errors: errors }, status: 422
29 render json: { errors: ["Path not found"] }, status: 401
33 @objects ||= model_class.all
42 @attrs = params[resource_name]
44 raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
46 if @attrs.class == String
47 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
49 @object = model_class.new @attrs
55 @attrs = params[resource_name]
56 if @attrs.is_a? String
57 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
59 @object.update_attributes @attrs
66 controller_name.classify.constantize
69 def resource_name # params[] key used by client
70 controller_name.singularize
73 def find_object_by_uuid
74 logger.info params.inspect
75 if params[:id] and params[:id].match /\D/
76 params[:uuid] = params.delete :id
78 @object = model_class.where('uuid=?', params[:uuid]).first
81 def self.accept_attribute_as_json(attr, force_class=nil)
82 before_filter lambda { accept_attribute_as_json attr, force_class }
84 def accept_attribute_as_json(attr, force_class)
85 if params[resource_name].is_a? Hash
86 if params[resource_name][attr].is_a? String
87 params[resource_name][attr] = JSON.parse params[resource_name][attr]
88 if force_class and !params[resource_name][attr].is_a? force_class
89 raise TypeError.new("#{resource_name}[#{attr.to_s}] must be a #{force_class.to_s}")
95 def uncamelcase_params_hash_keys
96 self.params = uncamelcase_hash_keys(params)
99 def uncamelcase_hash_keys(h, max_depth=-1)
100 if h.is_a? Hash and max_depth != 0
105 elsif k.class == Symbol
106 nk = k.to_s.underscore.to_sym
110 nh[nk] = uncamelcase_hash_keys(v, max_depth-1)
119 :kind => "orvos##{resource_name}List",
122 :next_page_token => "",
124 :items => @objects.map { |x| x }
126 render json: @object_list