<commit_before>class ChecklistController < ApplicationController
  layout "finder_layout"

  def show
    if redirect_to_results?
      redirect_to_result_page
    else
      @checklist_questions = ChecklistQuestionsPresenter.new(page, filtered_params, questions)
      if @checklist_questions.get_next_page != page
        redirect_to find_brexit_guidance_path(filtered_params.merge(page: @checklist_questions.get_next_page))
      else
        render "checklist/show"
      end
    end
  end

  def results
    @checklist = ChecklistAnswers.new(request.query_parameters.except(:page))
    render "checklist/results"
  end

private

  def qa_config
    @qa_config ||= YAML.load_file("lib/find_brexit_guidance.yaml")
  end

  ###
  # Page title and breadcrumbs
  ###

  def title
    qa_config["title"]
  end
  helper_method :title

  def breadcrumbs
    [{ title: "Home", url: "/" }]
  end
  helper_method :breadcrumbs

  ###
  # Questions
  ###

  def questions
    @questions ||= begin
      qa_config["questions"].map do |question|
        ChecklistQuestion.new(question)
      end
    end
  end

  ###
  # Current page
  ###

  def page
    @page ||= begin
      params.permit(:page)
      params[:page].to_i.clamp(1, questions.length + 1)
    end
  end

  ###
  # Navigation
  ###
  def next_page
    page + 1
  end
  helper_method :next_page

  def next_page_url
    request.path
  end
  helper_method :next_page_url

  def skip_link_url
    page_number = { page: next_page }
    next_page_url + "?" + filtered_params.merge(page_number).to_query
  end
  helper_method :skip_link_url

  ###
  # Redirect
  ###

  def redirect_to_results?
    page == questions.length + 1
  end

  def redirect_to_result_page
    redirect_to find_brexit_guidance_results_path(filtered_params)
  end

  def filtered_params
    request.query_parameters.except(:page)
  end
  helper_method :filtered_params
end<commit_msg>tidies up redirect to next question logic<commit_after>class ChecklistController < ApplicationController
  layout "finder_layout"

  def show
    return redirect_to_result_page if redirect_to_results?

    @checklist_questions = ChecklistQuestionsPresenter.new(page, filtered_params, questions)
    return redirect_to_next_question if redirect_to_next_question?

    render "checklist/show"
  end

  def results
    @checklist = ChecklistAnswers.new(request.query_parameters.except(:page))
    render "checklist/results"
  end

private

  def qa_config
    @qa_config ||= YAML.load_file("lib/find_brexit_guidance.yaml")
  end

  ###
  # Redirect
  ###

  def redirect_to_next_question?
    @checklist_questions.get_next_page != page
  end

  def redirect_to_next_question
    redirect_to find_brexit_guidance_path(filtered_params.merge(
                                            page: @checklist_questions.get_next_page
                                          ))
  end

  def redirect_to_results?
    page == questions.length + 1
  end

  def redirect_to_result_page
    redirect_to find_brexit_guidance_results_path(filtered_params)
  end

  ###
  # Filtered params
  ###

  def filtered_params
    request.query_parameters.except(:page)
  end
  helper_method :filtered_params

  ###
  # Page title and breadcrumbs
  ###

  def title
    qa_config["title"]
  end
  helper_method :title

  def breadcrumbs
    [{ title: "Home", url: "/" }]
  end
  helper_method :breadcrumbs

  ###
  # Questions
  ###

  def questions
    @questions ||= begin
      qa_config["questions"].map do |question|
        ChecklistQuestion.new(question)
      end
    end
  end

  ###
  # Current page
  ###

  def page
    @page ||= begin
      params.permit(:page)
      params[:page].to_i.clamp(1, questions.length + 1)
    end
  end

  ###
  # Navigation
  ###
  def next_page
    page + 1
  end
  helper_method :next_page

  def next_page_url
    request.path
  end
  helper_method :next_page_url

  def skip_link_url
    page_number = { page: next_page }
    next_page_url + "?" + filtered_params.merge(page_number).to_query
  end
  helper_method :skip_link_url
end
{'fraction_non_alphanumeric': 0.0854890367085489, 'fraction_numerical': 0.001970928800197093, 'mean_word_length': 3.080402010050251, '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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3615896', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1299, 'n_words': 264}