support where:{attr:[a,b,c]} query, check :eager param more carefully
[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 ||= if params[:where]
37       where = params[:where]
38       where = JSON.parse(where) if where.is_a?(String)
39       conditions = ['1=1']
40       where.each do |attr,value|
41         if (!value.nil? and
42             attr.to_s.match(/^[a-z][_a-z0-9]+$/) and
43             model_class.columns.collect(&:name).index(attr))
44           if value.is_a? Array
45             conditions[0] << " and #{attr} in (?)"
46             conditions << value
47           else
48             conditions[0] << " and #{attr}=?"
49             conditions << value
50           end
51         end
52       end
53       model_class.where(*conditions)
54     end
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)
58     end
59     render_list
60   end
61
62   def show
63     if @object
64       render json: @object.as_api_response(:superuser)
65     else
66       render_not_found("object not found")
67     end
68   end
69
70   def create
71     @attrs = params[resource_name]
72     if @attrs.nil?
73       raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
74     end
75     if @attrs.class == String
76       @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
77     end
78     @object = model_class.new @attrs
79     @object.save
80     show
81   end
82
83   def update
84     @attrs = params[resource_name]
85     if @attrs.is_a? String
86       @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
87     end
88     @object.update_attributes @attrs
89     show
90   end
91
92   protected
93
94   def model_class
95     controller_name.classify.constantize
96   end
97
98   def resource_name             # params[] key used by client
99     controller_name.singularize
100   end
101
102   def find_object_by_uuid
103     if params[:id] and params[:id].match /\D/
104       params[:uuid] = params.delete :id
105     end
106     @object = model_class.where('uuid=?', params[:uuid]).first
107   end
108
109   def self.accept_attribute_as_json(attr, force_class=nil)
110     before_filter lambda { accept_attribute_as_json attr, force_class }
111   end
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}")
118         end
119       end
120     end
121   end
122
123   def uncamelcase_params_hash_keys
124     self.params = uncamelcase_hash_keys(params)
125   end
126
127   def uncamelcase_hash_keys(h, max_depth=-1)
128     if h.is_a? Hash and max_depth != 0
129       nh = Hash.new
130       h.each do |k,v|
131         if k.class == String
132           nk = k.underscore
133         elsif k.class == Symbol
134           nk = k.to_s.underscore.to_sym
135         else
136           nk = k
137         end
138         nh[nk] = uncamelcase_hash_keys(v, max_depth-1)
139       end
140       h.replace(nh)
141     end
142     h
143   end
144
145   def render_list
146     @object_list = {
147       :kind  => "orvos##{resource_name}List",
148       :etag => "",
149       :self_link => "",
150       :next_page_token => "",
151       :next_link => "",
152       :items => @objects.as_api_response(:superuser)
153     }
154     render json: @object_list
155   end
156 end