fix response type for #index
[arvados.git] / services / api / app / controllers / application_controller.rb
index a80d93e42feb9b07c69589bf38b20b3f65799b6e..c80b578adaa69efe1584caeece5fb225b4608d65 100644 (file)
@@ -14,6 +14,41 @@ class ApplicationController < ActionController::Base
 
   attr_accessor :resource_attrs
 
+  def index
+    @objects.uniq!(&:id)
+    if params[:eager] and params[:eager] != '0' and params[:eager] != 0 and params[:eager] != ''
+      @objects.each(&:eager_load_associations)
+    end
+    render_list
+  end
+
+  def show
+    if @object
+      render json: @object.as_api_response(:superuser)
+    else
+      render_not_found("object not found")
+    end
+  end
+
+  def create
+    @object = model_class.new resource_attrs
+    @object.save
+    show
+  end
+
+  def update
+    if @object.update_attributes resource_attrs
+      show
+    else
+      render json: { errors: @object.errors.full_messages }, status: 422
+    end
+  end
+
+  def destroy
+    @object.destroy
+    show
+  end
+
   def catch_redirect_hint
     if !current_user
       if params.has_key?('redirect_to') then
@@ -51,6 +86,8 @@ class ApplicationController < ActionController::Base
     render json: { errors: ["Path not found"] }, status: 404
   end
 
+  protected
+
   def find_objects_for_index
     uuid_list = [current_user.uuid, *current_user.groups_i_can(:read)]
     sanitized_uuid_list = uuid_list.
@@ -109,40 +146,6 @@ class ApplicationController < ActionController::Base
     @objects = @objects.order("#{table_name}.modified_at desc")
   end
 
-  def index
-    @objects.uniq!(&:id)
-    if params[:eager] and params[:eager] != '0' and params[:eager] != 0 and params[:eager] != ''
-      @objects.each(&:eager_load_associations)
-    end
-    render_list
-  end
-
-  def show
-    if @object
-      render json: @object.as_api_response(:superuser)
-    else
-      render_not_found("object not found")
-    end
-  end
-
-  def create
-    @object = model_class.new resource_attrs
-    @object.save
-    show
-  end
-
-  def update
-    @object.update_attributes resource_attrs
-    show
-  end
-
-  def destroy
-    @object.destroy
-    show
-  end
-
-  protected
-
   def resource_attrs
     return @attrs if @attrs
     @attrs = params[resource_name]
@@ -177,7 +180,10 @@ class ApplicationController < ActionController::Base
       user = nil
       api_client = nil
       api_client_auth = nil
-      supplied_token = params[:api_token] || params[:oauth_token]
+      supplied_token =
+        params[:api_token] ||
+        params[:oauth_token] ||
+        request.headers["Authorization"].andand.match(/OAuth2 ([a-z0-9]+)/).andand[1]
       if supplied_token
         api_client_auth = ApiClientAuthorization.
           includes(:api_client, :user).
@@ -254,7 +260,6 @@ class ApplicationController < ActionController::Base
   def uncamelcase_params_hash_keys
     self.params = uncamelcase_hash_keys(params)
   end
-
   def uncamelcase_hash_keys(h, max_depth=-1)
     if h.is_a? Hash and max_depth != 0
       nh = Hash.new
@@ -275,7 +280,7 @@ class ApplicationController < ActionController::Base
 
   def render_list
     @object_list = {
-      :kind  => "orvos##{resource_name}List",
+      :kind  => "arvados##{resource_name}List",
       :etag => "",
       :self_link => "",
       :next_page_token => "",