From 54a68800b00d3e48fdd426fc540fc1b1bffc928b Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 3 Sep 2016 14:02:47 +0800 Subject: [PATCH] use policy scope --- app/models/topic.rb | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 4cb58f65..6259cb43 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -43,33 +43,13 @@ class Topic < ActiveRecord::Base end scope :relatives1, ->(topic_id = nil, user = nil) { - visible = %w(public commons) - permission = 'synapses.permission IN (?)' - if user - synapse_permission = permission + ' OR synapses.defer_to_map_id IN (?) OR synapses.user_id = ?' - return includes(:topics1) - .where('synapses.node1_id = ? AND (' + synapse_permission + ')', topic_id, visible, user.shared_maps.map(&:id), user.id) - .references(:synapses) - else - return includes(:topics1) - .where('synapses.node1_id = ? AND (' + permission + ')', topic_id, visible) - .references(:synapses) - end + synapses = Pundit.policy_scope(user, Synapse.where(node1_id: topic_id)) + includes(:topics1).where(id: synapses.map(&:node1_id)) } scope :relatives2, ->(topic_id = nil, user = nil) { - visible = %w(public commons) - permission = 'synapses.permission IN (?)' - if user - synapse_permission = permission + ' OR synapses.defer_to_map_id IN (?) OR synapses.user_id = ?' - return includes(:topics2) - .where('synapses.node2_id = ? AND (' + synapse_permission + ')', topic_id, visible, user.shared_maps.map(&:id), user.id) - .references(:synapses) - else - return includes(:topics2) - .where('synapses.node2_id = ? AND (' + permission + ')', topic_id, visible) - .references(:synapses) - end + synapses = Pundit.policy_scope(user, Synapse.where(node2_id: topic_id)) + includes(:topics2).where(id: synapses.map(&:node2_id)) } delegate :name, to: :user, prefix: true