metamaps--metamaps/app/policies/message_policy.rb
Connor Turland c0955d7c5e multiple policy issues (#771)
* multiple policy errors

* make some things more explicit
2016-10-17 01:20:48 -04:00

36 lines
965 B
Ruby

# frozen_string_literal: true
class MessagePolicy < ApplicationPolicy
class Scope < Scope
def resolve
visible = %w(public commons)
permission = 'maps.permission IN (?)'
return scope.joins(:map).where(permission, visible) unless user
# if this is getting changed, the policy_scope for mappings should also be changed
# as it is based entirely on the map to which it belongs
scope.joins(:map).where(permission, visible)
.or(scope.joins(:map).where('maps.id IN (?)', user.shared_maps.map(&:id)))
.or(scope.joins(:map).where('maps.user_id = ?', user.id))
end
end
delegate :show?, to: :resource_policy
def create?
record.resource.present? && resource_policy.update?
end
def update?
record.user == user
end
def destroy?
record.user == user || admin_override
end
# Helpers
def resource_policy
@resource_policy ||= Pundit.policy(user, record.resource)
end
end