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 :enum
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
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #state ⇒ Object readonly
Belongs to collapse
-
#contact ⇒ Contact
instruction_typeenum is declared on PartyTopic — see comment there. - #topic ⇒ Topic
- #trainer_party ⇒ Employee
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, in_progress, 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 Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#state ⇒ Object (readonly)
50 |
# File 'app/models/contact_training_topic.rb', line 50 validates :state, :party, :topic, presence: true |
Class Method Details
.active ⇒ ActiveRecord::Relation<ContactTrainingTopic>
A relation of ContactTrainingTopics that are active. Active Record Scope
52 |
# File 'app/models/contact_training_topic.rb', line 52 scope :active, -> { where.not(state: 'cancelled') } |
.apply_state_order(relation) ⇒ Object
146 147 148 |
# File 'app/models/contact_training_topic.rb', line 146 def self.apply_state_order(relation) relation.group_by(&:state).slice(*state_order).values.flatten end |
.assigned_topic_select_options ⇒ Object
101 102 103 104 105 106 107 |
# File 'app/models/contact_training_topic.rb', line 101 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
109 110 111 |
# File 'app/models/contact_training_topic.rb', line 109 def self. Contact.joins(:contact_training_topics).uniq.order(:full_name).map { |e| [e.full_name, e.id] } end |
.assigned_trainer_select_options ⇒ Object
97 98 99 |
# File 'app/models/contact_training_topic.rb', line 97 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
114 115 116 117 |
# File 'app/models/contact_training_topic.rb', line 114 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
133 134 135 136 137 138 139 140 |
# File 'app/models/contact_training_topic.rb', line 133 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
120 121 122 |
# File 'app/models/contact_training_topic.rb', line 120 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
93 94 95 |
# File 'app/models/contact_training_topic.rb', line 93 def self. instruction_types.keys.map { |k| [k.humanize, k] } end |
.state_order ⇒ Object
142 143 144 |
# File 'app/models/contact_training_topic.rb', line 142 def self.state_order state_machines[:state].states.map { |s| s.name.to_s } end |
.states_for_select ⇒ Object
89 90 91 |
# File 'app/models/contact_training_topic.rb', line 89 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
125 126 127 128 129 130 |
# File 'app/models/contact_training_topic.rb', line 125 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
instruction_type enum is declared on PartyTopic — see comment there.
44 |
# File 'app/models/contact_training_topic.rb', line 44 belongs_to :contact, inverse_of: :contact_training_topics, foreign_key: :party_id, optional: true |
#customer ⇒ Object
Alias for Contact#customer
55 |
# File 'app/models/contact_training_topic.rb', line 55 delegate :customer, to: :contact |
#possible_events ⇒ Object
150 151 152 |
# File 'app/models/contact_training_topic.rb', line 150 def possible_events state_transitions.map(&:event).sort end |
#possible_events_for_select ⇒ Object
154 155 156 |
# File 'app/models/contact_training_topic.rb', line 154 def possible_events_for_select possible_events.map { |evt| [evt.to_s.titleize, evt] } end |
#title ⇒ Object
Alias for Topic#title
54 |
# File 'app/models/contact_training_topic.rb', line 54 delegate :uploads, :topic_thumbnail, :title, to: :topic |
#to_s ⇒ Object
158 159 160 |
# File 'app/models/contact_training_topic.rb', line 158 def to_s "#{topic.title} for #{contact.full_name}" end |
#topic ⇒ Topic
Validations:
45 |
# File 'app/models/contact_training_topic.rb', line 45 belongs_to :topic, inverse_of: :contact_training_topics, optional: true |
#topic_thumbnail ⇒ Object
Alias for Topic#topic_thumbnail
54 |
# File 'app/models/contact_training_topic.rb', line 54 delegate :uploads, :topic_thumbnail, :title, to: :topic |
#trainer_party ⇒ Employee
46 |
# File 'app/models/contact_training_topic.rb', line 46 belongs_to :trainer_party, class_name: 'Employee', optional: true |
#uploads ⇒ Object
Alias for Topic#uploads
54 |
# File 'app/models/contact_training_topic.rb', line 54 delegate :uploads, :topic_thumbnail, :title, to: :topic |