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

59 lines
1.5 KiB
Ruby
Raw Normal View History

module TopicsHelper
## this one is for building our custom JSON autocomplete format for typeahead
def autocomplete_array_json(topics)
temp = []
topics.each do |t|
topic = {}
topic['id'] = t.id
topic['label'] = t.name
2014-02-05 03:03:24 +00:00
topic['value'] = t.name
topic['description'] = t.desc.truncate(35) # make this return matched results
topic['type'] = t.metacode.name
2014-05-30 15:00:31 +00:00
topic['typeImageURL'] = t.metacode.icon
topic['permission'] = t.permission
topic['mapCount'] = t.maps.count
topic['synapseCount'] = t.synapses.count
topic['originator'] = t.user.name
topic['rtype'] = "topic"
temp.push topic
end
return temp
end
2014-08-12 22:14:04 +00:00
#find all nodes in any given nodes network
def network(node, array, count)
# recurse starting with a node to find all connected nodes and return an array of topics that constitutes the starting nodes network
# if the array of nodes is empty initialize it
if array.nil?
array = Array.new
end
# add the node to the array
array.push(node)
if count == 0
return array
end
count = count - 1
# check if each relative is already in the array and if not, call the network function again
if not node.relatives.empty?
if (node.relatives-array).empty?
return array
else
(node.relatives-array).each do |relative|
array = (array | network(relative, array, count))
end
return array
end
elsif node.relatives.empty?
return array
end
end
end