Merge branch '8784-dir-listings'
[arvados.git] / services / api / lib / common_api_template.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 module CommonApiTemplate
6   def self.included(base)
7     base.acts_as_api
8     base.class_eval do
9       alias_method :as_api_response_orig, :as_api_response
10       include InstanceMethods
11     end
12     base.extend(ClassMethods)
13     base.api_accessible :common do |t|
14       t.add :href
15       t.add :kind
16       t.add :etag
17       t.add :uuid
18       t.add :owner_uuid
19       t.add :created_at
20       t.add :modified_by_client_uuid
21       t.add :modified_by_user_uuid
22       t.add :modified_at
23     end
24   end
25
26   module InstanceMethods
27     # choose template based on opts[:for_user]
28     def as_api_response(template=nil, opts={})
29       if template.nil?
30         user = opts[:for_user] || current_user
31         if user.andand.is_admin and self.respond_to? :api_accessible_superuser
32           template = :superuser
33         else
34           template = :user
35         end
36       end
37       self.as_api_response_orig(template, opts)
38     end
39   end
40
41   module ClassMethods
42   end
43 end