Implement "remove from folder"
[arvados.git] / apps / workbench / app / controllers / folders_controller.rb
1 class FoldersController < ApplicationController
2   def model_class
3     Group
4   end
5
6   def index_pane_list
7     %w(My_folders Shared_with_me)
8   end
9
10   def remove_item
11     @removed_uuids = []
12     item = ArvadosBase.find params[:item_uuid]
13     if (item.class == Link and
14         item.link_class == 'name' and
15         item.tail_uuid = @object.uuid)
16       # Given uuid is a name link, linking an object to this
17       # folder. First follow the link to find the item we're removing,
18       # then delete the link.
19       link = item
20       item = ArvadosBase.find link.head_uuid
21       @removed_uuids << link.uuid
22       link.destroy
23     end
24     if item.owner_uuid == @object.uuid
25       # Object is owned by this folder. Remove it from the folder by
26       # changing owner to the current user.
27       item.update_attributes owner_uuid: current_user
28       @removed_uuids << item.uuid
29     end
30   end
31
32   def index
33     @my_folders = []
34     @shared_with_me = []
35     @objects = Group.where(group_class: 'folder').order('name')
36     owner_of = {}
37     moretodo = true
38     while moretodo
39       moretodo = false
40       @objects.each do |folder|
41         if !owner_of[folder.uuid]
42           moretodo = true
43           owner_of[folder.uuid] = folder.owner_uuid
44         end
45         if owner_of[folder.owner_uuid]
46           if owner_of[folder.uuid] != owner_of[folder.owner_uuid]
47             owner_of[folder.uuid] = owner_of[folder.owner_uuid]
48             moretodo = true
49           end
50         end
51       end
52     end
53     @objects.each do |folder|
54       if owner_of[folder.uuid] == current_user.uuid
55         @my_folders << folder
56       else
57         @shared_with_me << folder
58       end
59     end
60     @object
61   end
62
63   def show
64     @objects = @object.contents include_linked: true
65     @share_links = Link.filter([['head_uuid', '=', @object.uuid],
66                                 ['link_class', '=', 'permission']])
67     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
68
69     @objects_and_names = []
70     @objects.each do |object|
71       if !(name_links = @objects.links_for(object, 'name')).empty?
72         name_links.each do |name_link|
73           @objects_and_names << [object, name_link]
74         end
75       else
76         @objects_and_names << [object,
77                                Link.new(tail_uuid: @object.uuid,
78                                         head_uuid: object.uuid,
79                                         link_class: "name",
80                                         name: "")]
81       end
82     end
83
84     super
85   end
86
87   def create
88     @new_resource_attrs = (params['folder'] || {}).merge(group_class: 'folder')
89     @new_resource_attrs[:name] ||= 'New folder'
90     super
91   end
92 end