Sha256: bbc22615c5fd2d5889ac8c208fbc84d651a3d4c287cb19d90468ed9245d7c234

Contents?: true

Size: 1.24 KB

Versions: 53

Compression:

Stored size: 1.24 KB

Contents

# Collected responses from one Participant PHQ-9 assessment session.
class PhqAssessment < ActiveRecord::Base
  MIN_QUESTION_SCORE = 0
  MAX_QUESTION_SCORE = 3
  QUESTION_ATTRIBUTES = :q1, :q2, :q3, :q4, :q5, :q6, :q7, :q8, :q9
  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

53 entries across 53 versions & 1 rubygems

Version Path
think_feel_do_engine-3.19.7 app/models/phq_assessment.rb
think_feel_do_engine-3.19.6 app/models/phq_assessment.rb
think_feel_do_engine-3.19.5 app/models/phq_assessment.rb
think_feel_do_engine-3.19.4 app/models/phq_assessment.rb
think_feel_do_engine-3.19.3 app/models/phq_assessment.rb
think_feel_do_engine-3.19.2 app/models/phq_assessment.rb
think_feel_do_engine-3.19.1 app/models/phq_assessment.rb
think_feel_do_engine-3.19.0 app/models/phq_assessment.rb
think_feel_do_engine-3.18.0 app/models/phq_assessment.rb
think_feel_do_engine-3.17.2 app/models/phq_assessment.rb
think_feel_do_engine-3.17.1 app/models/phq_assessment.rb
think_feel_do_engine-3.17.0 app/models/phq_assessment.rb
think_feel_do_engine-3.16.3 app/models/phq_assessment.rb
think_feel_do_engine-3.16.2 app/models/phq_assessment.rb
think_feel_do_engine-3.16.1 app/models/phq_assessment.rb
think_feel_do_engine-3.15.7 app/models/phq_assessment.rb
think_feel_do_engine-3.16.0 app/models/phq_assessment.rb
think_feel_do_engine-3.15.6 app/models/phq_assessment.rb
think_feel_do_engine-3.15.5 app/models/phq_assessment.rb
think_feel_do_engine-3.15.4 app/models/phq_assessment.rb