2961: Escaped erb used embedded in javascript. Updated comments in
[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(Folders)
8   end
9
10   def remove_item
11     @removed_uuids = []
12     links = []
13     item = ArvadosBase.find params[:item_uuid]
14     if (item.class == Link and
15         item.link_class == 'name' and
16         item.tail_uuid = @object.uuid)
17       # Given uuid is a name link, linking an object to this
18       # folder. First follow the link to find the item we're removing,
19       # then delete the link.
20       links << item
21       item = ArvadosBase.find item.head_uuid
22     else
23       # Given uuid is an object. Delete all names.
24       links += Link.where(tail_uuid: @object.uuid,
25                           head_uuid: item.uuid,
26                           link_class: 'name')
27     end
28     links.each do |link|
29       @removed_uuids << link.uuid
30       link.destroy
31     end
32     if item.owner_uuid == @object.uuid
33       # Object is owned by this folder. Remove it from the folder by
34       # changing owner to the current user.
35       item.update_attributes owner_uuid: current_user
36       @removed_uuids << item.uuid
37     end
38   end
39
40   def index
41     @objects = Group.where(group_class: 'folder').order('name')
42     parent_of = {current_user.uuid => 'me'}
43     @objects.each do |ob|
44       parent_of[ob.uuid] = ob.owner_uuid
45     end
46     children_of = {false => [], 'me' => [current_user]}
47     @objects.each do |ob|
48       if ob.owner_uuid != current_user.uuid and
49           not parent_of.has_key? ob.owner_uuid
50         parent_of[ob.uuid] = false
51       end
52       children_of[parent_of[ob.uuid]] ||= []
53       children_of[parent_of[ob.uuid]] << ob
54     end
55     buildtree = lambda do |children_of, root_uuid=false|
56       tree = {}
57       children_of[root_uuid].andand.each do |ob|
58         tree[ob] = buildtree.call(children_of, ob.uuid)
59       end
60       tree
61     end
62     sorted_paths = lambda do |tree, depth=0|
63       paths = []
64       tree.keys.sort_by { |ob|
65         ob.is_a?(String) ? ob : ob.friendly_link_name
66       }.each do |ob|
67         paths << {object: ob, depth: depth}
68         paths += sorted_paths.call tree[ob], depth+1
69       end
70       paths
71     end
72     @my_folder_tree =
73       sorted_paths.call buildtree.call(children_of, 'me')
74     @shared_folder_tree =
75       sorted_paths.call({'Shared with me' =>
76                           buildtree.call(children_of, false)})
77
78     super
79   end
80
81   def choose
82     index
83     render partial: 'choose'
84   end
85
86   def show
87     @objects = @object.contents include_linked: true
88     @share_links = Link.filter([['head_uuid', '=', @object.uuid],
89                                 ['link_class', '=', 'permission']])
90     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
91
92     @objects_and_names = []
93     @objects.each do |object|
94       if !(name_links = @objects.links_for(object, 'name')).empty?
95         name_links.each do |name_link|
96           @objects_and_names << [object, name_link]
97         end
98       else
99         @objects_and_names << [object,
100                                Link.new(tail_uuid: @object.uuid,
101                                         head_uuid: object.uuid,
102                                         link_class: "name",
103                                         name: "")]
104       end
105     end
106
107     super
108   end
109
110   def create
111     @new_resource_attrs = (params['folder'] || {}).merge(group_class: 'folder')
112     @new_resource_attrs[:name] ||= 'New folder'
113     super
114   end
115
116   def update
117     @updates = params['folder']
118     super
119   end
120 end