2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2016-02-13 09:28:16 +00:00
|
|
|
class TopicPolicy < ApplicationPolicy
|
|
|
|
class Scope < Scope
|
|
|
|
def resolve
|
2016-12-06 21:46:46 +00:00
|
|
|
return scope.where(permission: %w(public commons)) unless user
|
2016-09-24 04:27:34 +00:00
|
|
|
|
2016-12-06 21:46:46 +00:00
|
|
|
scope.where(permission: %w(public commons))
|
|
|
|
.or(scope.where(defer_to_map_id: user.all_accessible_maps.map(&:id)))
|
2016-09-24 04:27:34 +00:00
|
|
|
.or(scope.where(user_id: user.id))
|
2016-02-13 09:28:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
def index?
|
2016-10-26 00:37:23 +00:00
|
|
|
true
|
2016-09-21 17:22:40 +00:00
|
|
|
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
|
2016-12-13 03:28:10 +00:00
|
|
|
record.permission.in?(%w(commons public)) || record.user == user
|
2016-04-24 15:50:35 +00:00
|
|
|
end
|
2016-02-13 09:28:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update?
|
2017-11-25 19:23:47 +00:00
|
|
|
return false if user.blank?
|
2016-09-24 04:27:34 +00:00
|
|
|
if 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
|
|
|
|
2017-03-07 03:49:46 +00:00
|
|
|
def follow?
|
|
|
|
show? && user.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def unfollow?
|
|
|
|
user.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def unfollow_from_email?
|
|
|
|
user.present?
|
|
|
|
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
|