Class: TopicExam

Inherits:
ApplicationRecord show all
Defined in:
app/models/topic_exam.rb

Overview

== Schema Information

Table name: topic_exams
Database name: primary

id :bigint not null, primary key
active :boolean default(FALSE), not null
applies_to_customers :boolean default(FALSE), not null
applies_to_employees :boolean default(FALSE), not null
description :text
min_score_to_pass :integer
name :string not null
position :integer
training_module :boolean default(FALSE), not null
course_id :integer not null
topic_category_id :integer

Foreign Keys

fk_rails_... (topic_category_id => topic_categories.id)

A course's final-exam definition: an ordered set of question Topics and
the absolute pass threshold (min_score_to_pass). Learner attempts are
CourseExam records that snapshot these questions as CustomerTopic
copies at request time.

Constant Summary

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#min_score_to_passObject (readonly)



44
# File 'app/models/topic_exam.rb', line 44

validates :min_score_to_pass, presence: true

Class Method Details

.activeActiveRecord::Relation<TopicExam>

A relation of TopicExams that are active. Active Record Scope

Returns:

See Also:



41
# File 'app/models/topic_exam.rb', line 41

scope :active, -> { where(active: true) }

.select_optionsArray<Array(String, Integer)>

Name-labelled options for exam select inputs.

Returns:

  • (Array<Array(String, Integer)>)


49
50
51
# File 'app/models/topic_exam.rb', line 49

def self.select_options
  order(:name).map { |te| [te.to_s, te.id] }
end

.sortedActiveRecord::Relation<TopicExam>

A relation of TopicExams that are sorted. Active Record Scope

Returns:

See Also:



42
# File 'app/models/topic_exam.rb', line 42

scope :sorted, -> { order(:position) }

Instance Method Details

#courseCourse

The course this exam certifies.

Returns:

See Also:



30
# File 'app/models/topic_exam.rb', line 30

belongs_to :course, optional: true

#customer_topicsActiveRecord::Relation<CustomerTopic>

Learner answer copies across all attempts of this exam.

Returns:

See Also:



35
# File 'app/models/topic_exam.rb', line 35

has_many :customer_topics

#max_scoreInteger

Highest attainable score: the sum of every question's positive answer
points (multi-select questions can carry several scoring answers) from active topics only.
Compare against #min_score_to_pass — below it the exam is unpassable.

Returns:

  • (Integer)


63
64
65
# File 'app/models/topic_exam.rb', line 63

def max_score
  topics.active.sum { |topic| topic.topic_responses.sum { |tr| [tr.score.to_i, 0].max } }
end

#to_sString

Returns the exam name.

Returns:

  • (String)

    the exam name



54
55
56
# File 'app/models/topic_exam.rb', line 54

def to_s
  name
end

#topicsActiveRecord::Relation<Topic>

The exam's questions, in display order.

Returns:

  • (ActiveRecord::Relation<Topic>)

See Also:



33
# File 'app/models/topic_exam.rb', line 33

has_many :topics, -> { order(:position) }, inverse_of: :topic_exam