2016-02-13 09:28:16 +00:00
|
|
|
class TopicPolicy < ApplicationPolicy
|
|
|
|
class Scope < Scope
|
|
|
|
def resolve
|
2016-07-26 00:14:23 +00:00
|
|
|
visible = %w(public commons)
|
2016-03-12 15:41:32 +00:00
|
|
|
permission = 'topics.permission IN (?)'
|
|
|
|
if user
|
2016-04-24 15:50:35 +00:00
|
|
|
scope.where(permission + ' OR topics.defer_to_map_id IN (?) OR topics.user_id = ?', visible, user.shared_maps.map(&:id), user.id)
|
2016-03-12 15:41:32 +00:00
|
|
|
else
|
|
|
|
scope.where(permission, visible)
|
|
|
|
end
|
2016-02-13 09:28:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
def index?
|
|
|
|
user.present?
|
|
|
|
end
|
|
|
|
|
2016-02-13 09:28:16 +00:00
|
|
|
def create?
|
2016-02-28 09:24:00 +00:00
|
|
|
user.present?
|
2016-02-13 09:28:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show?
|
2016-04-24 15:50:35 +00:00
|
|
|
if record.defer_to_map.present?
|
|
|
|
map_policy.show?
|
|
|
|
else
|
|
|
|
record.permission == 'commons' || record.permission == 'public' || record.user == user
|
|
|
|
end
|
2016-02-13 09:28:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update?
|
2016-07-26 00:14:23 +00:00
|
|
|
if !user.present?
|
2016-04-24 15:50:35 +00:00
|
|
|
false
|
|
|
|
elsif record.defer_to_map.present?
|
2016-07-26 00:14:23 +00:00
|
|
|
map_policy.update?
|
|
|
|
else
|
2016-04-24 15:50:35 +00:00
|
|
|
record.permission == 'commons' || record.user == user
|
|
|
|
end
|
2016-02-13 09:28:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy?
|
2016-03-11 13:32:18 +00:00
|
|
|
record.user == user || admin_override
|
2016-02-13 09:28:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def autocomplete_topic?
|
2016-02-28 09:24:00 +00:00
|
|
|
user.present?
|
2016-02-13 09:28:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def network?
|
|
|
|
show?
|
|
|
|
end
|
|
|
|
|
|
|
|
def relative_numbers?
|
|
|
|
show?
|
|
|
|
end
|
|
|
|
|
|
|
|
def relatives?
|
|
|
|
show?
|
|
|
|
end
|
2016-04-24 15:50:35 +00:00
|
|
|
|
|
|
|
# Helpers
|
|
|
|
def map_policy
|
|
|
|
@map_policy ||= Pundit.policy(user, record.defer_to_map)
|
|
|
|
end
|
2016-02-13 09:28:16 +00:00
|
|
|
end
|