Class: ContactTrainingTopic
- Inherits:
-
PartyTopic
- Object
- ActiveRecord::Base
- ApplicationRecord
- PartyTopic
- ContactTrainingTopic
- Defined in:
- app/models/contact_training_topic.rb
Overview
== Schema Information
Table name: party_topics
Database name: primary
id :integer not null, primary key
comments :text
due_on :datetime
follow_up :boolean
instruction_type :integer
position :integer
state :string(20)
state_created_at :datetime
type :string(20)
created_at :datetime not null
updated_at :datetime not null
course_category_id :integer
course_enrollment_id :integer
course_exam_id :integer
creator_id :integer
party_id :integer not null
survey_enrollment_id :integer
topic_id :integer not null
trainer_party_id :integer
updater_id :integer
Indexes
index_party_topic_enrollment_and_exam (party_id,topic_id,course_enrollment_id,course_exam_id)
index_party_topics_on_topic_id (topic_id)
index_party_topics_on_type (type)
party_topics_trainer_party_id_idx (trainer_party_id)
Foreign Keys
fk_rails_... (party_id => parties.id) ON DELETE => cascade
fk_rails_... (topic_id => topics.id) ON DELETE => cascade
fk_rails_... (trainer_party_id => parties.id) ON DELETE => nullify
Constant Summary
Constants inherited from PartyTopic
Constants included from Models::Auditable
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
- #state ⇒ Object readonly
Belongs to collapse
Methods inherited from PartyTopic
Methods included from Models::Auditable
Delegated Instance Attributes collapse
-
#customer ⇒ Object
Alias for Contact#customer.
-
#title ⇒ Object
Alias for Topic#title.
-
#topic_thumbnail ⇒ Object
Alias for Topic#topic_thumbnail.
-
#uploads ⇒ Object
Alias for Topic#uploads.
Class Method Summary collapse
-
.active ⇒ ActiveRecord::Relation<ContactTrainingTopic>
A relation of ContactTrainingTopics that are active.
- .apply_state_order(relation) ⇒ Object
- .assigned_topic_select_options ⇒ Object
- .assigned_trainee_select_options ⇒ Object
- .assigned_trainer_select_options ⇒ Object
-
.auto_assign(contact) ⇒ Object
This method will find all missing contact training topics for a given contact and create them.
-
.create_contact_training_topics(contact, topics) ⇒ Object
Builds employee topics for a given employee.
-
.find_missing_topics(contact) ⇒ Object
Find all topics that are not already in this employee's party topics.
- .instruction_types_select_options ⇒ Object
- .state_order ⇒ Object
- .states_for_select ⇒ Object
-
.topics_for_contact(contact) ⇒ Object
Find all applicable topics for an employee based on their role.
Instance Method Summary collapse
Methods inherited from PartyTopic
assigned, #complete, completed, exams, for_topic_category, in_progress, no_exams, ongoing, sorted
Methods included from Models::Auditable
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Models::EventPublishable
Instance Attribute Details
#state ⇒ Object (readonly)
49 |
# File 'app/models/contact_training_topic.rb', line 49 validates :state, :party, :topic, presence: true |
Class Method Details
.active ⇒ ActiveRecord::Relation<ContactTrainingTopic>
A relation of ContactTrainingTopics that are active. Active Record Scope
51 |
# File 'app/models/contact_training_topic.rb', line 51 scope :active, -> { where.not(state: 'cancelled') } |
.apply_state_order(relation) ⇒ Object
145 146 147 |
# File 'app/models/contact_training_topic.rb', line 145 def self.apply_state_order(relation) relation.group_by(&:state).slice(*state_order).values.flatten end |
.assigned_topic_select_options ⇒ Object
100 101 102 103 104 105 106 |
# File 'app/models/contact_training_topic.rb', line 100 def self. Topic.joins(:contact_training_topics) .includes(:contact_training_topics, :topic_category) .where(training_module: true) .order(TopicCategory[:name], Topic[:title]) .map { |t| ["#{t.topic_category.name} > #{t.title}", t.id] } end |
.assigned_trainee_select_options ⇒ Object
108 109 110 |
# File 'app/models/contact_training_topic.rb', line 108 def self. Contact.joins(:contact_training_topics).uniq.order(:full_name).map { |e| [e.full_name, e.id] } end |
.assigned_trainer_select_options ⇒ Object
96 97 98 |
# File 'app/models/contact_training_topic.rb', line 96 def self. Employee.joins(:trainer_employee_topics).order(:full_name).map { |e| [e.full_name, e.id] }.uniq end |
.auto_assign(contact) ⇒ Object
This method will find all missing contact training topics for a given contact and create them
113 114 115 116 |
# File 'app/models/contact_training_topic.rb', line 113 def self.auto_assign(contact) topics = find_missing_topics(contact) create_contact_training_topics(employee, topics) end |
.create_contact_training_topics(contact, topics) ⇒ Object
Builds employee topics for a given employee
132 133 134 135 136 137 138 139 |
# File 'app/models/contact_training_topic.rb', line 132 def self.create_contact_training_topics(contact, topics) transaction do topics.each do |t| contact.contact_training_topics.create!(topic: t, state: 'assigned') end end contact.contact_training_topics end |
.find_missing_topics(contact) ⇒ Object
Find all topics that are not already in this employee's party topics
119 120 121 |
# File 'app/models/contact_training_topic.rb', line 119 def self.find_missing_topics(contact) topics_for_contact(contact).where.not(id: contact.contact_training_topics.pluck(:topic_id)) end |
.instruction_types_select_options ⇒ Object
92 93 94 |
# File 'app/models/contact_training_topic.rb', line 92 def self. instruction_types.keys.map { |k| [k.humanize, k] } end |
.state_order ⇒ Object
141 142 143 |
# File 'app/models/contact_training_topic.rb', line 141 def self.state_order state_machines[:state].states.map { |s| s.name.to_s } end |
.states_for_select ⇒ Object
88 89 90 |
# File 'app/models/contact_training_topic.rb', line 88 def self.states_for_select state_machines[:state].states.map { |s| [s.human_name, s.name] } end |
.topics_for_contact(contact) ⇒ Object
Find all applicable topics for an employee based on their role
124 125 126 127 128 129 |
# File 'app/models/contact_training_topic.rb', line 124 def self.topics_for_contact(contact) # TODO: Apply filtering of all sorts here Topic .active .where(applies_to_contact: true, training_module: true) end |
Instance Method Details
#contact ⇒ Contact
43 |
# File 'app/models/contact_training_topic.rb', line 43 belongs_to :contact, inverse_of: :contact_training_topics, foreign_key: :party_id, optional: true |
#customer ⇒ Object
Alias for Contact#customer
54 |
# File 'app/models/contact_training_topic.rb', line 54 delegate :customer, to: :contact |
#possible_events ⇒ Object
153 154 155 |
# File 'app/models/contact_training_topic.rb', line 153 def possible_events state_transitions.map(&:event).sort end |
#possible_events_for_select ⇒ Object
157 158 159 |
# File 'app/models/contact_training_topic.rb', line 157 def possible_events_for_select possible_events.map { |evt| [evt.to_s.titleize, evt] } end |
#title ⇒ Object
Alias for Topic#title
53 |
# File 'app/models/contact_training_topic.rb', line 53 delegate :uploads, :topic_thumbnail, :title, to: :topic |
#to_s ⇒ Object
161 162 163 |
# File 'app/models/contact_training_topic.rb', line 161 def to_s "#{topic.title} for #{contact.full_name}" end |
#topic ⇒ Topic
Validations:
44 |
# File 'app/models/contact_training_topic.rb', line 44 belongs_to :topic, inverse_of: :contact_training_topics, optional: true |
#topic_thumbnail ⇒ Object
Alias for Topic#topic_thumbnail
53 |
# File 'app/models/contact_training_topic.rb', line 53 delegate :uploads, :topic_thumbnail, :title, to: :topic |
#trainer_party ⇒ Employee
45 |
# File 'app/models/contact_training_topic.rb', line 45 belongs_to :trainer_party, class_name: 'Employee', optional: true |
#uploads ⇒ Object
Alias for Topic#uploads
53 |
# File 'app/models/contact_training_topic.rb', line 53 delegate :uploads, :topic_thumbnail, :title, to: :topic |