node{1,2}_id => topic{1,2}_id migration and code changes

This commit is contained in:
Devin Howard 2016-09-28 10:32:28 +08:00
parent 8f0b350a2d
commit 743c9b3af9
17 changed files with 57 additions and 52 deletions

View file

@ -156,8 +156,8 @@ class MainController < ApplicationController
@synapses = @synapses.uniq(&:desc)
elsif topic1id && !topic1id.empty?
@one = policy_scope(Synapse).where('node1_id = ? AND node2_id = ?', topic1id, topic2id)
@two = policy_scope(Synapse).where('node2_id = ? AND node1_id = ?', topic1id, topic2id)
@one = policy_scope(Synapse).where('topic1_id = ? AND topic2_id = ?', topic1id, topic2id)
@two = policy_scope(Synapse).where('topic2_id = ? AND topic1_id = ?', topic1id, topic2id)
@synapses = @one + @two
@synapses.sort! { |s1, s2| s1.desc <=> s2.desc }.to_a
else

View file

@ -63,6 +63,6 @@ class SynapsesController < ApplicationController
private
def synapse_params
params.require(:synapse).permit(:id, :desc, :category, :weight, :permission, :node1_id, :node2_id, :user_id)
params.require(:synapse).permit(:id, :desc, :category, :weight, :permission, :topic1_id, :topic2_id, :user_id)
end
end

View file

@ -96,7 +96,7 @@ class TopicsController < ApplicationController
# find synapses between topics in alltopics array
allsynapses = policy_scope(Synapse.for_topic(@topic.id)).to_a
synapse_ids = (allsynapses.map(&:node1_id) + allsynapses.map(&:node2_id)).uniq
synapse_ids = (allsynapses.map(&:topic1_id) + allsynapses.map(&:topic2_id)).uniq
allsynapses.delete_if do |synapse|
!synapse_ids.index(synapse.id).nil?
end

View file

@ -19,7 +19,7 @@ class PermittedParams < Struct.new(:params)
end
def synapse_attributes
[:desc, :category, :weight, :permission, :node1_id, :node2_id]
[:desc, :category, :weight, :permission, :topic1_id, :topic2_id]
end
def topic_attributes

View file

@ -3,8 +3,8 @@ class Synapse < ApplicationRecord
belongs_to :user
belongs_to :defer_to_map, class_name: 'Map', foreign_key: 'defer_to_map_id'
belongs_to :topic1, class_name: 'Topic', foreign_key: 'node1_id'
belongs_to :topic2, class_name: 'Topic', foreign_key: 'node2_id'
belongs_to :topic1, class_name: 'Topic', foreign_key: 'topic1_id'
belongs_to :topic2, class_name: 'Topic', foreign_key: 'topic2_id'
has_many :mappings, as: :mappable, dependent: :destroy
has_many :maps, through: :mappings
@ -12,14 +12,14 @@ class Synapse < ApplicationRecord
validates :desc, length: { minimum: 0, allow_nil: false }
validates :permission, presence: true
validates :node1_id, presence: true
validates :node2_id, presence: true
validates :topic1_id, presence: true
validates :topic2_id, presence: true
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
validates :category, inclusion: { in: ['from-to', 'both'], allow_nil: true }
scope :for_topic, ->(topic_id = nil) {
where('node1_id = ? OR node2_id = ?', topic_id, topic_id)
where(topic1_id: topic_id).or(where(topic2_id: topic_id))
}
delegate :name, to: :user, prefix: true

View file

@ -5,8 +5,8 @@ class Topic < ApplicationRecord
belongs_to :user
belongs_to :defer_to_map, class_name: 'Map', foreign_key: 'defer_to_map_id'
has_many :synapses1, class_name: 'Synapse', foreign_key: 'node1_id', dependent: :destroy
has_many :synapses2, class_name: 'Synapse', foreign_key: 'node2_id', dependent: :destroy
has_many :synapses1, class_name: 'Synapse', foreign_key: 'topic1_id', dependent: :destroy
has_many :synapses2, class_name: 'Synapse', foreign_key: 'topic2_id', dependent: :destroy
has_many :topics1, through: :synapses2, source: :topic1
has_many :topics2, through: :synapses1, source: :topic2
@ -46,8 +46,8 @@ class Topic < ApplicationRecord
scope :relatives, ->(topic_id = nil, user = nil) {
# should only see topics through *visible* synapses
# e.g. Topic A (commons) -> synapse (private) -> Topic B (commons) must be filtered out
synapses = Pundit.policy_scope(user, Synapse.where(node1_id: topic_id)).pluck(:node2_id)
synapses += Pundit.policy_scope(user, Synapse.where(node2_id: topic_id)).pluck(:node1_id)
synapses = Pundit.policy_scope(user, Synapse.where(topic1_id: topic_id)).pluck(:topic2_id)
synapses += Pundit.policy_scope(user, Synapse.where(topic2_id: topic_id)).pluck(:topic1_id)
where(id: synapses.uniq)
}
@ -94,18 +94,18 @@ class Topic < ApplicationRecord
output = []
synapses.each do |synapse|
if synapse.category == 'from-to'
if synapse.node1_id == id
output << synapse.node1_id.to_s + '->' + synapse.node2_id.to_s
elsif synapse.node2_id == id
output << synapse.node2_id.to_s + '<-' + synapse.node1_id.to_s
if synapse.topic1_id == id
output << synapse.topic1_id.to_s + '->' + synapse.topic2_id.to_s
elsif synapse.topic2_id == id
output << synapse.topic2_id.to_s + '<-' + synapse.topic1_id.to_s
else
raise 'invalid synapse on topic in synapse_csv'
end
elsif synapse.category == 'both'
if synapse.node1_id == id
output << synapse.node1_id.to_s + '<->' + synapse.node2_id.to_s
elsif synapse.node2_id == id
output << synapse.node2_id.to_s + '<->' + synapse.node1_id.to_s
if synapse.topic1_id == id
output << synapse.topic1_id.to_s + '<->' + synapse.topic2_id.to_s
elsif synapse.topic2_id == id
output << synapse.topic2_id.to_s + '<->' + synapse.topic1_id.to_s
else
raise 'invalid synapse on topic in synapse_csv'
end

View file

@ -14,8 +14,7 @@ module Api
end
# self.embeddable might look like this:
# topic1: { attr: :node1, serializer: TopicSerializer }
# topic2: { attr: :node2, serializer: TopicSerializer }
# creator: { attr: :first_creator, serializer: UserSerializer }
# contributors: { serializer: UserSerializer}
# This method will remove the :attr key if the underlying attribute name
# is different than the name provided in the final json output. All other keys
@ -24,9 +23,9 @@ module Api
#
# This setup means if you passed this self.embeddable config and sent no
# ?embed= query param with your API request, you would get the regular attributes
# plus topic1_id, topic2_id, and contributor_ids. If you pass
# ?embed=topic1,topic2,contributors, then instead of two ids and an array of ids,
# you would get two serialized topics and an array of serialized users
# plus creator_id and contributor_ids. If you passed ?embed=creator,contributors
# then instead of an id and an array of ids, you would get a serialized user
# (the first_creator) and an array of serialized users (the contributors).
def self.embed_dat
embeddable.each_pair do |key, opts|
attr = opts.delete(:attr) || key

View file

@ -11,8 +11,8 @@ module Api
def self.embeddable
{
topic1: { attr: :node1, serializer: TopicSerializer },
topic2: { attr: :node2, serializer: TopicSerializer },
topic1: { serializer: TopicSerializer },
topic2: { serializer: TopicSerializer },
user: {}
}
end

View file

@ -62,8 +62,8 @@ class MapExportService
visible_synapses.map do |synapse|
next nil if synapse.nil?
OpenStruct.new(
topic1: synapse.node1_id,
topic2: synapse.node2_id,
topic1: synapse.topic1_id,
topic2: synapse.topic2_id,
category: synapse.category,
description: synapse.desc,
user: synapse.user.name,

View file

@ -0,0 +1,6 @@
class RenameNode1IdToTopic1IdInSynapses < ActiveRecord::Migration[5.0]
def change
rename_column :synapses, :node1_id, :topic1_id
rename_column :synapses, :node2_id, :topic2_id
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160820231717) do
ActiveRecord::Schema.define(version: 20160928022635) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -175,16 +175,16 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.text "category"
t.text "weight"
t.text "permission"
t.integer "node1_id"
t.integer "node2_id"
t.integer "topic1_id"
t.integer "topic2_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "defer_to_map_id"
t.index ["node1_id", "node1_id"], name: "index_synapses_on_node1_id_and_node1_id", using: :btree
t.index ["node1_id"], name: "index_synapses_on_node1_id", using: :btree
t.index ["node2_id", "node2_id"], name: "index_synapses_on_node2_id_and_node2_id", using: :btree
t.index ["node2_id"], name: "index_synapses_on_node2_id", using: :btree
t.index ["topic1_id", "topic1_id"], name: "index_synapses_on_node1_id_and_node1_id", using: :btree
t.index ["topic1_id"], name: "index_synapses_on_topic1_id", using: :btree
t.index ["topic2_id", "topic2_id"], name: "index_synapses_on_node2_id_and_node2_id", using: :btree
t.index ["topic2_id"], name: "index_synapses_on_topic2_id", using: :btree
t.index ["user_id"], name: "index_synapses_on_user_id", using: :btree
end

View file

@ -531,10 +531,10 @@ _Backbone.init = function () {
else return false
},
getTopic1: function () {
return Metamaps.Topics.get(this.get('node1_id'))
return Metamaps.Topics.get(this.get('topic1_id'))
},
getTopic2: function () {
return Metamaps.Topics.get(this.get('node2_id'))
return Metamaps.Topics.get(this.get('topic2_id'))
},
getDirection: function () {
var t1 = this.getTopic1(),
@ -559,8 +559,8 @@ _Backbone.init = function () {
var synapseID = this.isNew() ? this.cid : this.id
var edge = {
nodeFrom: this.get('node1_id'),
nodeTo: this.get('node2_id'),
nodeFrom: this.get('topic1_id'),
nodeTo: this.get('topic2_id'),
data: {
$synapses: [],
$synapseIDs: [synapseID],

View file

@ -333,8 +333,8 @@ const Import = {
desc: desc || "",
category: category,
permission: permission,
node1_id: topic1.id,
node2_id: topic2.id
topic1_id: topic1.id,
topic2_id: topic2.id
})
Metamaps.Synapses.add(synapse)

View file

@ -93,7 +93,7 @@ const JIT = {
synapses.each(function (s) {
edge = s.createEdge()
if (topics.get(s.get('node1_id')) === undefined || topics.get(s.get('node2_id')) === undefined) {
if (topics.get(s.get('topic1_id')) === undefined || topics.get(s.get('topic2_id')) === undefined) {
// this means it's an invalid synapse
synapsesToRemove.push(s)
}

View file

@ -200,8 +200,8 @@ const Map = {
var descNotFiltered = Filter.visible.synapses.indexOf(desc) > -1
// make sure that both topics are being added, otherwise, it
// doesn't make sense to add the synapse
var topicsNotFiltered = nodes_array.indexOf(synapse.get('node1_id')) > -1
topicsNotFiltered = topicsNotFiltered && nodes_array.indexOf(synapse.get('node2_id')) > -1
var topicsNotFiltered = nodes_array.indexOf(synapse.get('topic1_id')) > -1
topicsNotFiltered = topicsNotFiltered && nodes_array.indexOf(synapse.get('topic2_id')) > -1
if (descNotFiltered && topicsNotFiltered) {
synapses_array.push(synapse.id)
}

View file

@ -128,8 +128,8 @@ const Synapse = {
topic1 = node1.getData('topic')
synapse = new Metamaps.Backbone.Synapse({
desc: Create.newSynapse.description,
node1_id: topic1.isNew() ? topic1.cid : topic1.id,
node2_id: topic2.isNew() ? topic2.cid : topic2.id,
topic1_id: topic1.isNew() ? topic1.cid : topic1.id,
topic2_id: topic2.isNew() ? topic2.cid : topic2.id,
})
Metamaps.Synapses.add(synapse)

View file

@ -238,7 +238,7 @@ const SynapseCard = {
var directionCat = synapse.get('category'); // both, none, from-to
if (directionCat == 'from-to') {
var from_to = [synapse.get('node1_id'), synapse.get('node2_id')]
var from_to = [synapse.get('topic1_id'), synapse.get('topic2_id')]
if (from_to[0] == left.id) {
// check left checkbox
$('#edit_synapse_left').addClass('checked')
@ -273,8 +273,8 @@ const SynapseCard = {
synapse.save({
category: dirCat,
node1_id: dir[0],
node2_id: dir[1]
topic1_id: dir[0],
topic2_id: dir[1]
})
Visualize.mGraph.plot()
})