<commit_before>class DepartmentPolicy < ApplicationPolicy
  class Scope
    attr_reader :user, :department
    def initialize(user, department)
      @user = user
      @department = department
    end

    def resolve
      if @user.superadmin?
        @department.all
      else
        @department.where(user: @user)
      end
    end
  end

  def initialize(user, department)
    @user = user
    @department = department
  end

  def permitted_attributes
    return [user_ids: [], contact_attributes: contact_attrs] if @user.superadmin?
    return [contact_attributes: contact_attrs] if @user.department_manager?
    []
  end

  def phone_attrs
    [:id, :body, :label, :_destroy, :type, :contacts_id]
  end

  def email_attrs
    [:id, :body, :label, :_destroy, :type, :contacts_id]
  end

  def contact_attrs
    [
      :id, :name, :_destroy, :contactable_id, :contactable_type, :street,
      :extended, :city, :postal_code,
      contact_emails_attributes: email_attrs,
      contact_phones_attributes: phone_attrs
    ]
  end

  def index?
    @user.superadmin?
  end

  def show?
    @user.superadmin? || @department.user.include?(@user)
  end

  def new?
    @user.superadmin?
  end

  def edit?
    @user.superadmin? || @department.user.include?(@user)
  end

  def create?
    @user.superadmin?
  end

  def associate_user?
    @user.superadmin?
  end

  def manager_with_department?
    @user.department_manager? && @user.department?
  end

  def update?
    @user.superadmin? || @department.user.include?(@user)
  end

  def destroy?
    @user.superadmin?
  end
end<commit_msg>slimming contact_attrs and putting it to private methods<commit_after>class DepartmentPolicy < ApplicationPolicy
  class Scope
    attr_reader :user, :department
    def initialize(user, department)
      @user = user
      @department = department
    end

    def resolve
      if @user.superadmin?
        @department.all
      else
        @department.where(user: @user)
      end
    end
  end

  def initialize(user, department)
    @user = user
    @department = department
  end

  def permitted_attributes
    return contact_attrs.push(user_ids: []) if @user.superadmin?
    return contact_attrs if @user.department_manager?
    []
  end

  def index?
    @user.superadmin?
  end

  def show?
    @user.superadmin? || @department.user.include?(@user)
  end

  def new?
    @user.superadmin?
  end

  def edit?
    @user.superadmin? || @department.user.include?(@user)
  end

  def create?
    @user.superadmin?
  end

  def associate_user?
    @user.superadmin?
  end

  def manager_with_department?
    @user.department_manager? && @user.department?
  end

  def update?
    @user.superadmin? || @department.user.include?(@user)
  end

  def destroy?
    @user.superadmin?
  end

  private

  def contact_point_attrs
    [:id, :body, :label, :_destroy, :type, :contacts_id]
  end

  def contact_attrs
    [
      contact_attributes: [
        :id, :name, :_destroy, :contactable_id, :contactable_type, :street,
        :extended, :city, :postal_code,
        contact_emails_attributes: contact_point_attrs,
        contact_phones_attributes: contact_point_attrs
      ]
    ]
  end
end
{'fraction_non_alphanumeric': 0.10034493571652556, 'fraction_numerical': 0.0, 'mean_word_length': 2.9726027397260273, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5608074', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1041, 'n_words': 244}