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