<commit_before># 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
<commit_msg>Update message_policy.rb (#973)<commit_after># 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?
    # we have currently decided to let any map that is visible to someone be commented on by them
    record.resource.present? && resource_policy.show?
  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

{'fraction_non_alphanumeric': 0.10724499523355577, 'fraction_numerical': 0.0014299332697807435, 'mean_word_length': 3.2064128256513027, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1052870', 'n_tokens_mistral': 639, 'n_tokens_neox': 619, 'n_words': 188}