module Fiona7 class WidgetResolver def initialize(widget_pool_value, obj_repository) self.value = widget_pool_value self.obj_repository = obj_repository self.preload end attr_reader :id_map, :path_map, :instance_map def all self.instance_map.values end protected attr_accessor :value, :obj_repository attr_writer :id_map, :path_map, :instance_map def preload widget_id_to_object_id_map = Hash[ self.value.map {|link| [link["title"], link["destination"]] } ] object_id_to_widget_instance_map = Hash[ self.obj_repository.find_many(widget_id_to_object_id_map.values).compact.map {|obj| [obj.id, obj] } ] self.id_map = widget_id_to_object_id_map.dup self.path_map = Hash[ widget_id_to_object_id_map.map {|widget_id, obj_id| path = object_id_to_widget_instance_map[obj_id].try(:path) next unless path [widget_id, object_id_to_widget_instance_map[obj_id].try(:path)] }.compact ] self.instance_map = Hash[ widget_id_to_object_id_map.map {|widget_id, obj_id| object = object_id_to_widget_instance_map[obj_id] if !object Rails.logger.error("Unable to find widget with id #{widget_id} (obj_id = #{obj_id})") next end [widget_id, object] }.compact ] end end end