2872: Fix tests broken in merge.
[arvados.git] / apps / workbench / app / controllers / projects_controller.rb
1 class ProjectsController < ApplicationController
2   def model_class
3     Group
4   end
5
6   def index_pane_list
7     %w(Projects)
8   end
9
10   def show_pane_list
11     %w(Contents Permissions Advanced)
12   end
13
14   def remove_item
15     params[:item_uuids] = [params[:item_uuid]]
16     remove_items
17     render template: 'projects/remove_items'
18   end
19
20   def remove_items
21     @removed_uuids = []
22     links = []
23     params[:item_uuids].collect { |uuid| ArvadosBase.find uuid }.each do |item|
24       if (item.class == Link and
25           item.link_class == 'name' and
26           item.tail_uuid == @object.uuid)
27         # Given uuid is a name link, linking an object to this
28         # project. First follow the link to find the item we're removing,
29         # then delete the link.
30         links << item
31         item = ArvadosBase.find item.head_uuid
32       else
33         # Given uuid is an object. Delete all names.
34         links += Link.where(tail_uuid: @object.uuid,
35                             head_uuid: item.uuid,
36                             link_class: 'name')
37       end
38       links.each do |link|
39         @removed_uuids << link.uuid
40         link.destroy
41       end
42       if item.owner_uuid == @object.uuid
43         # Object is owned by this project. Remove it from the project by
44         # changing owner to the current user.
45         item.update_attributes owner_uuid: current_user.uuid
46         @removed_uuids << item.uuid
47       end
48     end
49   end
50
51   def find_objects_for_index
52     @objects = Group.
53       filter([['group_class','in',['project','folder']]]).
54       order('name')
55     super
56     parent_of = {current_user.uuid => 'me'}
57     @objects.each do |ob|
58       parent_of[ob.uuid] = ob.owner_uuid
59     end
60     children_of = {false => [], 'me' => [current_user]}
61     @objects.each do |ob|
62       if ob.owner_uuid != current_user.uuid and
63           not parent_of.has_key? ob.owner_uuid
64         parent_of[ob.uuid] = false
65       end
66       children_of[parent_of[ob.uuid]] ||= []
67       children_of[parent_of[ob.uuid]] << ob
68     end
69     buildtree = lambda do |children_of, root_uuid=false|
70       tree = {}
71       children_of[root_uuid].andand.each do |ob|
72         tree[ob] = buildtree.call(children_of, ob.uuid)
73       end
74       tree
75     end
76     sorted_paths = lambda do |tree, depth=0|
77       paths = []
78       tree.keys.sort_by { |ob|
79         ob.is_a?(String) ? ob : ob.friendly_link_name
80       }.each do |ob|
81         paths << {object: ob, depth: depth}
82         paths += sorted_paths.call tree[ob], depth+1
83       end
84       paths
85     end
86     @my_project_tree =
87       sorted_paths.call buildtree.call(children_of, 'me')
88     @shared_project_tree =
89       sorted_paths.call({'Shared with me' =>
90                           buildtree.call(children_of, false)})
91   end
92
93   def show
94     if !@object
95       return render_not_found("object not found")
96     end
97     @objects = @object.contents(limit: 50,
98                                 include_linked: true,
99                                 offset: params[:offset] || 0)
100     @share_links = Link.filter([['head_uuid', '=', @object.uuid],
101                                 ['link_class', '=', 'permission']])
102     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
103
104     @objects_and_names = []
105     @objects.each do |object|
106       if !(name_links = @objects.links_for(object, 'name')).empty?
107         name_links.each do |name_link|
108           @objects_and_names << [object, name_link]
109         end
110       elsif object.respond_to? :name
111         @objects_and_names << [object, object]
112       else
113         @objects_and_names << [object,
114                                Link.new(owner_uuid: @object.uuid,
115                                         tail_uuid: @object.uuid,
116                                         head_uuid: object.uuid,
117                                         link_class: "name",
118                                         name: "")]
119       end
120     end
121     if params[:partial]
122       respond_to do |f|
123         f.json {
124           render json: {
125             content: render_to_string(partial: 'show_contents_rows.html',
126                                       formats: [:html],
127                                       locals: {
128                                         objects_and_names: @objects_and_names,
129                                         project: @object
130                                       }),
131             next_page_href: (next_page_offset and
132                              url_for(offset: next_page_offset, partial: true))
133           }
134         }
135       end
136     else
137       super
138     end
139   end
140
141   def create
142     @new_resource_attrs = (params['project'] || {}).merge(group_class: 'project')
143     @new_resource_attrs[:name] ||= 'New project'
144     super
145   end
146
147   def update
148     @updates = params['project']
149     super
150   end
151 end