2012-09-23 02:39:12 +00:00
|
|
|
module ItemsHelper
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2012-10-19 21:26:46 +00:00
|
|
|
#find all nodes in any given nodes network
|
2012-11-01 19:59:54 +00:00
|
|
|
def network(node, array, count)
|
2012-10-19 21:26:46 +00:00
|
|
|
# recurse starting with a node to find all connected nodes and return an array of items that constitutes the starting nodes network
|
2012-10-19 04:06:16 +00:00
|
|
|
|
2012-10-19 21:26:46 +00:00
|
|
|
# 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)
|
|
|
|
|
2012-11-01 19:59:54 +00:00
|
|
|
if count == 0
|
|
|
|
return array
|
|
|
|
end
|
|
|
|
|
|
|
|
count = count - 1
|
|
|
|
|
2012-10-19 21:26:46 +00:00
|
|
|
# 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|
|
2012-11-01 19:59:54 +00:00
|
|
|
array = (array | network(relative, array, count))
|
2012-10-19 21:26:46 +00:00
|
|
|
end
|
|
|
|
return array
|
|
|
|
end
|
|
|
|
|
|
|
|
elsif node.relatives.empty?
|
|
|
|
return array
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#return a json object containing all of a users added synapses
|
2012-10-27 08:30:56 +00:00
|
|
|
def synapses_as_json(current, synapses)
|
2012-10-19 21:26:46 +00:00
|
|
|
Jbuilder.encode do |json|
|
|
|
|
@items = Array.new
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
synapses.each do |synapse|
|
|
|
|
@items.push(synapse.item1) if (not @items.include?(synapse.item1)) && synapse.item1.authorize_to_view(current)
|
|
|
|
@items.push(synapse.item2) if (not @items.include?(synapse.item2)) && synapse.item2.authorize_to_view(current)
|
2012-10-19 21:26:46 +00:00
|
|
|
end
|
2012-10-19 04:06:16 +00:00
|
|
|
|
2012-10-19 21:26:46 +00:00
|
|
|
json.array!(@items) do |item|
|
2012-10-27 08:30:56 +00:00
|
|
|
json.adjacencies item.synapses2.delete_if{|synapse| not @items.include?(Item.find_by_id(synapse.node1_id))} do |json, synapse|
|
2012-10-19 21:26:46 +00:00
|
|
|
json.nodeTo synapse.node1_id
|
|
|
|
json.nodeFrom synapse.node2_id
|
|
|
|
|
|
|
|
@synapsedata = Hash.new
|
|
|
|
@synapsedata['$desc'] = synapse.desc
|
2012-12-21 00:24:27 +00:00
|
|
|
@synapsedata['$showDesc'] = false
|
|
|
|
@synapsedata['$category'] = synapse.category
|
|
|
|
@synapsedata['$id'] = synapse.id
|
|
|
|
@synapsedata['$userid'] = synapse.user.id
|
|
|
|
@synapsedata['$username'] = synapse.user.name
|
|
|
|
@synapsedata['$direction'] = [synapse.node1_id.to_s(), synapse.node2_id.to_s()]
|
2012-10-19 21:26:46 +00:00
|
|
|
json.data @synapsedata
|
|
|
|
end
|
|
|
|
|
2012-12-22 08:32:12 +00:00
|
|
|
@inmaps = Array.new
|
|
|
|
item.maps.each do |map|
|
|
|
|
@inmaps.push(map.id)
|
|
|
|
end
|
|
|
|
|
2012-10-19 21:26:46 +00:00
|
|
|
@itemdata = Hash.new
|
|
|
|
@itemdata['$desc'] = item.desc
|
|
|
|
@itemdata['$link'] = item.link
|
|
|
|
@itemdata['$itemcatname'] = item.item_category.name
|
2012-12-22 08:32:12 +00:00
|
|
|
@itemdata['$inmaps'] = @inmaps
|
2012-10-26 10:04:52 +00:00
|
|
|
@itemdata['$userid'] = item.user.id
|
|
|
|
@itemdata['$username'] = item.user.name
|
2012-10-19 21:26:46 +00:00
|
|
|
json.data @itemdata
|
|
|
|
json.id item.id
|
|
|
|
json.name item.name
|
|
|
|
end
|
|
|
|
end
|
2012-10-19 04:06:16 +00:00
|
|
|
end
|
2012-10-27 08:30:56 +00:00
|
|
|
|
2012-12-14 18:31:39 +00:00
|
|
|
def all_as_json(current, user)
|
|
|
|
|
|
|
|
# current is current user
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
Jbuilder.encode do |json|
|
2012-12-14 18:31:39 +00:00
|
|
|
if user.nil?
|
|
|
|
@items = Item.visibleToUser(current, nil)
|
|
|
|
@synapses = Synapse.visibleToUser(current, nil)
|
|
|
|
else
|
|
|
|
@items = Item.visibleToUser(current, user)
|
|
|
|
@synapses = Synapse.visibleToUser(current, user)
|
|
|
|
end
|
2012-10-27 08:30:56 +00:00
|
|
|
|
|
|
|
json.array!(@items) do |item|
|
|
|
|
json.adjacencies item.synapses2.delete_if{|synapse| (not @items.include?(Item.find_by_id(synapse.node1_id))) || (not @synapses.include?(synapse))} do |json, synapse|
|
|
|
|
json.nodeTo synapse.node1_id
|
|
|
|
json.nodeFrom synapse.node2_id
|
|
|
|
|
|
|
|
@synapsedata = Hash.new
|
|
|
|
@synapsedata['$desc'] = synapse.desc
|
|
|
|
@synapsedata['$category'] = synapse.category
|
|
|
|
@synapsedata['$userid'] = synapse.user.id
|
|
|
|
@synapsedata['$username'] = synapse.user.name
|
|
|
|
json.data @synapsedata
|
|
|
|
end
|
|
|
|
|
2012-12-22 08:32:12 +00:00
|
|
|
@inmaps = Array.new
|
|
|
|
item.maps.each do |map|
|
|
|
|
@inmaps.push(map.id)
|
|
|
|
end
|
|
|
|
|
2012-10-27 08:30:56 +00:00
|
|
|
@itemdata = Hash.new
|
|
|
|
@itemdata['$desc'] = item.desc
|
|
|
|
@itemdata['$link'] = item.link
|
|
|
|
@itemdata['$itemcatname'] = item.item_category.name
|
2012-12-22 08:32:12 +00:00
|
|
|
@itemdata['$inmaps'] = @inmaps
|
2012-10-27 08:30:56 +00:00
|
|
|
@itemdata['$userid'] = item.user.id
|
|
|
|
@itemdata['$username'] = item.user.name
|
|
|
|
|
|
|
|
json.data @itemdata
|
|
|
|
json.id item.id
|
|
|
|
json.name item.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-19 04:06:16 +00:00
|
|
|
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|