+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
class ApplicationController < ActionController::Base
include ArvadosApiClientHelper
include ApplicationHelper
ERROR_ACTIONS = [:render_error, :render_not_found]
- prepend_before_filter :set_current_request_id, except: ERROR_ACTIONS
- around_filter :thread_clear
- around_filter :set_thread_api_token
+ around_action :thread_clear
+ around_action :set_current_request_id
+ around_action :set_thread_api_token
# Methods that don't require login should
- # skip_around_filter :require_thread_api_token
- around_filter :require_thread_api_token, except: ERROR_ACTIONS
- before_filter :ensure_arvados_api_exists, only: [:index, :show]
- before_filter :set_cache_buster
- before_filter :accept_uuid_as_id_param, except: ERROR_ACTIONS
- before_filter :check_user_agreements, except: ERROR_ACTIONS
- before_filter :check_user_profile, except: ERROR_ACTIONS
- before_filter :load_filters_and_paging_params, except: ERROR_ACTIONS
- before_filter :find_object_by_uuid, except: [:create, :index, :choose] + ERROR_ACTIONS
+ # skip_around_action :require_thread_api_token
+ around_action :require_thread_api_token, except: ERROR_ACTIONS
+ before_action :ensure_arvados_api_exists, only: [:index, :show]
+ before_action :set_cache_buster
+ before_action :accept_uuid_as_id_param, except: ERROR_ACTIONS
+ before_action :check_user_agreements, except: ERROR_ACTIONS
+ before_action :check_user_profile, except: ERROR_ACTIONS
+ before_action :load_filters_and_paging_params, except: ERROR_ACTIONS
+ before_action :find_object_by_uuid, except: [:create, :index, :choose] + ERROR_ACTIONS
theme :select_theme
begin
end
def index
+ @objects = nil if !defined?(@objects)
find_objects_for_index if !@objects
render_index
end
objects = @objects
end
if objects.respond_to?(:result_offset) and
- objects.respond_to?(:result_limit) and
- objects.respond_to?(:items_available)
+ objects.respond_to?(:result_limit)
next_offset = objects.result_offset + objects.result_limit
- if next_offset < objects.items_available
+ if objects.respond_to?(:items_available) and (next_offset < objects.items_available)
next_offset
+ elsif @objects.results.size > 0 and (params[:count] == 'none' or
+ (params[:controller] == 'search' and params[:action] == 'choose'))
+ last_object_class = @objects.last.class
+ if params['last_object_class'].nil? or params['last_object_class'] == last_object_class.to_s
+ next_offset
+ else
+ @objects.select{|obj| obj.class == last_object_class}.size
+ end
else
nil
end
end
def choose
+ @objects = nil if !defined?(@objects)
params[:limit] ||= 40
respond_to do |f|
if params[:partial]
def update
@updates ||= params[@object.resource_param_name.to_sym]
+ if @updates.is_a? ActionController::Parameters
+ @updates = @updates.to_unsafe_hash
+ end
@updates.keys.each do |attr|
if @object.send(attr).is_a? Hash
if @updates[attr].is_a? String
if params[:merge] || params["merge_#{attr}".to_sym]
# Merge provided Hash with current Hash, instead of
# replacing.
+ if @updates[attr].is_a? ActionController::Parameters
+ @updates[attr] = @updates[attr].to_unsafe_hash
+ end
@updates[attr] = @object.send(attr).with_indifferent_access.
deep_merge(@updates[attr].with_indifferent_access)
end
def accept_uuid_as_id_param
- if params[:id] and params[:id].match /\D/
+ if params[:id] and params[:id].match(/\D/)
params[:uuid] = params.delete :id
end
end
Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
end
+ def set_current_request_id
+ response.headers['X-Request-Id'] =
+ Thread.current[:request_id] =
+ "req-" + Random::DEFAULT.rand(2**128).to_s(36)[0..19]
+ yield
+ Thread.current[:request_id] = nil
+ end
+
+ def append_info_to_payload(payload)
+ super
+ payload[:request_id] = response.headers['X-Request-Id']
+ end
+
# Set up the thread with the given API token and associated user object.
def load_api_token(new_token)
Thread.current[:arvados_api_token] = new_token
helper_method :user_notifications
def user_notifications
+ @errors = nil if !defined?(@errors)
return [] if @errors or not current_user.andand.is_active or not Rails.configuration.show_user_notifications
@notifications ||= @@notification_tests.map do |t|
t.call(self, current_user)
helper_method :my_starred_projects
def my_starred_projects user
- return if @starred_projects
+ return if defined?(@starred_projects) && @starred_projects
links = Link.filter([['tail_uuid', '=', user.uuid],
['link_class', '=', 'star'],
['head_uuid', 'is_a', 'arvados#group']]).select(%w(head_uuid))
# That is: get toplevel projects under home, get subprojects of
# these projects, and so on until we hit the limit.
def my_wanted_projects(user, page_size=100)
- return @my_wanted_projects if @my_wanted_projects
+ return @my_wanted_projects if defined?(@my_wanted_projects) && @my_wanted_projects
from_top = []
uuids = [user.uuid]
end
def build_my_wanted_projects_tree(user, page_size=100)
- return @my_wanted_projects_tree if @my_wanted_projects_tree
+ return @my_wanted_projects_tree if defined?(@my_wanted_projects_tree) && @my_wanted_projects_tree
parent_of = {user.uuid => 'me'}
my_wanted_projects(user, page_size).each do |ob|
children_of[parent_of[ob.uuid]] ||= []
children_of[parent_of[ob.uuid]] << ob
end
- buildtree = lambda do |children_of, root_uuid=false|
+ buildtree = lambda do |chldrn_of, root_uuid=false|
tree = {}
- children_of[root_uuid].andand.each do |ob|
- tree[ob] = buildtree.call(children_of, ob.uuid)
+ chldrn_of[root_uuid].andand.each do |ob|
+ tree[ob] = buildtree.call(chldrn_of, ob.uuid)
end
tree
end
@objects_for[obj.name] = obj
end
else
+ key_prefix = "request_#{Thread.current.object_id}_#{dataclass.to_s}_"
dataclass.where(uuid: uuids).each do |obj|
@objects_for[obj.uuid] = obj
+ if dataclass == Collection
+ # The collecions#index defaults to "all attributes except manifest_text"
+ # Hence, this object is not suitable for preloading the find() cache.
+ else
+ Rails.cache.write(key_prefix + obj.uuid, obj.as_json)
+ end
end
end
@objects_for
end
end
- def wiselinks_layout
- 'body'
+ # helper method to get the names of collection files selected
+ helper_method :selected_collection_files
+ def selected_collection_files params
+ link_uuids, coll_ids = params["selection"].partition do |sel_s|
+ ArvadosBase::resource_class_for_uuid(sel_s) == Link
+ end
+
+ unless link_uuids.empty?
+ Link.select([:head_uuid]).where(uuid: link_uuids).each do |link|
+ if ArvadosBase::resource_class_for_uuid(link.head_uuid) == Collection
+ coll_ids << link.head_uuid
+ end
+ end
+ end
+
+ uuids = []
+ pdhs = []
+ source_paths = Hash.new { |hash, key| hash[key] = [] }
+ coll_ids.each do |coll_id|
+ if m = CollectionsHelper.match(coll_id)
+ key = m[1] + m[2]
+ pdhs << key
+ source_paths[key] << m[4]
+ elsif m = CollectionsHelper.match_uuid_with_optional_filepath(coll_id)
+ key = m[1]
+ uuids << key
+ source_paths[key] << m[4]
+ end
+ end
+
+ unless pdhs.empty?
+ Collection.where(portable_data_hash: pdhs.uniq).
+ select([:uuid, :portable_data_hash]).each do |coll|
+ unless source_paths[coll.portable_data_hash].empty?
+ uuids << coll.uuid
+ source_paths[coll.uuid] = source_paths.delete(coll.portable_data_hash)
+ end
+ end
+ end
+
+ [uuids, source_paths]
end
- def set_current_request_id
- # Request ID format: '<timestamp>-<9_digits_random_number>'
- current_request_id = "#{Time.new.to_i}-#{sprintf('%09d', rand(0..10**9-1))}"
- Thread.current[:current_request_id] = current_request_id
+ def wiselinks_layout
+ 'body'
end
end