From e761e1693c3818f6acab158693f612352ae2d547 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Mon, 5 Sep 2016 11:52:35 +0800 Subject: [PATCH] use Topic.relatives scope to get all relatives --- app/controllers/topics_controller.rb | 4 +--- app/models/topic.rb | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index b0fd4057..fa7eada8 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -24,10 +24,8 @@ class TopicsController < ApplicationController respond_to do |format| format.html do - @alltopics = [@topic].concat(policy_scope(Topic.relatives1(@topic.id, current_user)).to_a).concat(policy_scope(Topic.relatives2(@topic.id, current_user)).to_a) + @alltopics = [@topic].concat(policy_scope(Topic.relatives(@topic.id, current_user)).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 diff --git a/app/models/topic.rb b/app/models/topic.rb index 4cb58f65..6ca214fe 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -42,6 +42,12 @@ class Topic < ActiveRecord::Base topics1 + topics2 end + scope :relatives, ->(topic_id = nil, user = nil) { + 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) + where(id: synapses.uniq) + } + scope :relatives1, ->(topic_id = nil, user = nil) { visible = %w(public commons) permission = 'synapses.permission IN (?)'