4233: will rescale y-axis for a series that scrolls off leaving less than fully scale...
[arvados.git] / apps / workbench / app / controllers / application_controller.rb
1 class ApplicationController < ActionController::Base
2   include ArvadosApiClientHelper
3   include ApplicationHelper
4
5   respond_to :html, :json, :js
6   protect_from_forgery
7
8   ERROR_ACTIONS = [:render_error, :render_not_found]
9
10   around_filter :thread_clear
11   around_filter :set_thread_api_token
12   # Methods that don't require login should
13   #   skip_around_filter :require_thread_api_token
14   around_filter :require_thread_api_token, except: ERROR_ACTIONS
15   before_filter :set_cache_buster
16   before_filter :accept_uuid_as_id_param, except: ERROR_ACTIONS
17   before_filter :check_user_agreements, except: ERROR_ACTIONS
18   before_filter :check_user_profile, except: ERROR_ACTIONS
19   before_filter :check_user_notifications, except: ERROR_ACTIONS
20   before_filter :load_filters_and_paging_params, except: ERROR_ACTIONS
21   before_filter :find_object_by_uuid, except: [:create, :index, :choose] + ERROR_ACTIONS
22   theme :select_theme
23
24   begin
25     rescue_from(ActiveRecord::RecordNotFound,
26                 ActionController::RoutingError,
27                 ActionController::UnknownController,
28                 AbstractController::ActionNotFound,
29                 with: :render_not_found)
30     rescue_from(Exception,
31                 ActionController::UrlGenerationError,
32                 with: :render_exception)
33   end
34
35   def set_cache_buster
36     response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
37     response.headers["Pragma"] = "no-cache"
38     response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
39   end
40
41   def unprocessable(message=nil)
42     @errors ||= []
43
44     @errors << message if message
45     render_error status: 422
46   end
47
48   def render_error(opts={})
49     opts[:status] ||= 500
50     respond_to do |f|
51       # json must come before html here, so it gets used as the
52       # default format when js is requested by the client. This lets
53       # ajax:error callback parse the response correctly, even though
54       # the browser can't.
55       f.json { render opts.merge(json: {success: false, errors: @errors}) }
56       f.html { render({action: 'error'}.merge(opts)) }
57     end
58   end
59
60   def render_exception(e)
61     logger.error e.inspect
62     logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
63     err_opts = {status: 422}
64     if e.is_a?(ArvadosApiClient::ApiError)
65       err_opts.merge!(action: 'api_error', locals: {api_error: e})
66       @errors = e.api_response[:errors]
67     elsif @object.andand.errors.andand.full_messages.andand.any?
68       @errors = @object.errors.full_messages
69     else
70       @errors = [e.to_s]
71     end
72     # Make user information available on the error page, falling back to the
73     # session cache if the API server is unavailable.
74     begin
75       load_api_token(session[:arvados_api_token])
76     rescue ArvadosApiClient::ApiError
77       unless session[:user].nil?
78         begin
79           Thread.current[:user] = User.new(session[:user])
80         rescue ArvadosApiClient::ApiError
81           # This can happen if User's columns are unavailable.  Nothing to do.
82         end
83       end
84     end
85     # Preload projects trees for the template.  If that's not doable, set empty
86     # trees so error page rendering can proceed.  (It's easier to rescue the
87     # exception here than in a template.)
88     unless current_user.nil?
89       begin
90         build_project_trees
91       rescue ArvadosApiClient::ApiError
92         # Fall back to the default-setting code later.
93       end
94     end
95     @my_project_tree ||= []
96     @shared_project_tree ||= []
97     render_error(err_opts)
98   end
99
100   def render_not_found(e=ActionController::RoutingError.new("Path not found"))
101     logger.error e.inspect
102     @errors = ["Path not found"]
103     set_thread_api_token do
104       self.render_error(action: '404', status: 404)
105     end
106   end
107
108   # params[:order]:
109   #
110   # The order can be left empty to allow it to default.
111   # Or it can be a comma separated list of real database column names, one per model.
112   # Column names should always be qualified by a table name and a direction is optional, defaulting to asc
113   # (e.g. "collections.name" or "collections.name desc").
114   # If a column name is specified, that table will be sorted by that column.
115   # If there are objects from different models that will be shown (such as in Jobs and Pipelines tab),
116   # then a sort column name can optionally be specified for each model, passed as an comma-separated list (e.g. "jobs.script, pipeline_instances.name")
117   # Currently only one sort column name and direction can be specified for each model.
118   def load_filters_and_paging_params
119     if params[:order].blank?
120       @order = 'created_at desc'
121     elsif params[:order].is_a? Array
122       @order = params[:order]
123     else
124       begin
125         @order = JSON.load(params[:order])
126       rescue
127         @order = params[:order].split(',')
128       end
129     end
130     @order = [@order] unless @order.is_a? Array
131
132     @limit ||= 200
133     if params[:limit]
134       @limit = params[:limit].to_i
135     end
136
137     @offset ||= 0
138     if params[:offset]
139       @offset = params[:offset].to_i
140     end
141
142     @filters ||= []
143     if params[:filters]
144       filters = params[:filters]
145       if filters.is_a? String
146         filters = Oj.load filters
147       elsif filters.is_a? Array
148         filters = filters.collect do |filter|
149           if filter.is_a? String
150             # Accept filters[]=["foo","=","bar"]
151             Oj.load filter
152           else
153             # Accept filters=[["foo","=","bar"]]
154             filter
155           end
156         end
157       end
158       # After this, params[:filters] can be trusted to be an array of arrays:
159       params[:filters] = filters
160       @filters += filters
161     end
162   end
163
164   def find_objects_for_index
165     @objects ||= model_class
166     @objects = @objects.filter(@filters).limit(@limit).offset(@offset)
167   end
168
169   def render_index
170     respond_to do |f|
171       f.json { render json: @objects }
172       f.html {
173         if params[:tab_pane]
174           render_pane params[:tab_pane]
175         else
176           render
177         end
178       }
179       f.js { render }
180     end
181   end
182
183   helper_method :render_pane
184   def render_pane tab_pane, opts={}
185     render_opts = {
186       partial: 'show_' + tab_pane.downcase,
187       locals: {
188         comparable: self.respond_to?(:compare),
189         objects: @objects,
190         tab_pane: tab_pane
191       }.merge(opts[:locals] || {})
192     }
193     if opts[:to_string]
194       render_to_string render_opts
195     else
196       render render_opts
197     end
198   end
199
200   def index
201     find_objects_for_index if !@objects
202     render_index
203   end
204
205   helper_method :next_page_offset
206   def next_page_offset objects=nil
207     if !objects
208       objects = @objects
209     end
210     if objects.respond_to?(:result_offset) and
211         objects.respond_to?(:result_limit) and
212         objects.respond_to?(:items_available)
213       next_offset = objects.result_offset + objects.result_limit
214       if next_offset < objects.items_available
215         next_offset
216       else
217         nil
218       end
219     end
220   end
221
222   helper_method :next_page_href
223   def next_page_href with_params={}
224     if next_page_offset
225       url_for with_params.merge(offset: next_page_offset)
226     end
227   end
228
229   def show
230     if !@object
231       return render_not_found("object not found")
232     end
233     respond_to do |f|
234       f.json do
235         extra_attrs = { href: url_for(action: :show, id: @object) }
236         @object.textile_attributes.each do |textile_attr|
237           extra_attrs.merge!({ "#{textile_attr}Textile" => view_context.render_markup(@object.attributes[textile_attr]) })
238         end
239         render json: @object.attributes.merge(extra_attrs)
240       end
241       f.html {
242         if params['tab_pane']
243           render_pane(if params['tab_pane'].is_a? Hash then params['tab_pane']["name"] else params['tab_pane'] end)
244         elsif request.method.in? ['GET', 'HEAD']
245           render
246         else
247           redirect_to params[:return_to] || @object
248         end
249       }
250       f.js { render }
251     end
252   end
253
254   def choose
255     params[:limit] ||= 40
256     respond_to do |f|
257       if params[:partial]
258         f.json {
259           find_objects_for_index if !@objects
260           render json: {
261             content: render_to_string(partial: "choose_rows.html",
262                                       formats: [:html]),
263             next_page_href: next_page_href(partial: params[:partial])
264           }
265         }
266       end
267       f.js {
268         find_objects_for_index if !@objects
269         render partial: 'choose', locals: {multiple: params[:multiple]}
270       }
271     end
272   end
273
274   def render_content
275     if !@object
276       return render_not_found("object not found")
277     end
278   end
279
280   def new
281     @object = model_class.new
282   end
283
284   def update
285     @updates ||= params[@object.resource_param_name.to_sym]
286     @updates.keys.each do |attr|
287       if @object.send(attr).is_a? Hash
288         if @updates[attr].is_a? String
289           @updates[attr] = Oj.load @updates[attr]
290         end
291         if params[:merge] || params["merge_#{attr}".to_sym]
292           # Merge provided Hash with current Hash, instead of
293           # replacing.
294           @updates[attr] = @object.send(attr).with_indifferent_access.
295             deep_merge(@updates[attr].with_indifferent_access)
296         end
297       end
298     end
299     if @object.update_attributes @updates
300       show
301     else
302       self.render_error status: 422
303     end
304   end
305
306   def create
307     @new_resource_attrs ||= params[model_class.to_s.underscore.singularize]
308     @new_resource_attrs ||= {}
309     @new_resource_attrs.reject! { |k,v| k.to_s == 'uuid' }
310     @object ||= model_class.new @new_resource_attrs, params["options"]
311     if @object.save
312       respond_to do |f|
313         f.json { render json: @object.attributes.merge(href: url_for(action: :show, id: @object)) }
314         f.html {
315           redirect_to @object
316         }
317         f.js { render }
318       end
319     else
320       self.render_error status: 422
321     end
322   end
323
324   # Clone the given object, merging any attribute values supplied as
325   # with a create action.
326   def copy
327     @new_resource_attrs ||= params[model_class.to_s.underscore.singularize]
328     @new_resource_attrs ||= {}
329     @object = @object.dup
330     @object.update_attributes @new_resource_attrs
331     if not @new_resource_attrs[:name] and @object.respond_to? :name
332       if @object.name and @object.name != ''
333         @object.name = "Copy of #{@object.name}"
334       else
335         @object.name = ""
336       end
337     end
338     @object.save!
339     show
340   end
341
342   def destroy
343     if @object.destroy
344       respond_to do |f|
345         f.json { render json: @object }
346         f.html {
347           redirect_to(params[:return_to] || :back)
348         }
349         f.js { render }
350       end
351     else
352       self.render_error status: 422
353     end
354   end
355
356   def current_user
357     Thread.current[:user]
358   end
359
360   def model_class
361     controller_name.classify.constantize
362   end
363
364   def breadcrumb_page_name
365     (@breadcrumb_page_name ||
366      (@object.friendly_link_name if @object.respond_to? :friendly_link_name) ||
367      action_name)
368   end
369
370   def index_pane_list
371     %w(Recent)
372   end
373
374   def show_pane_list
375     %w(Attributes Advanced)
376   end
377
378   protected
379
380   def strip_token_from_path(path)
381     path.sub(/([\?&;])api_token=[^&;]*[&;]?/, '\1')
382   end
383
384   def redirect_to_login
385     respond_to do |f|
386       f.html {
387         if request.method.in? ['GET', 'HEAD']
388           redirect_to arvados_api_client.arvados_login_url(return_to: strip_token_from_path(request.url))
389         else
390           flash[:error] = "Either you are not logged in, or your session has timed out. I can't automatically log you in and re-attempt this request."
391           redirect_to :back
392         end
393       }
394       f.json {
395         @errors = ['You do not seem to be logged in. You did not supply an API token with this request, and your session (if any) has timed out.']
396         self.render_error status: 422
397       }
398     end
399     false  # For convenience to return from callbacks
400   end
401
402   def using_specific_api_token(api_token, opts={})
403     start_values = {}
404     [:arvados_api_token, :user].each do |key|
405       start_values[key] = Thread.current[key]
406     end
407     if opts.fetch(:load_user, true)
408       load_api_token(api_token)
409     else
410       Thread.current[:arvados_api_token] = api_token
411       Thread.current[:user] = nil
412     end
413     begin
414       yield
415     ensure
416       start_values.each_key { |key| Thread.current[key] = start_values[key] }
417     end
418   end
419
420
421   def accept_uuid_as_id_param
422     if params[:id] and params[:id].match /\D/
423       params[:uuid] = params.delete :id
424     end
425   end
426
427   def find_object_by_uuid
428     begin
429       if not model_class
430         @object = nil
431       elsif not params[:uuid].is_a?(String)
432         @object = model_class.where(uuid: params[:uuid]).first
433       elsif params[:uuid].empty?
434         @object = nil
435       elsif (model_class != Link and
436              resource_class_for_uuid(params[:uuid]) == Link)
437         @name_link = Link.find(params[:uuid])
438         @object = model_class.find(@name_link.head_uuid)
439       else
440         @object = model_class.find(params[:uuid])
441       end
442     rescue ArvadosApiClient::NotFoundException, RuntimeError => error
443       if error.is_a?(RuntimeError) and (error.message !~ /^argument to find\(/)
444         raise
445       end
446       render_not_found(error)
447       return false
448     end
449   end
450
451   def thread_clear
452     load_api_token(nil)
453     Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
454     yield
455     Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
456   end
457
458   # Set up the thread with the given API token and associated user object.
459   def load_api_token(new_token)
460     Thread.current[:arvados_api_token] = new_token
461     if new_token.nil?
462       Thread.current[:user] = nil
463     else
464       Thread.current[:user] = User.current
465     end
466   end
467
468   # If there's a valid api_token parameter, set up the session with that
469   # user's information.  Return true if the method redirects the request
470   # (usually a post-login redirect); false otherwise.
471   def setup_user_session
472     return false unless params[:api_token]
473     Thread.current[:arvados_api_token] = params[:api_token]
474     begin
475       user = User.current
476     rescue ArvadosApiClient::NotLoggedInException
477       false  # We may redirect to login, or not, based on the current action.
478     else
479       session[:arvados_api_token] = params[:api_token]
480       # If we later have trouble contacting the API server, we still want
481       # to be able to render basic user information in the UI--see
482       # render_exception above.  We store that in the session here.  This is
483       # not intended to be used as a general-purpose cache.  See #2891.
484       session[:user] = {
485         uuid: user.uuid,
486         email: user.email,
487         first_name: user.first_name,
488         last_name: user.last_name,
489         is_active: user.is_active,
490         is_admin: user.is_admin,
491         prefs: user.prefs
492       }
493
494       if !request.format.json? and request.method.in? ['GET', 'HEAD']
495         # Repeat this request with api_token in the (new) session
496         # cookie instead of the query string.  This prevents API
497         # tokens from appearing in (and being inadvisedly copied
498         # and pasted from) browser Location bars.
499         redirect_to strip_token_from_path(request.fullpath)
500         true
501       else
502         false
503       end
504     ensure
505       Thread.current[:arvados_api_token] = nil
506     end
507   end
508
509   # Save the session API token in thread-local storage, and yield.
510   # This method also takes care of session setup if the request
511   # provides a valid api_token parameter.
512   # If a token is unavailable or expired, the block is still run, with
513   # a nil token.
514   def set_thread_api_token
515     if Thread.current[:arvados_api_token]
516       yield   # An API token has already been found - pass it through.
517       return
518     elsif setup_user_session
519       return  # A new session was set up and received a response.
520     end
521
522     begin
523       load_api_token(session[:arvados_api_token])
524       yield
525     rescue ArvadosApiClient::NotLoggedInException
526       # If we got this error with a token, it must've expired.
527       # Retry the request without a token.
528       unless Thread.current[:arvados_api_token].nil?
529         load_api_token(nil)
530         yield
531       end
532     ensure
533       # Remove token in case this Thread is used for anything else.
534       load_api_token(nil)
535     end
536   end
537
538   # Redirect to login/welcome if client provided expired API token (or none at all)
539   def require_thread_api_token
540     if Thread.current[:arvados_api_token]
541       yield
542     elsif session[:arvados_api_token]
543       # Expired session. Clear it before refreshing login so that,
544       # if this login procedure fails, we end up showing the "please
545       # log in" page instead of getting stuck in a redirect loop.
546       session.delete :arvados_api_token
547       redirect_to_login
548     else
549       redirect_to welcome_users_path(return_to: request.fullpath)
550     end
551   end
552
553   def ensure_current_user_is_admin
554     unless current_user and current_user.is_admin
555       @errors = ['Permission denied']
556       self.render_error status: 401
557     end
558   end
559
560   helper_method :unsigned_user_agreements
561   def unsigned_user_agreements
562     @signed_ua_uuids ||= UserAgreement.signatures.map &:head_uuid
563     @unsigned_user_agreements ||= UserAgreement.all.map do |ua|
564       if not @signed_ua_uuids.index ua.uuid
565         Collection.find(ua.uuid)
566       end
567     end.compact
568   end
569
570   def check_user_agreements
571     if current_user && !current_user.is_active
572       if not current_user.is_invited
573         return redirect_to inactive_users_path(return_to: request.fullpath)
574       end
575       if unsigned_user_agreements.empty?
576         # No agreements to sign. Perhaps we just need to ask?
577         current_user.activate
578         if !current_user.is_active
579           logger.warn "#{current_user.uuid.inspect}: " +
580             "No user agreements to sign, but activate failed!"
581         end
582       end
583       if !current_user.is_active
584         redirect_to user_agreements_path(return_to: request.fullpath)
585       end
586     end
587     true
588   end
589
590   def check_user_profile
591     if request.method.downcase != 'get' || params[:partial] ||
592        params[:tab_pane] || params[:action_method] ||
593        params[:action] == 'setup_popup'
594       return true
595     end
596
597     if missing_required_profile?
598       redirect_to profile_user_path(current_user.uuid, return_to: request.fullpath)
599     end
600     true
601   end
602
603   helper_method :missing_required_profile?
604   def missing_required_profile?
605     missing_required = false
606
607     profile_config = Rails.configuration.user_profile_form_fields
608     if current_user && profile_config
609       current_user_profile = current_user.prefs[:profile]
610       profile_config.kind_of?(Array) && profile_config.andand.each do |entry|
611         if entry['required']
612           if !current_user_profile ||
613              !current_user_profile[entry['key'].to_sym] ||
614              current_user_profile[entry['key'].to_sym].empty?
615             missing_required = true
616             break
617           end
618         end
619       end
620     end
621
622     missing_required
623   end
624
625   def select_theme
626     return Rails.configuration.arvados_theme
627   end
628
629   @@notification_tests = []
630
631   @@notification_tests.push lambda { |controller, current_user|
632     AuthorizedKey.limit(1).where(authorized_user_uuid: current_user.uuid).each do
633       return nil
634     end
635     return lambda { |view|
636       view.render partial: 'notifications/ssh_key_notification'
637     }
638   }
639
640   @@notification_tests.push lambda { |controller, current_user|
641     Collection.limit(1).where(created_by: current_user.uuid).each do
642       return nil
643     end
644     return lambda { |view|
645       view.render partial: 'notifications/collections_notification'
646     }
647   }
648
649   @@notification_tests.push lambda { |controller, current_user|
650     PipelineInstance.limit(1).where(created_by: current_user.uuid).each do
651       return nil
652     end
653     return lambda { |view|
654       view.render partial: 'notifications/pipelines_notification'
655     }
656   }
657
658   def check_user_notifications
659     return if params['tab_pane']
660
661     @notification_count = 0
662     @notifications = []
663
664     if current_user.andand.is_active
665       @showallalerts = false
666       @@notification_tests.each do |t|
667         a = t.call(self, current_user)
668         if a
669           @notification_count += 1
670           @notifications.push a
671         end
672       end
673     end
674
675     if @notification_count == 0
676       @notification_count = ''
677     end
678   end
679
680   helper_method :all_projects
681   def all_projects
682     @all_projects ||= Group.
683       filter([['group_class','=','project']]).order('name')
684   end
685
686   helper_method :my_projects
687   def my_projects
688     return @my_projects if @my_projects
689     @my_projects = []
690     root_of = {}
691     all_projects.each do |g|
692       root_of[g.uuid] = g.owner_uuid
693       @my_projects << g
694     end
695     done = false
696     while not done
697       done = true
698       root_of = root_of.each_with_object({}) do |(child, parent), h|
699         if root_of[parent]
700           h[child] = root_of[parent]
701           done = false
702         else
703           h[child] = parent
704         end
705       end
706     end
707     @my_projects = @my_projects.select do |g|
708       root_of[g.uuid] == current_user.uuid
709     end
710   end
711
712   helper_method :projects_shared_with_me
713   def projects_shared_with_me
714     my_project_uuids = my_projects.collect &:uuid
715     all_projects.reject { |x| x.uuid.in? my_project_uuids }
716   end
717
718   helper_method :recent_jobs_and_pipelines
719   def recent_jobs_and_pipelines
720     (Job.limit(10) |
721      PipelineInstance.limit(10)).
722       sort_by do |x|
723       (x.finished_at || x.started_at rescue nil) || x.modified_at || x.created_at
724     end.reverse
725   end
726
727   helper_method :running_pipelines
728   def running_pipelines
729     pi = PipelineInstance.order(["started_at asc", "created_at asc"]).filter([["state", "in", ["RunningOnServer", "RunningOnClient"]]])
730     jobs = {}
731     pi.each do |pl|
732       pl.components.each do |k,v|
733         if v.is_a? Hash and v[:job]
734           jobs[v[:job][:uuid]] = {}
735         end
736       end
737     end
738
739     if jobs.keys.any?
740       Job.filter([["uuid", "in", jobs.keys]]).each do |j|
741         jobs[j[:uuid]] = j
742       end
743
744       pi.each do |pl|
745         pl.components.each do |k,v|
746           if v.is_a? Hash and v[:job]
747             v[:job] = jobs[v[:job][:uuid]]
748           end
749         end
750       end
751     end
752
753     pi
754   end
755
756   helper_method :finished_pipelines
757   def finished_pipelines lim
758     PipelineInstance.limit(lim).order(["finished_at desc"]).filter([["state", "in", ["Complete", "Failed", "Paused"]], ["finished_at", "!=", nil]])
759   end
760
761   helper_method :recent_collections
762   def recent_collections lim
763     c = Collection.limit(lim).order(["modified_at desc"]).filter([["owner_uuid", "is_a", "arvados#group"]])
764     own = {}
765     Group.filter([["uuid", "in", c.map(&:owner_uuid)]]).each do |g|
766       own[g[:uuid]] = g
767     end
768     {collections: c, owners: own}
769   end
770
771   helper_method :my_project_tree
772   def my_project_tree
773     build_project_trees
774     @my_project_tree
775   end
776
777   helper_method :shared_project_tree
778   def shared_project_tree
779     build_project_trees
780     @shared_project_tree
781   end
782
783   def build_project_trees
784     return if @my_project_tree and @shared_project_tree
785     parent_of = {current_user.uuid => 'me'}
786     all_projects.each do |ob|
787       parent_of[ob.uuid] = ob.owner_uuid
788     end
789     children_of = {false => [], 'me' => [current_user]}
790     all_projects.each do |ob|
791       if ob.owner_uuid != current_user.uuid and
792           not parent_of.has_key? ob.owner_uuid
793         parent_of[ob.uuid] = false
794       end
795       children_of[parent_of[ob.uuid]] ||= []
796       children_of[parent_of[ob.uuid]] << ob
797     end
798     buildtree = lambda do |children_of, root_uuid=false|
799       tree = {}
800       children_of[root_uuid].andand.each do |ob|
801         tree[ob] = buildtree.call(children_of, ob.uuid)
802       end
803       tree
804     end
805     sorted_paths = lambda do |tree, depth=0|
806       paths = []
807       tree.keys.sort_by { |ob|
808         ob.is_a?(String) ? ob : ob.friendly_link_name
809       }.each do |ob|
810         paths << {object: ob, depth: depth}
811         paths += sorted_paths.call tree[ob], depth+1
812       end
813       paths
814     end
815     @my_project_tree =
816       sorted_paths.call buildtree.call(children_of, 'me')
817     @shared_project_tree =
818       sorted_paths.call({'Projects shared with me' =>
819                           buildtree.call(children_of, false)})
820   end
821
822   helper_method :get_object
823   def get_object uuid
824     if @get_object.nil? and @objects
825       @get_object = @objects.each_with_object({}) do |object, h|
826         h[object.uuid] = object
827       end
828     end
829     @get_object ||= {}
830     @get_object[uuid]
831   end
832
833   helper_method :project_breadcrumbs
834   def project_breadcrumbs
835     crumbs = []
836     current = @name_link || @object
837     while current
838       # Halt if a group ownership loop is detected. API should refuse
839       # to produce this state, but it could still arise from a race
840       # condition when group ownership changes between our find()
841       # queries.
842       break if crumbs.collect(&:uuid).include? current.uuid
843
844       if current.is_a?(Group) and current.group_class == 'project'
845         crumbs.prepend current
846       end
847       if current.is_a? Link
848         current = Group.find?(current.tail_uuid)
849       else
850         current = Group.find?(current.owner_uuid)
851       end
852     end
853     crumbs
854   end
855
856   helper_method :current_project_uuid
857   def current_project_uuid
858     if @object.is_a? Group and @object.group_class == 'project'
859       @object.uuid
860     elsif @name_link.andand.tail_uuid
861       @name_link.tail_uuid
862     elsif @object and resource_class_for_uuid(@object.owner_uuid) == Group
863       @object.owner_uuid
864     else
865       nil
866     end
867   end
868
869   # helper method to get links for given object or uuid
870   helper_method :links_for_object
871   def links_for_object object_or_uuid
872     raise ArgumentError, 'No input argument' unless object_or_uuid
873     preload_links_for_objects([object_or_uuid])
874     uuid = object_or_uuid.is_a?(String) ? object_or_uuid : object_or_uuid.uuid
875     @all_links_for[uuid] ||= []
876   end
877
878   # helper method to preload links for given objects and uuids
879   helper_method :preload_links_for_objects
880   def preload_links_for_objects objects_and_uuids
881     @all_links_for ||= {}
882
883     raise ArgumentError, 'Argument is not an array' unless objects_and_uuids.is_a? Array
884     return @all_links_for if objects_and_uuids.empty?
885
886     uuids = objects_and_uuids.collect { |x| x.is_a?(String) ? x : x.uuid }
887
888     # if already preloaded for all of these uuids, return
889     if not uuids.select { |x| @all_links_for[x].nil? }.any?
890       return @all_links_for
891     end
892
893     uuids.each do |x|
894       @all_links_for[x] = []
895     end
896
897     # TODO: make sure we get every page of results from API server
898     Link.filter([['head_uuid', 'in', uuids]]).each do |link|
899       @all_links_for[link.head_uuid] << link
900     end
901     @all_links_for
902   end
903
904   # helper method to get a certain number of objects of a specific type
905   # this can be used to replace any uses of: "dataclass.limit(n)"
906   helper_method :get_n_objects_of_class
907   def get_n_objects_of_class dataclass, size
908     @objects_map_for ||= {}
909
910     raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class and dataclass < ArvadosBase
911     raise ArgumentError, 'Argument is not a valid limit size' unless (size && size>0)
912
913     # if the objects_map_for has a value for this dataclass, and the
914     # size used to retrieve those objects is equal, return it
915     size_key = "#{dataclass.name}_size"
916     if @objects_map_for[dataclass.name] && @objects_map_for[size_key] &&
917         (@objects_map_for[size_key] == size)
918       return @objects_map_for[dataclass.name]
919     end
920
921     @objects_map_for[size_key] = size
922     @objects_map_for[dataclass.name] = dataclass.limit(size)
923   end
924
925   # helper method to get collections for the given uuid
926   helper_method :collections_for_object
927   def collections_for_object uuid
928     raise ArgumentError, 'No input argument' unless uuid
929     preload_collections_for_objects([uuid])
930     @all_collections_for[uuid] ||= []
931   end
932
933   # helper method to preload collections for the given uuids
934   helper_method :preload_collections_for_objects
935   def preload_collections_for_objects uuids
936     @all_collections_for ||= {}
937
938     raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
939     return @all_collections_for if uuids.empty?
940
941     # if already preloaded for all of these uuids, return
942     if not uuids.select { |x| @all_collections_for[x].nil? }.any?
943       return @all_collections_for
944     end
945
946     uuids.each do |x|
947       @all_collections_for[x] = []
948     end
949
950     # TODO: make sure we get every page of results from API server
951     Collection.where(uuid: uuids).each do |collection|
952       @all_collections_for[collection.uuid] << collection
953     end
954     @all_collections_for
955   end
956
957   # helper method to get log collections for the given log
958   helper_method :log_collections_for_object
959   def log_collections_for_object log
960     raise ArgumentError, 'No input argument' unless log
961
962     preload_log_collections_for_objects([log])
963
964     uuid = log
965     fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
966     if fixup && fixup.size>1
967       uuid = fixup[1]
968     end
969
970     @all_log_collections_for[uuid] ||= []
971   end
972
973   # helper method to preload collections for the given uuids
974   helper_method :preload_log_collections_for_objects
975   def preload_log_collections_for_objects logs
976     @all_log_collections_for ||= {}
977
978     raise ArgumentError, 'Argument is not an array' unless logs.is_a? Array
979     return @all_log_collections_for if logs.empty?
980
981     uuids = []
982     logs.each do |log|
983       fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
984       if fixup && fixup.size>1
985         uuids << fixup[1]
986       else
987         uuids << log
988       end
989     end
990
991     # if already preloaded for all of these uuids, return
992     if not uuids.select { |x| @all_log_collections_for[x].nil? }.any?
993       return @all_log_collections_for
994     end
995
996     uuids.each do |x|
997       @all_log_collections_for[x] = []
998     end
999
1000     # TODO: make sure we get every page of results from API server
1001     Collection.where(uuid: uuids).each do |collection|
1002       @all_log_collections_for[collection.uuid] << collection
1003     end
1004     @all_log_collections_for
1005   end
1006
1007   # helper method to get object of a given dataclass and uuid
1008   helper_method :object_for_dataclass
1009   def object_for_dataclass dataclass, uuid
1010     raise ArgumentError, 'No input argument dataclass' unless (dataclass && uuid)
1011     preload_objects_for_dataclass(dataclass, [uuid])
1012     @objects_for[uuid]
1013   end
1014
1015   # helper method to preload objects for given dataclass and uuids
1016   helper_method :preload_objects_for_dataclass
1017   def preload_objects_for_dataclass dataclass, uuids
1018     @objects_for ||= {}
1019
1020     raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class
1021     raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
1022
1023     return @objects_for if uuids.empty?
1024
1025     # if already preloaded for all of these uuids, return
1026     if not uuids.select { |x| @objects_for[x].nil? }.any?
1027       return @objects_for
1028     end
1029
1030     dataclass.where(uuid: uuids).each do |obj|
1031       @objects_for[obj.uuid] = obj
1032     end
1033     @objects_for
1034   end
1035
1036   def wiselinks_layout
1037     'body'
1038   end
1039 end