change from category/topic_id/synapse_id to mappable_type/mappable_id

This commit is contained in:
Devin Howard 2015-10-02 16:28:45 +08:00
parent 69f4b8c645
commit 6d1e382d5c
3 changed files with 15 additions and 25 deletions

View file

@ -457,11 +457,11 @@ Metamaps.Backbone.init = function () {
return Metamaps.Map.get(this.get('map_id')); return Metamaps.Map.get(this.get('map_id'));
}, },
getTopic: function () { 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; else return false;
}, },
getSynapse: function () { 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; else return false;
} }
}); });
@ -4109,10 +4109,10 @@ Metamaps.Topic = {
Metamaps.Topics.add(topic); Metamaps.Topics.add(topic);
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
category: "Topic",
xloc: Metamaps.Create.newTopic.x, xloc: Metamaps.Create.newTopic.x,
yloc: Metamaps.Create.newTopic.y, yloc: Metamaps.Create.newTopic.y,
topic_id: topic.cid mappable_id: topic.cid,
mappable_type: "Topic",
}); });
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping);
@ -4131,10 +4131,10 @@ Metamaps.Topic = {
var topic = self.get(id); var topic = self.get(id);
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
category: "Topic",
xloc: Metamaps.Create.newTopic.x, xloc: Metamaps.Create.newTopic.x,
yloc: Metamaps.Create.newTopic.y, yloc: Metamaps.Create.newTopic.y,
topic_id: topic.id mappable_type: "Topic",
mappable_id: topic.id,
}); });
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping);
@ -4149,10 +4149,10 @@ Metamaps.Topic = {
var nextCoords = Metamaps.Map.getNextCoord(); var nextCoords = Metamaps.Map.getNextCoord();
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
category: "Topic",
xloc: nextCoords.x, xloc: nextCoords.x,
yloc: nextCoords.y, yloc: nextCoords.y,
topic_id: topic.id mappable_type: "Topic",
mappable_id: topic.id,
}); });
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping);
@ -4287,8 +4287,8 @@ Metamaps.Synapse = {
Metamaps.Synapses.add(synapse); Metamaps.Synapses.add(synapse);
mapping = new Metamaps.Backbone.Mapping({ mapping = new Metamaps.Backbone.Mapping({
category: "Synapse", mappable_type: "Synapse",
synapse_id: synapse.cid mappable_id: synapse.cid,
}); });
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping);
@ -4308,8 +4308,8 @@ Metamaps.Synapse = {
var synapse = self.get(id); var synapse = self.get(id);
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
category: "Synapse", mappable_type: "Synapse",
synapse_id: synapse.id mappable_id: synapse.id,
}); });
Metamaps.Mappings.add(mapping); Metamaps.Mappings.add(mapping);

View file

@ -52,6 +52,6 @@ class MappingsController < ApplicationController
private private
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
def mapping_params 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
end end

View file

@ -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)) } @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)) } @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| @allmappings = @map.mappings.to_a.delete_if {|m|
if m.category == "Synapse" object = m.mappable
object = m.synapse
elsif m.category == "Topic"
object = m.topic
end
!object || (object.permission == "private" && (!authenticated? || (authenticated? && @current.id != object.user_id))) !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)) } @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)) } @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| @allmappings = @map.mappings.to_a.delete_if {|m|
if m.category == "Synapse" object = m.mappable
object = m.synapse
elsif m.category == "Topic"
object = m.topic
end
!object || (object.permission == "private" && (!authenticated? || (authenticated? && @current.id != object.user_id))) !object || (object.permission == "private" && (!authenticated? || (authenticated? && @current.id != object.user_id)))
} }
@ -141,7 +133,6 @@ class MapsController < ApplicationController
@all.each do |topic| @all.each do |topic|
topic = topic.split('/') topic = topic.split('/')
@mapping = Mapping.new() @mapping = Mapping.new()
@mapping.category = "Topic"
@mapping.user = @user @mapping.user = @user
@mapping.map = @map @mapping.map = @map
@mapping.topic = Topic.find(topic[0]) @mapping.topic = Topic.find(topic[0])
@ -155,7 +146,6 @@ class MapsController < ApplicationController
@synAll = @synAll.split(',') @synAll = @synAll.split(',')
@synAll.each do |synapse_id| @synAll.each do |synapse_id|
@mapping = Mapping.new() @mapping = Mapping.new()
@mapping.category = "Synapse"
@mapping.user = @user @mapping.user = @user
@mapping.map = @map @mapping.map = @map
@mapping.synapse = Synapse.find(synapse_id) @mapping.synapse = Synapse.find(synapse_id)