metamaps--metamaps/app/helpers/topics_helper.rb

43 lines
1.4 KiB
Ruby
Raw Permalink Normal View History

2016-09-24 03:00:46 +00:00
# frozen_string_literal: true
module TopicsHelper
## this one is for building our custom JSON autocomplete format for typeahead
def autocomplete_array_json(topics)
2016-09-24 04:27:34 +00:00
topics.map do |t|
is_map = t.is_a?(Map)
2016-10-06 14:32:06 +00:00
metamapMetacode = Metacode.find_by_name('Metamap')
2016-09-24 04:27:34 +00:00
{
id: t.id,
label: t.name,
value: t.name,
description: t.desc ? t.desc&.truncate(70) : '', # make this return matched results
originator: t.user.name,
originatorImage: t.user.image.url(:thirtytwo),
permission: t.permission,
rtype: is_map ? 'map' : 'topic',
inmaps: is_map ? [] : t.inmaps,
2016-10-06 13:27:18 +00:00
inmapsLinks: is_map ? [] : t.inmapsLinks,
2016-10-06 14:32:06 +00:00
type: is_map ? metamapMetacode.name : t.metacode.name,
typeImageURL: is_map ? metamapMetacode.icon : t.metacode.icon,
mapCount: is_map ? 0 : t.maps.count,
synapseCount: is_map ? 0 : t.synapses.count,
2016-09-24 04:27:34 +00:00
}
end
end
2016-09-24 04:27:34 +00:00
# recursively find all nodes in any given nodes network
def network(node, array, count)
array = [] if array.nil?
array.push(node)
2016-09-24 03:00:46 +00:00
return array if count.zero?
# check if each relative is already in the array and if not, call the network function again
2016-09-24 04:27:34 +00:00
remaining_relatives = node.relatives.to_a - array
remaining_relatives.each do |relative|
array = (array | network(relative, array, count - 1))
end
2016-09-24 04:27:34 +00:00
array
end
end