<commit_before>class QuestionsController < ApplicationController
  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: [:show, :new, :create]

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

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

  def new
    set_up_steps
    @question = Question.new(state: @state_code)
    @question.user = user_signed_in? ? current_user : User.new

    if params[:person]
      @person = Person.connected_to(@state_code).find(params[:person])
      @question.person = @person
    end

    if params[:bill]
      @bill = Bill.connected_to(@state_code).find(params[:bill])
      @question.bill = @bill
    end

    render layout: "data_collection"

    # new!
  end

  def create
    @question = Question.new(params[:question])
    @question.state = @state_code
    @person = @question.person if @question.person_id.present?
    @user = @question.user

    # mongoid nested user
    # saving doesn't prevent question saving for belongs_to
    # when user is invalid, thus we split it up
    if @question.valid? && (user_signed_in? || @user.valid?)
      @question.save
      QuestionMailer.question_posted(@user, @question).deliver
      redirect_to question_path(@state_code, @question, :created => true)
    else
      set_up_steps
      respond_to do |format|
        format.html { render "new" }
      end
    end
  end

  private
  def type
    @type ||= params[:type] || "Person"
  end

  def set_state_code
    @state_code = parent.abbreviation
  end

  def set_up_steps
    @first_step = relevant_steps.first
  end

  # steps may be different if...
  # user is logged in (no sign_up step)
  # person is passed in (no recipient step)
  def relevant_steps
    @relevant_steps = %w(recipient content sign_up confirm)
    @relevant_steps.delete("recipient") if params[:person]
    @relevant_steps.delete("sign_up") if user_signed_in?
    @relevant_steps
  end

  # @todo: questions are currently always created in default session
  # rather than session of owning metadatum
  # which can lead to unexpected results when using "in" query
  # unify databases or make consistent
  def end_of_association_chain
    questions = Question.where(state: parent.abbreviation)
    return questions unless type == "FederalLegislator"

    person_ids = FederalLegislator.connected_to(parent.abbreviation).collect(&:id)
    questions.where(person_ids: { "$in" => person_ids })
  end

  def collection
    @questions ||= end_of_association_chain
      .includes(:user)
      .page(params[:page] || 1)
  end

  def resource
    @question ||= Question.where(state: @state_code).find(params[:id])
  end
end<commit_msg>Fix bug in questions#index for FederalLegislator<commit_after>class QuestionsController < ApplicationController
  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: [:show, :new, :create]

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

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

  def new
    set_up_steps
    @question = Question.new(state: @state_code)
    @question.user = user_signed_in? ? current_user : User.new

    if params[:person]
      @person = Person.connected_to(@state_code).find(params[:person])
      @question.person = @person
    end

    if params[:bill]
      @bill = Bill.connected_to(@state_code).find(params[:bill])
      @question.bill = @bill
    end

    render layout: "data_collection"

    # new!
  end

  def create
    @question = Question.new(params[:question])
    @question.state = @state_code
    @person = @question.person if @question.person_id.present?
    @user = @question.user

    # mongoid nested user
    # saving doesn't prevent question saving for belongs_to
    # when user is invalid, thus we split it up
    if @question.valid? && (user_signed_in? || @user.valid?)
      @question.save
      QuestionMailer.question_posted(@user, @question).deliver
      redirect_to question_path(@state_code, @question, :created => true)
    else
      set_up_steps
      respond_to do |format|
        format.html { render "new" }
      end
    end
  end

  private
  def type
    @type ||= params[:type] || "Person"
  end

  def set_state_code
    @state_code = parent.abbreviation
  end

  def set_up_steps
    @first_step = relevant_steps.first
  end

  # steps may be different if...
  # user is logged in (no sign_up step)
  # person is passed in (no recipient step)
  def relevant_steps
    @relevant_steps = %w(recipient content sign_up confirm)
    @relevant_steps.delete("recipient") if params[:person]
    @relevant_steps.delete("sign_up") if user_signed_in?
    @relevant_steps
  end

  # @todo: questions are currently always created in default session
  # rather than session of owning metadatum
  # which can lead to unexpected results when using "in" query
  # unify databases or make consistent
  def end_of_association_chain
    questions = Question.where(state: parent.abbreviation)
    person_ids = Person.only_type(type)
      .connected_to(parent.abbreviation).collect(&:id)
    questions.where(person_id: { "$in" => person_ids })
  end

  def collection
    @questions ||= end_of_association_chain
      .includes(:user)
      .page(params[:page] || 1)
  end

  def resource
    @question ||= Question.where(state: @state_code).find(params[:id])
  end
end
{'fraction_non_alphanumeric': 0.10118740320082602, 'fraction_numerical': 0.00034417484081913615, 'mean_word_length': 3.4604758250191865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4654900', 'n_tokens_mistral': 1886, 'n_tokens_neox': 1795, 'n_words': 486}