From 10486fd2453c92a62c0a97491259d1391e5cb872 Mon Sep 17 00:00:00 2001 From: radhika Date: Thu, 5 Jun 2014 08:35:11 -0400 Subject: [PATCH] 2871: add preload_objects_for_dataclass method to application_controller --- .../app/controllers/application_controller.rb | 26 +++++++++++++++++-- .../app/helpers/application_helper.rb | 8 ++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/apps/workbench/app/controllers/application_controller.rb b/apps/workbench/app/controllers/application_controller.rb index 3e458bf33c..36cdb842f3 100644 --- a/apps/workbench/app/controllers/application_controller.rb +++ b/apps/workbench/app/controllers/application_controller.rb @@ -477,8 +477,8 @@ class ApplicationController < ActionController::Base # helper method to get a certain number of objects of a specific type # this can be used to replace any uses of: "dataclass.limit(n)" - helper_method :get_objects_of_type - def get_objects_of_type dataclass, size + helper_method :get_n_objects_of_class + def get_n_objects_of_class dataclass, size # if the objects_map_for has a value for this dataclass, and the size used # to retrieve those objects is greater than equal to size, return it size_key = "#{dataclass}_size" @@ -554,4 +554,26 @@ class ApplicationController < ActionController::Base end end + # helper method to get object of a given dataclass and uuid + helper_method :object_for_dataclass + def object_for_dataclass dataclass, uuid + preload_objects_for_dataclass(dataclass, [uuid]) + @objects_for[uuid] + end + + # helper method to preload objects for given dataclass and uuids + helper_method :preload_objects_for_dataclass + def preload_objects_for_dataclass dataclass, uuids + @objects_for ||= {} + + # if already preloaded for all of these uuids, return + if not uuids.select { |x| @objects_for[x].nil? }.any? + return + end + + dataclass.where(uuid: uuids).each do |obj| + @objects_for[obj.uuid] = obj + end + end + end diff --git a/apps/workbench/app/helpers/application_helper.rb b/apps/workbench/app/helpers/application_helper.rb index 4c9d57595d..937c204148 100644 --- a/apps/workbench/app/helpers/application_helper.rb +++ b/apps/workbench/app/helpers/application_helper.rb @@ -89,7 +89,11 @@ module ApplicationHelper link_name = attrvalue.friendly_link_name else begin - link_name = collections_for_object(link_uuid).andand.first.andand.friendly_link_name + if resource_class.name == 'Collection' + link_name = collections_for_object(link_uuid).first.friendly_link_name + else + link_name = object_for_dataclass(resource_class, link_uuid).friendly_link_name + end rescue RuntimeError # If that lookup failed, the link will too. So don't make one. return attrvalue @@ -247,7 +251,7 @@ module ApplicationHelper attrtext = attrvalue if dataclass and dataclass.is_a? Class - objects = get_objects_of_type dataclass, 10 + objects = get_n_objects_of_class dataclass, 10 objects.each do |item| items << item preload_uuids << item.uuid -- 2.30.2