<commit_before>class Valuation::BudgetInvestmentsController < Valuation::BaseController
  include FeatureFlags
  feature_flag :budgets

  before_action :restrict_access_to_assigned_items, only: [:show, :edit, :valuate]
  before_action :load_budget
  before_action :load_investment, only: [:show, :edit, :valuate]

  has_filters %w{valuating valuation_finished}, only: :index

  load_and_authorize_resource :investment, class: "Budget::Investment"

  def index
    @heading_filters = heading_filters
    @investments = if current_user.valuator? && @budget.present?
                     @budget.investments.scoped_filter(params_for_current_valuator, @current_filter)
                            .order(cached_votes_up: :desc)
                            .page(params[:page])
                   else
                     Budget::Investment.none.page(params[:page])
                   end
  end

  def valuate
    if valid_price_params? && @investment.update(valuation_params)

      if @investment.unfeasible_email_pending?
        @investment.send_unfeasible_email
      end

      Activity.log(current_user, :valuate, @investment)
      notice = t('valuation.budget_investments.notice.valuate')
      redirect_to valuation_budget_budget_investment_path(@budget, @investment), notice: notice
    else
      render action: :edit
    end
  end

  private

    def load_budget
      @budget = Budget.find(params[:budget_id])
    end

    def load_investment
      @investment = @budget.investments.find params[:id]
    end

    def heading_filters
      investments = @budget.investments.by_valuator(current_user.valuator.try(:id))
                           .valuation_open.select(:heading_id).all.to_a

      [ { name: t('valuation.budget_investments.index.headings_filter_all'),
          id: nil,
          pending_count: investments.size
        }
      ] + Budget::Heading.where(id: investments.map(&:heading_id).uniq).order(name: :asc).collect do |h|
        { name: h.name,
          id: h.id,
          pending_count: investments.count{|x| x.heading_id == h.id}
        }
      end
    end

    def params_for_current_valuator
      params = { valuator_id: current_user.valuator.id, budget_id: @budget.id }
      Budget::Investment.filter_params(params).merge(params)
    end

    def valuation_params
      params.require(:budget_investment).permit(:price, :price_first_year, :price_explanation,
                                                :feasibility, :unfeasibility_explanation,
                                                :duration, :valuation_finished, :internal_comments)
    end<commit_msg>Fix bad line break<commit_after>class Valuation::BudgetInvestmentsController < Valuation::BaseController
  include FeatureFlags
  feature_flag :budgets

  before_action :restrict_access_to_assigned_items, only: [:show, :edit, :valuate]
  before_action :load_budget
  before_action :load_investment, only: [:show, :edit, :valuate]

  has_filters %w{valuating valuation_finished}, only: :index

  load_and_authorize_resource :investment, class: "Budget::Investment"

  def index
    @heading_filters = heading_filters
    @investments = if current_user.valuator? && @budget.present?
                     @budget.investments.scoped_filter(params_for_current_valuator, @current_filter)
                            .order(cached_votes_up: :desc)
                            .page(params[:page])
                   else
                     Budget::Investment.none.page(params[:page])
                   end
  end

  def valuate
    if valid_price_params? && @investment.update(valuation_params)

      if @investment.unfeasible_email_pending?
        @investment.send_unfeasible_email
      end

      Activity.log(current_user, :valuate, @investment)
      notice = t('valuation.budget_investments.notice.valuate')
      redirect_to valuation_budget_budget_investment_path(@budget, @investment), notice: notice
    else
      render action: :edit
    end
  end

  private

    def load_budget
      @budget = Budget.find(params[:budget_id])
    end

    def load_investment
      @investment = @budget.investments.find params[:id]
    end

    def heading_filters
      investments = @budget.investments.by_valuator(current_user.valuator.try(:id))
                           .valuation_open.select(:heading_id).all.to_a

      [ { name: t('valuation.budget_investments.index.headings_filter_all'),
          id: nil,
          pending_count: investments.size
        }
      ] + Budget::Heading.where(id: investments.map(&:heading_id).uniq).order(name: :asc).collect do |h|
        { name: h.name,
          id: h.id,
          pending_count: investments.count{|x| x.heading_id == h.id}
        }
      end
    end

    def params_for_current_valuator
      Budget::Investment.filter_params(params).merge(valuator_id: current_user.valuator.id,
                                                     budget_id: @budget.id)
    end

    def valuation_params
      params.require(:budget_investment).permit(:price, :price_first_year, :price_explanation,
                                                :feasibility, :unfeasibility_explanation,
                                                :duration, :valuation_finished, :internal_comments)
    end
{'fraction_non_alphanumeric': 0.09348819497334349, 'fraction_numerical': 0.0, 'mean_word_length': 2.5589430894308944, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2011655', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1539, 'n_words': 260}