From 6d1e382d5c38fe17a509b030a3e56d7778a9007c Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Fri, 2 Oct 2015 16:28:45 +0800 Subject: [PATCH] change from category/topic_id/synapse_id to mappable_type/mappable_id --- app/assets/javascripts/src/Metamaps.js | 24 ++++++++++++------------ app/controllers/mappings_controller.rb | 2 +- app/controllers/maps_controller.rb | 14 ++------------ 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/app/assets/javascripts/src/Metamaps.js b/app/assets/javascripts/src/Metamaps.js index e77b9f74..78b6c868 100644 --- a/app/assets/javascripts/src/Metamaps.js +++ b/app/assets/javascripts/src/Metamaps.js @@ -457,11 +457,11 @@ Metamaps.Backbone.init = function () { return Metamaps.Map.get(this.get('map_id')); }, getTopic: function () { - if (this.get('category') === 'Topic') return Metamaps.Topic.get(this.get('topic_id')); + if (this.get('mappable_type') === 'Topic') return Metamaps.Topic.get(this.get('mappable_id')); else return false; }, getSynapse: function () { - if (this.get('category') === 'Synapse') return Metamaps.Synapse.get(this.get('synapse_id')); + if (this.get('mappable_type') === 'Synapse') return Metamaps.Synapse.get(this.get('mappable_id')); else return false; } }); @@ -4109,10 +4109,10 @@ Metamaps.Topic = { Metamaps.Topics.add(topic); var mapping = new Metamaps.Backbone.Mapping({ - category: "Topic", xloc: Metamaps.Create.newTopic.x, yloc: Metamaps.Create.newTopic.y, - topic_id: topic.cid + mappable_id: topic.cid, + mappable_type: "Topic", }); Metamaps.Mappings.add(mapping); @@ -4131,10 +4131,10 @@ Metamaps.Topic = { var topic = self.get(id); var mapping = new Metamaps.Backbone.Mapping({ - category: "Topic", xloc: Metamaps.Create.newTopic.x, yloc: Metamaps.Create.newTopic.y, - topic_id: topic.id + mappable_type: "Topic", + mappable_id: topic.id, }); Metamaps.Mappings.add(mapping); @@ -4149,10 +4149,10 @@ Metamaps.Topic = { var nextCoords = Metamaps.Map.getNextCoord(); var mapping = new Metamaps.Backbone.Mapping({ - category: "Topic", xloc: nextCoords.x, yloc: nextCoords.y, - topic_id: topic.id + mappable_type: "Topic", + mappable_id: topic.id, }); Metamaps.Mappings.add(mapping); @@ -4287,8 +4287,8 @@ Metamaps.Synapse = { Metamaps.Synapses.add(synapse); mapping = new Metamaps.Backbone.Mapping({ - category: "Synapse", - synapse_id: synapse.cid + mappable_type: "Synapse", + mappable_id: synapse.cid, }); Metamaps.Mappings.add(mapping); @@ -4308,8 +4308,8 @@ Metamaps.Synapse = { var synapse = self.get(id); var mapping = new Metamaps.Backbone.Mapping({ - category: "Synapse", - synapse_id: synapse.id + mappable_type: "Synapse", + mappable_id: synapse.id, }); Metamaps.Mappings.add(mapping); diff --git a/app/controllers/mappings_controller.rb b/app/controllers/mappings_controller.rb index 79d8d80a..27567eb4 100644 --- a/app/controllers/mappings_controller.rb +++ b/app/controllers/mappings_controller.rb @@ -52,6 +52,6 @@ class MappingsController < ApplicationController private # Never trust parameters from the scary internet, only allow the white list through. def mapping_params - params.require(:mapping).permit(:id, :category, :xloc, :yloc, :topic_id, :synapse_id, :map_id, :user_id) + params.require(:mapping).permit(:id, :xloc, :yloc, :mappable_id, :mappable_type, :map_id, :user_id) end end diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 21946f88..7fbddbcc 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -75,11 +75,7 @@ class MapsController < ApplicationController @alltopics = @map.topics.to_a.delete_if {|t| t.permission == "private" && (!authenticated? || (authenticated? && @current.id != t.user_id)) } @allsynapses = @map.synapses.to_a.delete_if {|s| s.permission == "private" && (!authenticated? || (authenticated? && @current.id != s.user_id)) } @allmappings = @map.mappings.to_a.delete_if {|m| - if m.category == "Synapse" - object = m.synapse - elsif m.category == "Topic" - object = m.topic - end + object = m.mappable !object || (object.permission == "private" && (!authenticated? || (authenticated? && @current.id != object.user_id))) } @@ -103,11 +99,7 @@ class MapsController < ApplicationController @alltopics = @map.topics.to_a.delete_if {|t| t.permission == "private" && (!authenticated? || (authenticated? && @current.id != t.user_id)) } @allsynapses = @map.synapses.to_a.delete_if {|s| s.permission == "private" && (!authenticated? || (authenticated? && @current.id != s.user_id)) } @allmappings = @map.mappings.to_a.delete_if {|m| - if m.category == "Synapse" - object = m.synapse - elsif m.category == "Topic" - object = m.topic - end + object = m.mappable !object || (object.permission == "private" && (!authenticated? || (authenticated? && @current.id != object.user_id))) } @@ -141,7 +133,6 @@ class MapsController < ApplicationController @all.each do |topic| topic = topic.split('/') @mapping = Mapping.new() - @mapping.category = "Topic" @mapping.user = @user @mapping.map = @map @mapping.topic = Topic.find(topic[0]) @@ -155,7 +146,6 @@ class MapsController < ApplicationController @synAll = @synAll.split(',') @synAll.each do |synapse_id| @mapping = Mapping.new() - @mapping.category = "Synapse" @mapping.user = @user @mapping.map = @map @mapping.synapse = Synapse.find(synapse_id)