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 logger.error e.inspect
21 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
22 if @object and @object.errors and @object.errors.full_messages
23 errors = @object.errors.full_messages
27 render json: { errors: errors }, status: 422
30 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
31 logger.error e.inspect
32 render json: { errors: ["Path not found"] }, status: 404
36 @objects ||= if params[:where]
37 where = params[:where]
38 where = JSON.parse(where) if where.is_a?(String)
40 where.each do |attr,value|
42 attr.to_s.match(/^[a-z][_a-z0-9]+$/) and
43 model_class.columns.collect(&:name).index(attr))
45 conditions[0] << " and #{attr} in (?)"
48 conditions[0] << " and #{attr}=?"
53 model_class.where(*conditions)
55 @objects ||= model_class.all
56 if params[:eager] and params[:eager] != '0' and params[:eager] != 0 and params[:eager] != ''
57 @objects.each(&:eager_load_associations)
64 render json: @object.as_api_response(:superuser)
66 render_not_found("object not found")
71 @attrs = params[resource_name]
73 raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
75 if @attrs.class == String
76 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
78 @object = model_class.new @attrs
84 @attrs = params[resource_name]
85 if @attrs.is_a? String
86 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
88 @object.update_attributes @attrs
95 controller_name.classify.constantize
98 def resource_name # params[] key used by client
99 controller_name.singularize
102 def find_object_by_uuid
103 if params[:id] and params[:id].match /\D/
104 params[:uuid] = params.delete :id
106 @object = model_class.where('uuid=?', params[:uuid]).first
109 def self.accept_attribute_as_json(attr, force_class=nil)
110 before_filter lambda { accept_attribute_as_json attr, force_class }
112 def accept_attribute_as_json(attr, force_class)
113 if params[resource_name].is_a? Hash
114 if params[resource_name][attr].is_a? String
115 params[resource_name][attr] = JSON.parse params[resource_name][attr]
116 if force_class and !params[resource_name][attr].is_a? force_class
117 raise TypeError.new("#{resource_name}[#{attr.to_s}] must be a #{force_class.to_s}")
123 def uncamelcase_params_hash_keys
124 self.params = uncamelcase_hash_keys(params)
127 def uncamelcase_hash_keys(h, max_depth=-1)
128 if h.is_a? Hash and max_depth != 0
133 elsif k.class == Symbol
134 nk = k.to_s.underscore.to_sym
138 nh[nk] = uncamelcase_hash_keys(v, max_depth-1)
147 :kind => "orvos##{resource_name}List",
150 :next_page_token => "",
152 :items => @objects.as_api_response(:superuser)
154 render json: @object_list