<commit_before>class CapitalPlansController < OrganizationAwareController

  before_action :get_capital_plan, only: [:show]

  def index
    # state view

    authorize! :read_all, CapitalPlan
    add_breadcrumb 'Capital Plans', capital_plans_path


    @capital_plans = []
    @organization_list.each do |org|
      if Asset.operational.where(organization_id: org).count > 0
        plan = CapitalPlan.current_plan(org)
        @capital_plans << plan
        run_system_actions(plan)
      end
    end
  end

  def show
    authorize! :read, @capital_plan

    # pagination if needed
    org_list = @organization_list.select{ |o| Asset.operational.where(organization_id: o).count > 0 }
    if org_list.count > 1
      if can? :read_all, CapitalPlan
        add_breadcrumb 'Capital Plans', capital_plans_path
      end

      @total_rows = org_list.count
      org_idx = org_list.index(@capital_plan.organization_id)
      @row_number = org_idx+1
      @prev_record_key = CapitalPlan.current_plan(org_list[org_idx-1]).object_key if org_idx > 0
      @next_record_key = CapitalPlan.current_plan(org_list[org_idx+1]).object_key if org_idx < org_list.count - 1

      @prev_record_path = @prev_record_key.nil? ? "#" : capital_plan_path(@prev_record_key)
      @next_record_path = @next_record_key.nil? ? "#" : capital_plan_path(@next_record_key)
    end

    add_breadcrumb @capital_plan, capital_plan_path(@capital_plan)

    run_system_actions(@capital_plan)

  end

  def complete_actions

    actions = CapitalPlanAction.where('(capital_plan_actions.object_key IN (?) AND capital_plan_actions.completed_at IS NULL) OR (capital_plan_actions.object_key IN (?) AND capital_plan_actions.completed_at IS NOT NULL)', params[:targets].split(','), params[:undo_targets].split(','))
    actions.each do |action|
      authorize! :update, action.capital_plan
      action.capital_plan_action_type.class_name.constantize.new(capital_plan_action: action, user: current_user).run
    end

    respond_to do |format|
      format.html { redirect_to :back }
      format.js { render text: URI(request.referer) }
    end
  end

  def get_checkboxes
    checked = params[:checked_targets].split(',')

    unless checked.any?
      checked = [nil]
    end

    capital_plan = CapitalPlan.find_by(object_key: params[:id])

    checkbox_params = Hash.new<commit_msg>[fixes#146702635] complete undo plan actions from last to first<commit_after>class CapitalPlansController < OrganizationAwareController

  before_action :get_capital_plan, only: [:show]

  def index
    # state view

    authorize! :read_all, CapitalPlan
    add_breadcrumb 'Capital Plans', capital_plans_path


    @capital_plans = []
    @organization_list.each do |org|
      if Asset.operational.where(organization_id: org).count > 0
        plan = CapitalPlan.current_plan(org)
        @capital_plans << plan
        run_system_actions(plan)
      end
    end
  end

  def show
    authorize! :read, @capital_plan

    # pagination if needed
    org_list = @organization_list.select{ |o| Asset.operational.where(organization_id: o).count > 0 }
    if org_list.count > 1
      if can? :read_all, CapitalPlan
        add_breadcrumb 'Capital Plans', capital_plans_path
      end

      @total_rows = org_list.count
      org_idx = org_list.index(@capital_plan.organization_id)
      @row_number = org_idx+1
      @prev_record_key = CapitalPlan.current_plan(org_list[org_idx-1]).object_key if org_idx > 0
      @next_record_key = CapitalPlan.current_plan(org_list[org_idx+1]).object_key if org_idx < org_list.count - 1

      @prev_record_path = @prev_record_key.nil? ? "#" : capital_plan_path(@prev_record_key)
      @next_record_path = @next_record_key.nil? ? "#" : capital_plan_path(@next_record_key)
    end

    add_breadcrumb @capital_plan, capital_plan_path(@capital_plan)

    run_system_actions(@capital_plan)

  end

  def complete_actions

    actions = CapitalPlanAction.where('capital_plan_actions.object_key IN (?) AND capital_plan_actions.completed_at IS NULL', params[:targets].split(','))
    actions.each do |action|
      authorize! :update, action.capital_plan
      action.capital_plan_action_type.class_name.constantize.new(capital_plan_action: action, user: current_user).run
    end

    undo_actions = CapitalPlanAction.unscoped.where('capital_plan_actions.object_key IN (?) AND capital_plan_actions.completed_at IS NOT NULL', params[:undo_targets].split(',')).order('capital_plan_modules.sequence DESC', 'capital_plan_actions.sequence DESC')
    undo_actions.each do |action|
      authorize! :update, action.capital_plan
      action.capital_plan_action_type.class_name.constantize.new(capital_plan_action: action, user: current_user).run
    end

    respond_to do |format|
      format.html { redirect_to :back }
      format.js { render text: URI(request.referer) }
    end
  end

  def get_checkboxes
    checked = params[:checked_targets].split(',')

    unless checked.any?
      checked = [nil]
    end

    capital_plan = CapitalPlan.find_by(object_key: params[:id])

    checkbox_params = Hash.new
{'fraction_non_alphanumeric': 0.09742176736862822, 'fraction_numerical': 0.00492029128124385, 'mean_word_length': 3.967741935483871, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1876155', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1605, 'n_words': 311}