From 8bd032472d48f682d9d9d5974eede47f93f7f8d6 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Mon, 14 Mar 2016 21:36:34 +1100 Subject: [PATCH] topic related things weren't working at all --- app/controllers/topics_controller.rb | 24 +++++++++++++----------- app/models/topic.rb | 17 +++++++++++------ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 1b1e9b3c..9911a7fe 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -24,9 +24,10 @@ class TopicsController < ApplicationController respond_to do |format| format.html { - @alltopics = ([@topic] + policy_scope(Topic.relatives(@topic.id))) - @allsynapses = policy_scope(Synapse.for_topic(@topic.id)) - + @alltopics = [@topic].concat(policy_scope(Topic.relatives1(@topic.id)).to_a).concat(policy_scope(Topic.relatives2(@topic.id)).to_a) + @allsynapses = policy_scope(Synapse.for_topic(@topic.id)).to_a +puts @alltopics.length +puts @allsynapses.length @allcreators = @alltopics.map(&:user).uniq @allcreators += @allsynapses.map(&:user).uniq @@ -41,8 +42,8 @@ class TopicsController < ApplicationController @topic = Topic.find(params[:id]) authorize @topic - @alltopics = [@topic] + policy_scope(@topic.relatives) - @allsynapses = policy_scope(@topic.synapses) + @alltopics = [@topic].concat(policy_scope(Topic.relatives1(@topic.id)).to_a).concat(policy_scope(Topic.relatives2(@topic.id)).to_a) + @allsynapses = policy_scope(Synapse.for_topic(@topic.id)) @allcreators = @alltopics.map(&:user).uniq @allcreators += @allsynapses.map(&:user).uniq @@ -65,8 +66,8 @@ class TopicsController < ApplicationController topicsAlreadyHas = params[:network] ? params[:network].split(',').map(&:to_i) : [] - @alltopics = policy_scope(@topic.relatives).to_a.uniq - @alltopics.delete_if! do |topic| + @alltopics = policy_scope(Topic.relatives1(@topic.id)).to_a.concat(policy_scope(Topic.relatives2(@topic.id)).to_a).uniq + @alltopics.delete_if do |topic| topicsAlreadyHas.index(topic.id) != nil end @@ -87,14 +88,15 @@ class TopicsController < ApplicationController topicsAlreadyHas = params[:network] ? params[:network].split(',').map(&:to_i) : [] - alltopics = policy_scope(@topic.relatives).to_a.uniq.delete_if do |topic| + alltopics = policy_scope(Topic.relatives1(@topic.id)).to_a.concat(policy_scope(Topic.relatives2(@topic.id)).to_a).uniq + alltopics.delete_if do |topic| topicsAlreadyHas.index(topic.id.to_s) != nil end #find synapses between topics in alltopics array - allsynapses = policy_scope(@topic.synapses) - synapse_ids = (allsynapses.map(&:topic1_id) + allsynapses.map(&:topic2_id)).uniq - allsynapses.delete_if! do |synapse| + allsynapses = policy_scope(Synapse.for_topic(@topic.id)).to_a + synapse_ids = (allsynapses.map(&:node1_id) + allsynapses.map(&:node2_id)).uniq + allsynapses.delete_if do |synapse| synapse_ids.index(synapse.id) != nil end diff --git a/app/models/topic.rb b/app/models/topic.rb index 0f312823..62f8b786 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -5,8 +5,8 @@ class Topic < ActiveRecord::Base 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 :topics1, :through => :synapses2, :source => :topic1 - has_many :topics2, :through => :synapses1, :source => :topic2 + has_many :topics1, :through => :synapses2, source: :topic1 + has_many :topics2, :through => :synapses1, source: :topic2 has_many :mappings, as: :mappable, dependent: :destroy has_many :maps, :through => :mappings @@ -41,10 +41,15 @@ class Topic < ActiveRecord::Base belongs_to :metacode - scope :relatives, ->(topic_id = nil) { - includes(:synapses1) - .includes(:synapses2) - .where('synapses.node1_id = ? OR synapses.node2_id = ?', topic_id, topic_id) + scope :relatives1, ->(topic_id = nil) { + includes(:topics1) + .where('synapses.node1_id = ?', topic_id) + .references(:synapses) + } + + scope :relatives2, ->(topic_id = nil) { + includes(:topics2) + .where('synapses.node2_id = ?', topic_id) .references(:synapses) }