From b6260d7015c469a10fee7f874c68aeb1bef1d9ff Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Fri, 19 Oct 2012 17:26:46 -0400 Subject: [PATCH] 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 --- app/controllers/main_controller.rb | 8 +++- app/controllers/synapses_controller.rb | 12 ++---- app/helpers/items_helper.rb | 60 ++++++++++++++++++++++++-- app/models/item.rb | 4 +- app/models/synapse.rb | 28 ++++++++++++ app/views/main/usersynapses.html.erb | 21 ++++++--- app/views/synapses/show.html.erb | 33 +++++++------- 7 files changed, 127 insertions(+), 39 deletions(-) diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb index 1f1255b9..1b4a9250 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -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 diff --git a/app/controllers/synapses_controller.rb b/app/controllers/synapses_controller.rb index cc22f90e..bc1c1526 100644 --- a/app/controllers/synapses_controller.rb +++ b/app/controllers/synapses_controller.rb @@ -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]) diff --git a/app/helpers/items_helper.rb b/app/helpers/items_helper.rb index b3559ca4..55f4e110 100644 --- a/app/helpers/items_helper.rb +++ b/app/helpers/items_helper.rb @@ -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 diff --git a/app/models/item.rb b/app/models/item.rb index 36ae92c9..70e308a7 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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| diff --git a/app/models/synapse.rb b/app/models/synapse.rb index a328b65f..718c05ec 100644 --- a/app/models/synapse.rb +++ b/app/models/synapse.rb @@ -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 diff --git a/app/views/main/usersynapses.html.erb b/app/views/main/usersynapses.html.erb index 0c783c8d..a9b343bc 100644 --- a/app/views/main/usersynapses.html.erb +++ b/app/views/main/usersynapses.html.erb @@ -1,9 +1,16 @@ -
- <% @all.each do |object| %> - <%= render object %> - <% end %> - <% if @all.empty? %> -


You haven't added any synapses yet.

- <% end %> +

+
+
+
+
+
+ + diff --git a/app/views/synapses/show.html.erb b/app/views/synapses/show.html.erb index 64292ae0..153a0412 100644 --- a/app/views/synapses/show.html.erb +++ b/app/views/synapses/show.html.erb @@ -1,19 +1,16 @@ -
-
-

<%= @node1.name %>

- <%= image_tag @node1.item_category.icon, :class => 'icon', :size => '50x50' %> -

<%= @node1.desc %>

-
-
-

Synapse <%= link_to "[edit]", edit_synapse_path(@synapse) %>

-
-

<%= @synapse.desc %>

-
-
-
-

<%= @node2.name %>

- <%= image_tag @node2.item_category.icon, :class => 'icon', :size => '50x50' %> -

<%= @node2.desc %>

-
+
+
+
+
+
+
-
\ No newline at end of file +
+ + \ No newline at end of file