updated my-synapses to only show synapses that you've made between items, even if the items weren't added by you. added function that recurses through data finding all items connected to a starting point. set item show page to return that kind of network

This commit is contained in:
Connor Turland 2012-10-19 17:26:46 -04:00
parent bd396e93d8
commit b6260d7015
7 changed files with 127 additions and 39 deletions

View file

@ -1,4 +1,5 @@
class MainController < ApplicationController
include ItemsHelper
before_filter :require_user, only: [:userobjects]
respond_to :html, :js, :json
@ -27,9 +28,12 @@ class MainController < ApplicationController
def usersynapses
@user = current_user
@all = @user.synapses
@synapsesjson = usersynapses_as_json(@user).html_safe
respond_with(@all)
respond_to do |format|
format.html
format.json { respond_with(@synapsesjson) }
end
end
end

View file

@ -25,17 +25,13 @@ class SynapsesController < ApplicationController
def show
@synapse = Synapse.find(params[:id])
@node1 = nil
@node2 = nil
if @synapse
@node1 = @synapse.item1
@node2 = @synapse.item2
@synapsejson = @synapse.self_as_json.html_safe
end
respond_to do |format|
format.html { respond_with(@synapse, @node1, @node2) }
# format.json { respond_with(@relatives) }
format.html
format.json { respond_with(@synapsejson) }
end
end
@ -71,7 +67,7 @@ class SynapsesController < ApplicationController
respond_with(@synapse, @items)
end
# PUT /actions/:id
# PUT /synapses/:id
def update
@synapse = Synapse.find_by_id(params[:id])

View file

@ -1,10 +1,64 @@
module ItemsHelper
def network(node)
#find all nodes in any given nodes network
def network(node, array)
# recurse starting with a node to find all connected nodes and return an array of items that constitutes the starting nodes network
@test = Array.new
@test.push(node)
# 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)
# 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))
end
return array
end
elsif node.relatives.empty?
return array
end
end
#return a json object containing all of a users added synapses
def usersynapses_as_json(user)
Jbuilder.encode do |json|
@synapses = user.synapses
@items = Array.new
@synapses.each do |synapse|
@items.push(synapse.item1) if not @items.include?(synapse.item1)
@items.push(synapse.item2) if not @items.include?(synapse.item2)
end
json.array!(@items) do |item|
json.adjacencies item.synapses2.delete_if{|synapse| not synapse.user == user} do |json, synapse|
json.nodeTo synapse.node1_id
json.nodeFrom synapse.node2_id
@synapsedata = Hash.new
@synapsedata['$desc'] = synapse.desc
@synapsedata['$category'] = synapse.category
json.data @synapsedata
end
@itemdata = Hash.new
@itemdata['$desc'] = item.desc
@itemdata['$link'] = item.link
@itemdata['$itemcatname'] = item.item_category.name
json.data @itemdata
json.id item.id
json.name item.name
end
end
end

View file

@ -1,4 +1,5 @@
class Item < ActiveRecord::Base
include ItemsHelper
belongs_to :user
@ -44,11 +45,12 @@ belongs_to :item_category
end
end
#build a json object of everything connected to a specified node
def map_as_json
Jbuilder.encode do |json|
@single = Array.new
@single.push(self)
@items = @single + self.relatives
@items = network(self,nil)
json.array!(@items) do |item|
json.adjacencies item.synapses2.delete_if{|synapse| not @items.include?(Item.find_by_id(synapse.node1_id))} do |json, synapse|

View file

@ -5,4 +5,32 @@ belongs_to :user
belongs_to :item1, :class_name => "Item", :foreign_key => "node1_id"
belongs_to :item2, :class_name => "Item", :foreign_key => "node2_id"
def self_as_json
Jbuilder.encode do |json|
@items = Array.new
@items.push(self.item1)
@items.push(self.item2)
json.array!(@items) do |item|
json.adjacencies item.synapses2.delete_if{|synapse| not @items.include?(Item.find_by_id(synapse.node1_id))} do |json, synapse|
json.nodeTo synapse.node1_id
json.nodeFrom synapse.node2_id
@synapsedata = Hash.new
@synapsedata['$desc'] = synapse.desc
@synapsedata['$category'] = synapse.category
json.data @synapsedata
end
@itemdata = Hash.new
@itemdata['$desc'] = item.desc
@itemdata['$link'] = item.link
@itemdata['$itemcatname'] = item.item_category.name
json.data @itemdata
json.id item.id
json.name item.name
end
end
end
end

View file

@ -1,9 +1,16 @@
<div class="everything" id="everything">
<% @all.each do |object| %>
<%= render object %>
<% end %>
<% if @all.empty? %>
<p><br>You haven't added any synapses yet.<p>
<% end %>
<div class="synapses" id="container">
<div id="center-container">
<div id="infovis"></div>
</div>
<div id="showcard">
</div>
</div>
<div class="clearfloat"></div>
<script>
json = <%= @synapsesjson %>;
console.log(json);
$(document).ready(function() {
init();
});
</script>

View file

@ -1,19 +1,16 @@
<div class="focus">
<div class="focusleft">
<p><%= @node1.name %></p>
<%= image_tag @node1.item_category.icon, :class => 'icon', :size => '50x50' %>
<p><%= @node1.desc %></p>
</div>
<div class="focusmiddle">
<h1 class="title">Synapse <%= link_to "[edit]", edit_synapse_path(@synapse) %></h1>
<div class="desc">
<p><%= @synapse.desc %></p>
</div>
</div>
<div class="focusright">
<p><%= @node2.name %></p>
<%= image_tag @node2.item_category.icon, :class => 'icon', :size => '50x50' %>
<p><%= @node2.desc %></p>
</div>
<div class="relatives" id="container">
<div id="center-container">
<div id="infovis"></div>
</div>
<div id="showcard">
</div>
</div>
<div class="clearfloat nodemargin"></div>
<div class="clearfloat"></div>
<script>
json = <%= @synapsejson %>;
console.log(json);
$(document).ready(function() {
init();
});
</script>