From 4d09fb6fc8ec8edb61e354802ca78fa5c4083882 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Tue, 9 Oct 2012 20:23:45 -0400 Subject: [PATCH] added ability to add synapses --- app/assets/javascripts/synapses.js.coffee | 3 + app/assets/stylesheets/synapses.css.scss | 3 + app/controllers/groups_controller.rb | 4 +- app/controllers/items_controller.rb | 6 +- app/controllers/people_controller.rb | 4 +- app/controllers/synapses_controller.rb | 145 ++++++++++++++++++++ app/helpers/synapses_helper.rb | 2 + app/models/group.rb | 8 +- app/models/item.rb | 8 +- app/models/person.rb | 2 +- app/views/layouts/application.html.erb | 4 +- app/views/synapses/edit.html.erb | 14 ++ app/views/synapses/new.html.erb | 45 ++++++ app/views/synapses/show.html.erb | 31 +++++ config/routes.rb | 3 +- test/functional/synapses_controller_test.rb | 7 + test/unit/helpers/synapses_helper_test.rb | 4 + 17 files changed, 275 insertions(+), 18 deletions(-) create mode 100644 app/assets/javascripts/synapses.js.coffee create mode 100644 app/assets/stylesheets/synapses.css.scss create mode 100644 app/controllers/synapses_controller.rb create mode 100644 app/helpers/synapses_helper.rb create mode 100644 app/views/synapses/edit.html.erb create mode 100644 app/views/synapses/new.html.erb create mode 100644 app/views/synapses/show.html.erb create mode 100644 test/functional/synapses_controller_test.rb create mode 100644 test/unit/helpers/synapses_helper_test.rb diff --git a/app/assets/javascripts/synapses.js.coffee b/app/assets/javascripts/synapses.js.coffee new file mode 100644 index 00000000..76156794 --- /dev/null +++ b/app/assets/javascripts/synapses.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/synapses.css.scss b/app/assets/stylesheets/synapses.css.scss new file mode 100644 index 00000000..73162259 --- /dev/null +++ b/app/assets/stylesheets/synapses.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the synapses controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 72e8c3cc..ace56529 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -46,7 +46,7 @@ class GroupsController < ApplicationController @group.save respond_to do |format| - format.html { respond_with(@user, location: restore(default: group_url(@group))) } + format.html { respond_with(@user, location: group_url(@group)) } format.js { respond_with(@group) } end @@ -121,7 +121,7 @@ class GroupsController < ApplicationController end end - respond_with(@user, location: restore(default: group_url(@group))) do |format| + respond_with(@user, location: group_url(@group)) do |format| end end diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index c2f03fb2..5e979446 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -50,7 +50,7 @@ class ItemsController < ApplicationController @item.save respond_to do |format| - format.html { respond_with(@user, location: restore(default: item_url(@item))) } + format.html { respond_with(@user, location: item_url(@item)) } format.js { respond_with(@item) } end @@ -180,12 +180,12 @@ class ItemsController < ApplicationController end end - respond_with(@user, location: restore(default: item_url(@item))) do |format| + respond_with(@user, location: item_url(@item)) do |format| end end - # DELETE /actions/:id + # DELETE /items/:id def destroy @item = Item.find_by_id(params[:id]) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 7bbd325f..3eff0c88 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -60,7 +60,7 @@ class PeopleController < ApplicationController end respond_to do |format| - format.html { respond_with(@user, location: restore(default: person_url(@person))) } + format.html { respond_with(@user, location: person_url(@person)) } format.js { respond_with(@person) } end @@ -105,7 +105,7 @@ class PeopleController < ApplicationController end end - respond_with(@user, location: restore(default: person_url(@person))) do |format| + respond_with(@user, location: person_url(@person)) do |format| end end diff --git a/app/controllers/synapses_controller.rb b/app/controllers/synapses_controller.rb new file mode 100644 index 00000000..20c75c29 --- /dev/null +++ b/app/controllers/synapses_controller.rb @@ -0,0 +1,145 @@ +class SynapsesController < ApplicationController + + before_filter :require_user, only: [:new, :create, :edit, :update] + + respond_to :html, :js, :json + + # GET /synapses + def index + @user = current_user + @synapses = Synapse.all + + respond_with(@synapses) + end + + # Get /synapse/new + def new + @synapse1 = Synapse.new + @synapse1.category = "Group" + @synapse2 = Synapse.new + @synapse2.category = "Person" + @synapse3 = Synapse.new + @synapse3.category = "Item" + @user = current_user + @allgroups = Group.all + @allpeople = Person.all + @allitems = Item.all + + respond_with(@synapse1, @synapse2, @synapse3, @allgroups, @allpeople, @allitems) + end + + # GET /synapse/:id + def show + @synapse = Synapse.find(params[:id]) + + @node1 = nil + @node2 = nil + + if @synapse + if (@synapse.category == "Group") + @node1 = @synapse.group1 + @node2 = @synapse.group2 + end + if (@synapse.category == "Person") + @node1 = @synapse.person1 + @node2 = @synapse.person2 + end + if (@synapse.category == "Item") + @node1 = @synapse.item1 + @node2 = @synapse.item2 + end + end + + respond_to do |format| + format.html { respond_with(@synapse, @node1, @node2) } + # format.json { respond_with(@relatives) } + end + end + + # POST /synapses + def create + + @user = current_user + @synapse = Synapse.new() + @synapse.desc = params[:synapse][:desc] + @synapse.category = params[:category] + if ( @synapse.category == "Group" ) + @synapse.group1 = Group.find(params[:node1_id]) + @synapse.group2 = Group.find(params[:node2_id]) + end + if ( @synapse.category == "Person" ) + @synapse.person1 = Person.find(params[:node1_id]) + @synapse.person2 = Person.find(params[:node2_id]) + end + if ( @synapse.category == "Item" ) + @synapse.item1 = Item.find(params[:node1_id]) + @synapse.item2 = Item.find(params[:node2_id]) + end + @synapse.user = @user + + @synapse.save + + respond_to do |format| + format.html { respond_with(@user, location: synapse_url(@synapse)) } + format.js { respond_with(@synapse) } + end + + end + + # GET /synapses/:id/edit + def edit + @synapse = Synapse.find_by_id(params[:id]) + + @collection1 = nil + @collection2 = nil + + if @synapse + if (@synapse.category == "Group") + @collection = Group.all + end + if (@synapse.category == "Person") + @collection = Person.all + end + if (@synapse.category == "Item") + @collection = Item.all + end + end + + respond_with(@synapse, @collection) + end + + # PUT /actions/:id + def update + @synapse = Synapse.find_by_id(params[:id]) + + if @synapse + @synapse.desc = params[:synapse][:desc] + if ( @synapse.category == "Group" ) + @synapse.group1 = Group.find(params[:node1_id][:node1]) + @synapse.group2 = Group.find(params[:node2_id][:node2]) + end + if ( @synapse.category == "Person" ) + @synapse.person1 = Person.find(params[:node1_id][:node1]) + @synapse.person2 = Person.find(params[:node2_id][:node2]) + end + if ( @synapse.category == "Item" ) + @synapse.item1 = Item.find(params[:node1_id][:node1]) + @synapse.item2 = Item.find(params[:node2_id][:node2]) + end + + @synapse.save + end + + respond_with(@user, location: synapse_url(@synapse)) do |format| + end + + end + + # DELETE /synapses/:id + def destroy + @synapse = Synapse.find_by_id(params[:id]) + + @synapse.delete + end + +end diff --git a/app/helpers/synapses_helper.rb b/app/helpers/synapses_helper.rb new file mode 100644 index 00000000..58508a37 --- /dev/null +++ b/app/helpers/synapses_helper.rb @@ -0,0 +1,2 @@ +module SynapsesHelper +end diff --git a/app/models/group.rb b/app/models/group.rb index 50d77912..c51dce1e 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -36,10 +36,10 @@ has_many :items, :through => :groupitems @groups = @single + self.relatives json.array!(@groups) do |group| - json.adjacencies group.synapses2 do |json, synapse| - json.nodeTo synapse.node1_id - json.nodeFrom synapse.node2_id - json.data @data1 + json.adjacencies group.synapses2.delete_if{|synapse| not @groups.include?(Group.find_by_id(synapse.node1_id))} do |json, synapse| + json.nodeTo synapse.node1_id + json.nodeFrom synapse.node2_id + json.data @data1 end json.data @data2 diff --git a/app/models/item.rb b/app/models/item.rb index 48eeff55..b99b4055 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -38,10 +38,10 @@ has_many :parent_items, :through => :itemitem_p, :source => :parent_item @items = @single + self.relatives json.array!(@items) do |item| - json.adjacencies item.synapses2 do |json, synapse| - json.nodeTo synapse.node1_id - json.nodeFrom synapse.node2_id - json.data @data1 + 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 + json.data @data1 end json.data @data2 diff --git a/app/models/person.rb b/app/models/person.rb index 4564745d..43b19860 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -30,7 +30,7 @@ has_many :people2, :through => :synapses1, :source => :person2 @people = @single + self.relatives json.array!(@people) do |person| - json.adjacencies person.synapses2 do |json, synapse| + json.adjacencies person.synapses2.delete_if{|synapse| not @people.include?(Person.find_by_id(synapse.node1_id))} do |json, synapse| json.nodeTo synapse.node1_id json.nodeFrom synapse.node2_id json.data @data1 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index dbb53b24..67da80cd 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -23,7 +23,9 @@ <% if authenticated? %>