<commit_before>class QuestionsController < ApplicationController
  before_filter :force_http

  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, :destroy
  custom_actions resource: [:need_signatures, :have_answers, :need_answers, :recent]

  before_filter :set_state_code, only: [:show, :new, :create, :need_signatures,
                                        :have_answers, :need_answers, :recent,
                                        :destroy]
  before_filter :set_question_person_id, only: :create
  before_filter :set_is_unaffiliated, only: [:index, :show]
  before_filter :redirect_to_unaffiliated_route_if_necessary, only: [:index, :show]
  before_filter :authenticate_user!, only: [:destroy]
  before_filter :check_can_destroy_question, only: :destroy

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

  def need_signatures
    @questions = end_of_association_chain.where(threshold_met: false)
      .includes(:user)
      .page(page)
    tab "need_signatures"
  end

  def have_answers
    @questions = end_of_association_chain.any_in(_id: Answer.all.distinct("question_id"))
      .includes(:user)
      .page(page)
    tab "have_answers"
  end

  def need_answers
    @questions = end_of_association_chain.not_in(_id: Answer.all.distinct("question_id"))
      .includes(:user)
      .page(page)
    tab "need_answers"
  end

  def recent
    @questions = end_of_association_chain.desc(:issued_at)
      .includes(:user)
      .page(page)
    tab "recent"
  end

  def show
    @user = user_signed_in? ? current_user : User.new
    show! do
      @recent_signatures = @question.signatures
        .includes(:user)
        .where(:user_id.nin => [@question.user_id])
        .order_by(created_at: "DESC").limit(5)
    end

  rescue Mongoid::Errors::DocumentNotFound => error
    question_different_jurisdiction = Question.where(id: params[:id]).first

    if question_different_jurisdiction
      correct_jurisdiction = question_different_jurisdiction.state
      if correct_jurisdiction == Metadatum::Unaffiliated::ABBREVIATION
        redirect_to unaffiliated_question_path(params[:id], share: params[:share])
      else
        redirect_to question_path(correct_jurisdiction, params[:id], share: params[:share])
      end
    else
      raise error
    end
  end

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

    if params[:person]
      @person = Person.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"
  end

  def create
    @question = Question.new(params[:question])
    if @question.person_id.present?
      @person = @question.person
      @question.state = @person.state
    else
      @question.state = @state_code
    end
    @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

      if @question.state == Metadatum::Unaffiliated::ABBREVIATION
        PersonMailer.notify_staff_new_from_twitter(@person).deliver
      end

      redirect_to question_path(@question.state, @question, share: true)
    else<commit_msg>Limit staff notify of new twitter person<commit_after>class QuestionsController < ApplicationController
  before_filter :force_http

  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, :destroy
  custom_actions resource: [:need_signatures, :have_answers, :need_answers, :recent]

  before_filter :set_state_code, only: [:show, :new, :create, :need_signatures,
                                        :have_answers, :need_answers, :recent,
                                        :destroy]
  before_filter :set_question_person_id, only: :create
  before_filter :set_is_unaffiliated, only: [:index, :show]
  before_filter :redirect_to_unaffiliated_route_if_necessary, only: [:index, :show]
  before_filter :authenticate_user!, only: [:destroy]
  before_filter :check_can_destroy_question, only: :destroy

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

  def need_signatures
    @questions = end_of_association_chain.where(threshold_met: false)
      .includes(:user)
      .page(page)
    tab "need_signatures"
  end

  def have_answers
    @questions = end_of_association_chain.any_in(_id: Answer.all.distinct("question_id"))
      .includes(:user)
      .page(page)
    tab "have_answers"
  end

  def need_answers
    @questions = end_of_association_chain.not_in(_id: Answer.all.distinct("question_id"))
      .includes(:user)
      .page(page)
    tab "need_answers"
  end

  def recent
    @questions = end_of_association_chain.desc(:issued_at)
      .includes(:user)
      .page(page)
    tab "recent"
  end

  def show
    @user = user_signed_in? ? current_user : User.new
    show! do
      @recent_signatures = @question.signatures
        .includes(:user)
        .where(:user_id.nin => [@question.user_id])
        .order_by(created_at: "DESC").limit(5)
    end

  rescue Mongoid::Errors::DocumentNotFound => error
    question_different_jurisdiction = Question.where(id: params[:id]).first

    if question_different_jurisdiction
      correct_jurisdiction = question_different_jurisdiction.state
      if correct_jurisdiction == Metadatum::Unaffiliated::ABBREVIATION
        redirect_to unaffiliated_question_path(params[:id], share: params[:share])
      else
        redirect_to question_path(correct_jurisdiction, params[:id], share: params[:share])
      end
    else
      raise error
    end
  end

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

    if params[:person]
      @person = Person.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"
  end

  def create
    @question = Question.new(params[:question])
    if @question.person_id.present?
      @person = @question.person
      @question.state = @person.state
    else
      @question.state = @state_code
    end
    @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

      if @question.state == Metadatum::Unaffiliated::ABBREVIATION &&
          @person.questions.count < 2
        PersonMailer.notify_staff_new_from_twitter(@person).deliver
      end

      redirect_to question_path(@question.state, @question, share: true)
    else
{'fraction_non_alphanumeric': 0.10245464247598719, 'fraction_numerical': 0.00040021344717182497, 'mean_word_length': 3.3867758923346987, 'pattern_counts': {'":': 0, '<': 6, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '917389', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2301, 'n_words': 493}