<commit_before>class QuestionsController < ApplicationController
  FULL_NEW_QUESTION_STEPS = %w(recipient content sign_up confirm)

  inherit_resources
  belongs_to :jurisdiction, parent_class: Metadatum, finder: :find_by_abbreviation, param: :jurisdiction
  respond_to :html
  respond_to :js, only: :index
  actions :index, :show, :new, :create

  before_filter :set_state_code, only: [:new, :create]

  def index
    index! do |format|
      format.js {render partial: 'page'}
    end
  end

  def show
    @user = User.new unless user_signed_in?
    show!
  end

  def new
    @step = relevant_steps.first
    session[:question_current_step] = @step
    @step_details = step_details

    session[:question_params] ||= { state: @state_code }
    @question = Question.new(session[:question_params])
    @question.user = user_signed_in? ? @user : User.new

    if params[:person]
      @person = Person.in(@state_code).find(params[:person])
      @question.person = @person
      session[:ask_person] = @person.id
    end

    new!
  end

  def create
    session[:question_params].deep_merge!(params[:question])
    @question = Question.new(session[:question_params])
    @person = @question.person
    @user = @question.user
    @step = session[:question_current_step]

    # TODO: step validation needs work
    if @step == relevant_steps.last
      if !user_signed_in? && @user.valid?
        @user.save
        @question.user = @user # necessary? check @question.user_id to see if it is updated without doing this
      end
      @question.save if @question.valid?
    else
      @step = next_step(@step)
      session[:question_current_step] = @step
    end

    @step_details = step_details

    if @question.new_record?
      create! do |format|
        format.html {render 'new'}
      end
    else<commit_msg>WIP - add data stubbing until recipient choosing is finished<commit_after>class QuestionsController < ApplicationController
  FULL_NEW_QUESTION_STEPS = %w(recipient content sign_up confirm)

  inherit_resources
  belongs_to :jurisdiction, parent_class: Metadatum, finder: :find_by_abbreviation, param: :jurisdiction
  respond_to :html
  respond_to :js, only: :index
  actions :index, :show, :new, :create

  before_filter :set_state_code, only: [:new, :create]

  def index
    index! do |format|
      format.js {render partial: 'page'}
    end
  end

  def show
    @user = User.new unless user_signed_in?
    show!
  end

  def new
    # TODO: decide if clearing session data
    # should always happen here
    clear_session_values_for_question

    @step = relevant_steps.first
    session[:question_current_step] = @step
    @step_details = step_details

    session[:question_params] ||= { state: @state_code }
    @question = Question.new(session[:question_params])
    @question.user = user_signed_in? ? @user : User.new

    if params[:person]
      @person = Person.in(@state_code).find(params[:person])
      @question.person = @person
      session[:ask_person] = @person.id
    end

    new!
  end

  def create
    session[:question_params].deep_merge!(params[:question])
    @question = Question.new(session[:question_params])
    @person = @question.person if @question.person_id.present?
    @user = @question.user
    @step = session[:question_current_step]

    # TODO: step validation needs work
    if @step == relevant_steps.last
      if !user_signed_in? && @user.valid?
        @user.save
        @question.user = @user # necessary? check @question.user_id to see if it is updated without doing this
      end
      @question.save if @question.valid?
    else
      @step = next_step(@step)
      session[:question_current_step] = @step
      # TODO: take out below data stubbing when recipient choosing is in order
      # data stubbing
      if @step == relevant_steps.last
        @question.person = Person.in(parent.abbreviation).last
      end
      # end stubbing
    end

    @step_details = step_details

    if @question.new_record?
      create! do |format|
        format.html {render 'new'}
      end
    else
{'fraction_non_alphanumeric': 0.1018450184501845, 'fraction_numerical': 0.0, 'mean_word_length': 3.284510010537408, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3733213', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1228, 'n_words': 320}