Merge branch '2891-improve-workbench-errors'
[arvados.git] / services / api / app / controllers / arvados / v1 / links_controller.rb
1 class Arvados::V1::LinksController < ApplicationController
2
3   def check_uuid_kind uuid, kind
4     if kind and ArvadosModel::resource_class_for_uuid(uuid).andand.kind != kind
5       send_error("'#{kind}' does not match uuid '#{uuid}', expected '#{ArvadosModel::resource_class_for_uuid(uuid).andand.kind}'",
6                  status: 422)
7       nil
8     else
9       true
10     end
11   end
12
13   def create
14     return if ! check_uuid_kind resource_attrs[:head_uuid], resource_attrs[:head_kind]
15     return if ! check_uuid_kind resource_attrs[:tail_uuid], resource_attrs[:tail_kind]
16
17     resource_attrs.delete :head_kind
18     resource_attrs.delete :tail_kind
19     super
20   end
21
22   protected
23
24   # Overrides ApplicationController load_where_param
25   def load_where_param
26     super
27
28     # head_kind and tail_kind columns are now virtual,
29     # equivilent functionality is now provided by
30     # 'is_a', so fix up any old-style 'where' clauses.
31     if @where
32       @filters ||= []
33       if @where[:head_kind]
34         @filters << ['head_uuid', 'is_a', @where[:head_kind]]
35         @where.delete :head_kind
36       end
37       if @where[:tail_kind]
38         @filters << ['tail_uuid', 'is_a', @where[:tail_kind]]
39         @where.delete :tail_kind
40       end
41     end
42   end
43
44   # Overrides ApplicationController load_filters_param
45   def load_filters_param
46     super
47
48     # head_kind and tail_kind columns are now virtual,
49     # equivilent functionality is now provided by
50     # 'is_a', so fix up any old-style 'filter' clauses.
51     @filters = @filters.map do |k|
52       if k[0] == 'head_kind' and k[1] == '='
53         ['head_uuid', 'is_a', k[2]]
54       elsif k[0] == 'tail_kind' and k[1] == '='
55         ['tail_uuid', 'is_a', k[2]]
56       else
57         k
58       end
59     end
60   end
61
62 end