Update to latest Rails 3.1 release
[arvados.git] / app / controllers / application_controller.rb
1 class ApplicationController < ActionController::Base
2   protect_from_forgery
3   before_filter :uncamelcase_params_hash_keys
4   before_filter :find_object_by_uuid, :except => :index
5
6   unless Rails.application.config.consider_all_requests_local
7     rescue_from Exception,
8     :with => :render_error
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
17   end
18
19   def render_error(e)
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
24     else
25       errors = [e.inspect]
26     end
27     render json: { errors: errors }, status: 422
28   end
29
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
33   end
34
35   def index
36     @objects ||= model_class.all
37     render_list
38   end
39
40   def show
41     if @object
42       render json: @object.as_api_response(:superuser)
43     else
44       render_not_found("object not found")
45     end
46   end
47
48   def create
49     @attrs = params[resource_name]
50     if @attrs.nil?
51       raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
52     end
53     if @attrs.class == String
54       @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
55     end
56     @object = model_class.new @attrs
57     @object.save
58     show
59   end
60
61   def update
62     @attrs = params[resource_name]
63     if @attrs.is_a? String
64       @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
65     end
66     @object.update_attributes @attrs
67     show
68   end
69
70   protected
71
72   def model_class
73     controller_name.classify.constantize
74   end
75
76   def resource_name             # params[] key used by client
77     controller_name.singularize
78   end
79
80   def find_object_by_uuid
81     if params[:id] and params[:id].match /\D/
82       params[:uuid] = params.delete :id
83     end
84     @object = model_class.where('uuid=?', params[:uuid]).first
85   end
86
87   def self.accept_attribute_as_json(attr, force_class=nil)
88     before_filter lambda { accept_attribute_as_json attr, force_class }
89   end
90   def accept_attribute_as_json(attr, force_class)
91     if params[resource_name].is_a? Hash
92       if params[resource_name][attr].is_a? String
93         params[resource_name][attr] = JSON.parse params[resource_name][attr]
94         if force_class and !params[resource_name][attr].is_a? force_class
95           raise TypeError.new("#{resource_name}[#{attr.to_s}] must be a #{force_class.to_s}")
96         end
97       end
98     end
99   end
100
101   def uncamelcase_params_hash_keys
102     self.params = uncamelcase_hash_keys(params)
103   end
104
105   def uncamelcase_hash_keys(h, max_depth=-1)
106     if h.is_a? Hash and max_depth != 0
107       nh = Hash.new
108       h.each do |k,v|
109         if k.class == String
110           nk = k.underscore
111         elsif k.class == Symbol
112           nk = k.to_s.underscore.to_sym
113         else
114           nk = k
115         end
116         nh[nk] = uncamelcase_hash_keys(v, max_depth-1)
117       end
118       h.replace(nh)
119     end
120     h
121   end
122
123   def render_list
124     @object_list = {
125       :kind  => "orvos##{resource_name}List",
126       :etag => "",
127       :self_link => "",
128       :next_page_token => "",
129       :next_link => "",
130       :items => @objects.as_api_response(:superuser)
131     }
132     render json: @object_list
133   end
134 end