1 class ProjectsController < ApplicationController
2 before_filter :set_share_links, if: -> { defined? @object }
3 if Rails.configuration.anonymous_user_token
4 skip_around_filter :require_thread_api_token, only: [:show, :tab_counts]
11 def find_object_by_uuid
12 if current_user and params[:uuid] == current_user.uuid
13 @object = current_user.dup
14 @object.uuid = current_user.uuid
22 def attribute_editable? attr, *args
24 when 'description', 'name'
40 # Returning an array of hashes instead of an array of strings will allow
41 # us to tell the interface to get counts for each pane (using :filters).
42 # It also seems to me that something like these could be used to configure the contents of the panes.
47 :name => 'Data_collections',
48 :filters => [%w(uuid is_a arvados#collection)]
52 :name => 'Jobs_and_pipelines',
53 :filters => [%w(uuid is_a) + [%w(arvados#job arvados#pipelineInstance)]]
57 :name => 'Pipeline_templates',
58 :filters => [%w(uuid is_a arvados#pipelineTemplate)]
62 :name => 'Subprojects',
63 :filters => [%w(uuid is_a arvados#group)]
67 :name => 'Other_objects',
68 :filters => [%w(uuid is_a) + [%w(arvados#human arvados#specimen arvados#trait)]]
70 pane_list << { :name => 'Sharing',
71 :count => @share_links.count } if @user_is_manager
72 pane_list << { :name => 'Advanced' }
75 # Called via AJAX and returns Javascript that populates tab counts into tab titles.
76 # References #show_pane_list action which should return an array of hashes each with :name
77 # and then optionally a :filters to run or a straight up :count
79 # This action could easily be moved to the ApplicationController to genericize the tab_counts behaviour,
80 # but one or more new routes would have to be created, the js.erb would also have to be moved
83 show_pane_list.each do |pane|
86 @tab_counts[pane[:name]] = pane[:count]
88 @tab_counts[pane[:name]] = @object.contents(filters: pane[:filters]).items_available
95 params[:item_uuids] = [params[:item_uuid]]
97 render template: 'projects/remove_items'
103 params[:item_uuids].collect { |uuid| ArvadosBase.find uuid }.each do |item|
104 if (item.class == Link and
105 item.link_class == 'name' and
106 item.tail_uuid == @object.uuid)
107 # Given uuid is a name link, linking an object to this
108 # project. First follow the link to find the item we're removing,
109 # then delete the link.
111 item = ArvadosBase.find item.head_uuid
113 # Given uuid is an object. Delete all names.
114 links += Link.where(tail_uuid: @object.uuid,
115 head_uuid: item.uuid,
119 @removed_uuids << link.uuid
123 # If this object has the 'expires_at' attribute, then simply mark it
125 if item.attributes.include?("expires_at")
126 item.update_attributes expires_at: Time.now
127 @removed_uuids << item.uuid
128 elsif item.owner_uuid == @object.uuid
129 # Object is owned by this project. Remove it from the project by
130 # changing owner to the current user.
132 item.update_attributes owner_uuid: current_user.uuid
133 @removed_uuids << item.uuid
134 rescue ArvadosApiClient::ApiErrorResponseException => e
135 if e.message.include? 'collection_owner_uuid_name_unique'
136 rename_to = item.name + ' removed from ' +
137 (@object.name ? @object.name : @object.uuid) +
138 ' at ' + Time.now.to_s
140 updates[:name] = rename_to
141 updates[:owner_uuid] = current_user.uuid
142 item.update_attributes updates
143 @removed_uuids << item.uuid
153 while (objects = Link.filter([['owner_uuid','=',@object.uuid],
154 ['tail_uuid','=',@object.uuid]])).any?
155 objects.each do |object|
159 while (objects = @object.contents(include_linked: false)).any?
160 objects.each do |object|
161 object.update_attributes! owner_uuid: current_user.uuid
164 if ArvadosBase::resource_class_for_uuid(@object.owner_uuid) == Group
165 params[:return_to] ||= group_path(@object.owner_uuid)
167 params[:return_to] ||= projects_path
172 def find_objects_for_index
173 @objects = all_projects
177 def load_contents_objects kinds=[]
178 kind_filters = @filters.select do |attr,op,val|
179 op == 'is_a' and val.is_a? Array and val.count > 1
181 if /^created_at\b/ =~ @order[0] and kind_filters.count == 1
182 # If filtering on multiple types and sorting by date: Get the
183 # first page of each type, sort the entire set, truncate to one
184 # page, and use the last item on this page as a filter for
185 # retrieving the next page. Ideally the API would do this for
186 # us, but it doesn't (yet).
188 # To avoid losing items that have the same created_at as the
189 # last item on this page, we retrieve an overlapping page with a
190 # "created_at <= last_created_at" filter, then remove duplicates
191 # with a "uuid not in [...]" filter (see below).
192 nextpage_operator = /\bdesc$/i =~ @order[0] ? '<=' : '>='
196 kind_filters.each do |attr,op,val|
197 (val.is_a?(Array) ? val : [val]).each do |type|
198 objects = @object.contents(order: @order,
200 include_linked: true,
201 filters: (@filters - kind_filters + [['uuid', 'is_a', type]]),
203 objects.each do |object|
204 @name_link_for[object.andand.uuid] = objects.links_for(object, 'name').first
209 @objects = @objects.to_a.sort_by(&:created_at)
210 @objects.reverse! if nextpage_operator == '<='
211 @objects = @objects[0..@limit-1]
212 @next_page_filters = @filters.reject do |attr,op,val|
213 (attr == 'created_at' and op == nextpage_operator) or
214 (attr == 'uuid' and op == 'not in')
218 last_created_at = @objects.last.created_at
221 @objects.each do |obj|
222 last_uuids << obj.uuid if obj.created_at.eql?(last_created_at)
225 @next_page_filters += [['created_at',
228 @next_page_filters += [['uuid', 'not in', last_uuids]]
229 @next_page_href = url_for(partial: :contents_rows,
231 filters: @next_page_filters.to_json)
233 @next_page_href = nil
236 @objects = @object.contents(order: @order,
238 include_linked: true,
241 @next_page_href = next_page_href(partial: :contents_rows,
242 filters: @filters.to_json,
243 order: @order.to_json)
246 preload_links_for_objects(@objects.to_a)
251 return render_not_found("object not found")
255 load_contents_objects
259 content: render_to_string(partial: 'show_contents_rows.html',
261 next_page_href: @next_page_href
272 @new_resource_attrs = (params['project'] || {}).merge(group_class: 'project')
273 @new_resource_attrs[:name] ||= 'New project'
278 @updates = params['project']
282 helper_method :get_objects_and_names
283 def get_objects_and_names(objects=nil)
284 objects = @objects if objects.nil?
285 objects_and_names = []
286 objects.each do |object|
287 if objects.respond_to? :links_for and
288 !(name_links = objects.links_for(object, 'name')).empty?
289 name_links.each do |name_link|
290 objects_and_names << [object, name_link]
292 elsif @name_link_for.andand[object.uuid]
293 objects_and_names << [object, @name_link_for[object.uuid]]
294 elsif object.respond_to? :name
295 objects_and_names << [object, object]
297 objects_and_names << [object,
298 Link.new(owner_uuid: @object.uuid,
299 tail_uuid: @object.uuid,
300 head_uuid: object.uuid,