<commit_before>class Question < ApplicationRecord
  has_many :answers
  belongs_to :quiz

  translates :content

  validates :content_i18n, presence: true
  validates :correct_answer, inclusion: { in: ->(question) { question.options } },
    if: ->(question) { question.options.length > 0}

  def self.requiring_response_from reader, in_group:
    # where.not(id: Answer.by_reader(reader).select(:question_id))
    where <<~SQL, reader_id: reader.id, group_id: in_group.id
      (
        SELECT COUNT(answers.id) FROM answers
        WHERE answers.question_id = questions.id
          AND answers.reader_id = :reader_id
      ) < (
        SELECT deployments.answers_needed FROM deployments
        WHERE deployments.group_id = :group_id
          AND deployments.quiz_id = questions.quiz_id
      )
    SQL
  end

  def multiple_choice?
    question_type == :multiple_choice
  end

  def open_ended?
    question_type == :open_ended
  end

  private
  def question_type
    options.empty? ? :open_ended : :multiple_choice
  end
end
<commit_msg>Fixes Question#requiring_response_from when no custom questions (#51)<commit_after>class Question < ApplicationRecord
  has_many :answers
  belongs_to :quiz

  translates :content

  validates :content_i18n, presence: true
  validates :correct_answer, inclusion: { in: ->(question) { question.options } },
    if: ->(question) { question.options.length > 0}

  def self.requiring_response_from reader, in_group:
    # where.not(id: Answer.by_reader(reader).select(:question_id))
    joins(:quiz).where <<~SQL, reader_id: reader.id, group_id: in_group.id
      (
        SELECT COUNT(answers.id) FROM answers
        WHERE answers.question_id = questions.id
          AND answers.reader_id = :reader_id
      ) < (
        SELECT deployments.answers_needed FROM deployments
        WHERE deployments.group_id = :group_id
          AND deployments.case_id = quizzes.case_id
      )
    SQL
  end

  def multiple_choice?
    question_type == :multiple_choice
  end

  def open_ended?
    question_type == :open_ended
  end

  private
  def question_type
    options.empty? ? :open_ended : :multiple_choice
  end
end

{'fraction_non_alphanumeric': 0.08985641500694766, 'fraction_numerical': 0.0037054191755442334, 'mean_word_length': 3.2857142857142856, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '2934278', 'n_tokens_mistral': 701, 'n_tokens_neox': 669, 'n_words': 156}