<commit_before>class ProjectPolicy < ApplicationPolicy

  self::UserScope = Struct.new(:current_user, :user, :scope) do
    def resolve
      if current_user.try(:admin?) || current_user == user
        scope.without_state('deleted')
      else
        scope.without_state(['deleted', 'draft', 'in_analysis', 'rejected'])
      end
    end
  end

  def create?
    done_by_owner_or_admin?
  end

  def update?
    create?
  end

  def send_to_analysis?
    create?
  end

  def save_recipient?
    create?
  end

  def edit_partner?
    is_admin?
  end

  def permitted_attributes
    if user.present? && (user.admin? || is_project_channel_admin? || (record.draft? || record.rejected? || record.in_analysis?))

      p_attr = [
        channel_ids: [],
        project_images_attributes: [:original_image_url, :caption, :id, :_destroy],
        project_partners_attributes: [:original_image_url, :link, :id, :_destroy]
      ]

      p_attr << record.attribute_names.map(&:to_sym)
      p_attr << [posts_attributes: [:title, :comment, :exclusive, :user_id]]
      {project: p_attr.flatten}
    elsif is_owned_by?(user)
      {project: [:about, :video_url, :uploaded_image, :headline, :name, :permalink, :category_id,
        posts_attributes: [:title, :comment, :exclusive, :user_id]]}
    else
      {project: [:about, :video_url, :uploaded_image, :headline]}
    end
  end
end<commit_msg>Add logic for allowed attributes<commit_after>class ProjectPolicy < ApplicationPolicy

  self::UserScope = Struct.new(:current_user, :user, :scope) do
    def resolve
      if current_user.try(:admin?) || current_user == user
        scope.without_state('deleted')
      else
        scope.without_state(['deleted', 'draft', 'in_analysis', 'rejected'])
      end
    end
  end

  def create?
    done_by_owner_or_admin?
  end

  def update?
    create?
  end

  def send_to_analysis?
    create?
  end

  def save_recipient?
    create?
  end

  def edit_partner?
    is_admin?
  end

  def permitted_attributes
    return project_attributes_for_admin if admin? || not_visible_project?
    return project_attributes_for_owner if visible_and_owned_by_user?
    return basic_project_attributes
  end

  private

  def not_visible_project?
    !record.visible?
  end

  def visible_and_owned_by_user?
    record.visible? && is_owned_by?(user)
  end

  def admin?
    return false unless user.present?

    user.admin? || is_project_channel_admin?
  end

  def project_attributes_for_admin
    { project: project_attributes + project_nested_attributes + post_attributes }
  end

  def project_attributes
    record.attribute_names.map(&:to_sym) - [:created_at, :updated_at]
  end

  def post_attributes
    [posts_attributes: [:title, :comment, :exclusive, :user_id]]
  end

  def project_nested_attributes
    [
      channel_ids: [],
      project_images_attributes: [:original_image_url, :caption, :id, :_destroy],
      project_partners_attributes: [:original_image_url, :link, :id, :_destroy]
    ]
  end

  def project_attributes_for_owner
    { project: [
        :video_url, :about, :thank_you, :uploaded_image,
        project_partners_attributes: [:original_image_url, :link, :id, :_destroy],
        posts_attributes: [:title, :comment, :exclusive, :user_id]
      ]
    }
  end

  def basic_project_attributes
    { project: [:about, :video_url, :uploaded_image, :headline] }
  end
end
{'fraction_non_alphanumeric': 0.10641442506650901, 'fraction_numerical': 0.0, 'mean_word_length': 3.5668016194331984, 'pattern_counts': {'":': 0, '<': 9, '<?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': '7223590', 'n_tokens_mistral': 1133, 'n_tokens_neox': 1108, 'n_words': 239}