<commit_before>class Users::PublicActivityComponent < ApplicationComponent
  attr_reader :user
  delegate :current_user, :valid_interests_access?, :current_path_with_query_params, to: :helpers

  def initialize(user)
    @user = user
  end

  def valid_access?
    user.public_activity || authorized_current_user?
  end

  def current_filter
    if valid_filters.include?(params[:filter])
      params[:filter]
    else
      valid_filters.first
    end
  end

  def valid_filters
    @valid_filters ||= [
      ("proposals" if feature?(:proposals)),
      ("debates" if feature?(:debates)),
      ("budget_investments" if feature?(:budgets)),
      "comments",
      ("follows" if valid_interests_access?(user))
    ].compact.select { |filter| send(filter).any? }
  end

  private

    def authorized_current_user?
      current_user == user || current_user&.moderator? || current_user&.administrator?
    end

    def proposals
      Proposal.where(author_id: user.id)
    end

    def debates
      Debate.where(author_id: user.id)
    end

    def comments
      only_active_commentables.includes(:commentable)
    end

    def budget_investments
      Budget::Investment.where(author_id: user.id)
    end

    def follows
      @follows ||= user.follows.select { |follow| follow.followable.present? }
    end

    def count(filter)
      send(filter).count
    end

    def render_user_partial(filter)
      render "users/#{filter}", "#{filter}": send(filter).order(created_at: :desc).page(page)
    end

    def page
      params[:page]
    end

    def only_active_commentables
      disabled_commentables = []
      disabled_commentables << "Debate" unless Setting["process.debates"]
      disabled_commentables << "Budget::Investment" unless Setting["process.budgets"]
      if disabled_commentables.present?
        all_user_comments.where.not(commentable_type: disabled_commentables)
      else
        all_user_comments
      end
    end

    def all_user_comments
      Comment.not_valuations.not_as_admin_or_moderator.where(user_id: user.id)
    end
end<commit_msg>Refactor code getting a user's comments<commit_after>class Users::PublicActivityComponent < ApplicationComponent
  attr_reader :user
  delegate :current_user, :valid_interests_access?, :current_path_with_query_params, to: :helpers

  def initialize(user)
    @user = user
  end

  def valid_access?
    user.public_activity || authorized_current_user?
  end

  def current_filter
    if valid_filters.include?(params[:filter])
      params[:filter]
    else
      valid_filters.first
    end
  end

  def valid_filters
    @valid_filters ||= [
      ("proposals" if feature?(:proposals)),
      ("debates" if feature?(:debates)),
      ("budget_investments" if feature?(:budgets)),
      "comments",
      ("follows" if valid_interests_access?(user))
    ].compact.select { |filter| send(filter).any? }
  end

  private

    def authorized_current_user?
      current_user == user || current_user&.moderator? || current_user&.administrator?
    end

    def proposals
      Proposal.where(author_id: user.id)
    end

    def debates
      Debate.where(author_id: user.id)
    end

    def comments
      Comment.not_valuations
             .not_as_admin_or_moderator
             .where(user_id: user.id)
             .where.not(commentable_type: disabled_commentables)
             .includes(:commentable)
    end

    def budget_investments
      Budget::Investment.where(author_id: user.id)
    end

    def follows
      @follows ||= user.follows.select { |follow| follow.followable.present? }
    end

    def count(filter)
      send(filter).count
    end

    def render_user_partial(filter)
      render "users/#{filter}", "#{filter}": send(filter).order(created_at: :desc).page(page)
    end

    def page
      params[:page]
    end

    def disabled_commentables
      [
        ("Debate" unless feature?(:debates)),
        ("Budget::Investment" unless feature?(:budgets))
      ].compact
    end
end
{'fraction_non_alphanumeric': 0.10270541082164329, 'fraction_numerical': 0.0, 'mean_word_length': 3.125, 'pattern_counts': {'":': 2, '<': 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': '6607083', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1211, 'n_words': 246}