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 class MainController < ApplicationController
include ItemsHelper
before_filter :require_user, only: [:userobjects] before_filter :require_user, only: [:userobjects]
respond_to :html, :js, :json respond_to :html, :js, :json
@ -27,9 +28,12 @@ class MainController < ApplicationController
def usersynapses def usersynapses
@user = current_user @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
end end

View file

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

View file

@ -1,10 +1,64 @@
module ItemsHelper 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 # if the array of nodes is empty initialize it
@test.push(node) 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 end

View file

@ -1,4 +1,5 @@
class Item < ActiveRecord::Base class Item < ActiveRecord::Base
include ItemsHelper
belongs_to :user belongs_to :user
@ -44,11 +45,12 @@ belongs_to :item_category
end end
end end
#build a json object of everything connected to a specified node
def map_as_json def map_as_json
Jbuilder.encode do |json| Jbuilder.encode do |json|
@single = Array.new @single = Array.new
@single.push(self) @single.push(self)
@items = @single + self.relatives @items = network(self,nil)
json.array!(@items) do |item| 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.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 :item1, :class_name => "Item", :foreign_key => "node1_id"
belongs_to :item2, :class_name => "Item", :foreign_key => "node2_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 end

View file

@ -1,9 +1,16 @@
<div class="everything" id="everything"> <div class="synapses" id="container">
<% @all.each do |object| %> <div id="center-container">
<%= render object %> <div id="infovis"></div>
<% end %> </div>
<% if @all.empty? %> <div id="showcard">
<p><br>You haven't added any synapses yet.<p> </div>
<% end %>
</div> </div>
<div class="clearfloat"></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="relatives" id="container">
<div class="focusleft"> <div id="center-container">
<p><%= @node1.name %></p> <div id="infovis"></div>
<%= image_tag @node1.item_category.icon, :class => 'icon', :size => '50x50' %> </div>
<p><%= @node1.desc %></p> <div id="showcard">
</div> </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> </div>
<div class="clearfloat nodemargin"></div> <div class="clearfloat"></div>
<script>
json = <%= @synapsejson %>;
console.log(json);
$(document).ready(function() {
init();
});
</script>