2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2013-01-01 22:45:35 +00:00
|
|
|
module TopicsHelper
|
2014-01-29 03:46:58 +00:00
|
|
|
## 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|
|
2016-10-06 13:12:01 +00:00
|
|
|
is_map = t.is_a?(Map)
|
2017-01-24 00:30:13 +00:00
|
|
|
metamap_metacode = 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),
|
2016-10-06 13:12:01 +00:00
|
|
|
permission: t.permission,
|
|
|
|
|
|
|
|
rtype: is_map ? 'map' : 'topic',
|
2016-10-23 14:11:38 +00:00
|
|
|
inmaps: is_map ? [] : t.inmaps(current_user),
|
|
|
|
inmapsLinks: is_map ? [] : t.inmapsLinks(current_user),
|
2017-01-24 00:30:13 +00:00
|
|
|
type: is_map ? metamap_metacode.name : t.metacode.name,
|
|
|
|
typeImageURL: is_map ? metamap_metacode.icon : t.metacode.icon,
|
2016-10-06 13:12:01 +00:00
|
|
|
mapCount: is_map ? 0 : t.maps.count,
|
2016-12-13 03:28:10 +00:00
|
|
|
synapseCount: is_map ? 0 : t.synapses.count
|
2016-09-24 04:27:34 +00:00
|
|
|
}
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-24 04:27:34 +00:00
|
|
|
# recursively find all nodes in any given nodes network
|
2013-01-01 22:45:35 +00:00
|
|
|
def network(node, array, count)
|
2016-07-26 00:14:23 +00:00
|
|
|
array = [] if array.nil?
|
|
|
|
array.push(node)
|
2016-09-24 03:00:46 +00:00
|
|
|
return array if count.zero?
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
# 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))
|
2016-07-26 00:14:23 +00:00
|
|
|
end
|
2016-09-24 04:27:34 +00:00
|
|
|
|
|
|
|
array
|
2013-01-01 22:45:35 +00:00
|
|
|
end
|
|
|
|
end
|