Sha256: ffc4d6b7b5cdbfcc1e89a0fdf0328a55d916392944264348fb05e01c27a58e33

Contents?: true

Size: 1.34 KB

Versions: 13

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true
# Collected responses from one Participant PHQ-9 assessment session.
class PhqAssessment < ActiveRecord::Base
  MIN_QUESTION_SCORE = 0
  MAX_QUESTION_SCORE = 3
  # rubocop:disable Style/MutableConstant
  QUESTION_ATTRIBUTES = :q1, :q2, :q3, :q4, :q5, :q6, :q7, :q8, :q9
  # rubocop:enable Style/MutableConstant
  SUICIDAL_SCORE = 3

  belongs_to :participant

  validates :participant, :release_date, presence: true
  validates :release_date, uniqueness: { scope: :participant_id }
  validate :scores_valid

  scope :recenty_released, -> { order(release_date: :desc) }

  def self.most_recent
    recenty_released.first
  end

  def answered_questions
    @answered_questions ||= remove_nils
  end

  def completed?
    number_answered == QUESTION_ATTRIBUTES.count
  end

  def number_answered
    answered_questions.count
  end

  def score
    answered_questions.inject(:+)
  end

  def suicidal?
    q9 == SUICIDAL_SCORE
  end

  private

  def remove_nils
    QUESTION_ATTRIBUTES.map { |answer| self[answer] }.compact
  end

  def scores_valid
    return unless number_answered > 0

    if answered_questions.min < MIN_QUESTION_SCORE ||
       answered_questions.max > MAX_QUESTION_SCORE
      errors.add(:base, "scores must be between #{MIN_QUESTION_SCORE} " \
                        "and #{MAX_QUESTION_SCORE} inclusive")
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
think_feel_do_engine-3.22.9 app/models/phq_assessment.rb
think_feel_do_engine-3.22.8 app/models/phq_assessment.rb
think_feel_do_engine-3.22.7 app/models/phq_assessment.rb
think_feel_do_engine-3.22.6 app/models/phq_assessment.rb
think_feel_do_engine-3.22.5 app/models/phq_assessment.rb
think_feel_do_engine-3.22.4 app/models/phq_assessment.rb
think_feel_do_engine-3.22.2 app/models/phq_assessment.rb
think_feel_do_engine-3.22.1 app/models/phq_assessment.rb
think_feel_do_engine-3.22.0 app/models/phq_assessment.rb
think_feel_do_engine-3.21.2 app/models/phq_assessment.rb
think_feel_do_engine-3.21.1 app/models/phq_assessment.rb
think_feel_do_engine-3.21.0 app/models/phq_assessment.rb
think_feel_do_engine-3.20.1 app/models/phq_assessment.rb