use policy scope
This commit is contained in:
parent
7de89cfa0f
commit
54a68800b0
1 changed files with 4 additions and 24 deletions
|
@ -43,33 +43,13 @@ class Topic < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
scope :relatives1, ->(topic_id = nil, user = nil) {
|
scope :relatives1, ->(topic_id = nil, user = nil) {
|
||||||
visible = %w(public commons)
|
synapses = Pundit.policy_scope(user, Synapse.where(node1_id: topic_id))
|
||||||
permission = 'synapses.permission IN (?)'
|
includes(:topics1).where(id: synapses.map(&:node1_id))
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scope :relatives2, ->(topic_id = nil, user = nil) {
|
scope :relatives2, ->(topic_id = nil, user = nil) {
|
||||||
visible = %w(public commons)
|
synapses = Pundit.policy_scope(user, Synapse.where(node2_id: topic_id))
|
||||||
permission = 'synapses.permission IN (?)'
|
includes(:topics2).where(id: synapses.map(&:node2_id))
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delegate :name, to: :user, prefix: true
|
delegate :name, to: :user, prefix: true
|
||||||
|
|
Loading…
Reference in a new issue