metamaps--metamaps/app/policies/topic_policy.rb

64 lines
1.1 KiB
Ruby
Raw Permalink Normal View History

2016-09-24 03:00:46 +00:00
# frozen_string_literal: true
2016-02-13 09:28:16 +00:00
class TopicPolicy < ApplicationPolicy
class Scope < Scope
def resolve
visible = %w(public commons)
2016-09-24 04:27:34 +00:00
return scope.where(permission: visible) unless user
scope.where(permission: visible)
.or(scope.where.not(defer_to_map_id: nil).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
def index?
true
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?
if record.defer_to_map.present?
map_policy.show?
else
2016-09-24 05:55:52 +00:00
record.permission.in?(['commons', 'public']) || record.user == user
end
2016-02-13 09:28:16 +00:00
end
def update?
2016-09-24 04:27:34 +00:00
return false unless user.present?
if record.defer_to_map.present?
map_policy.update?
else
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
# Helpers
def map_policy
@map_policy ||= Pundit.policy(user, record.defer_to_map)
end
2016-02-13 09:28:16 +00:00
end