Class: TopicCategory

Inherits:
ApplicationRecord show all
Extended by:
FriendlyId
Includes:
Models::Auditable
Defined in:
app/models/topic_category.rb

Overview

== Schema Information

Table name: topic_categories
Database name: primary

id :integer not null, primary key
active :boolean default(TRUE), not null
applies_to_customers :boolean default(FALSE)
applies_to_employees :boolean default(FALSE)
description :text
duration :string
name :string(255) not null
position :integer
slug :string
sub_category_name :string
training_module :boolean default(FALSE), not null
course_id :integer

Indexes

index_topic_categories_on_course_id (course_id)

Foreign Keys

fk_rails_... (course_id => courses.id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

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

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#nameObject (readonly)



43
# File 'app/models/topic_category.rb', line 43

validates :name, presence: true

Class Method Details

.activeActiveRecord::Relation<TopicCategory>

A relation of TopicCategories that are active. Active Record Scope

Returns:

See Also:



45
# File 'app/models/topic_category.rb', line 45

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

.categories_for_selectObject



52
53
54
# File 'app/models/topic_category.rb', line 52

def self.categories_for_select
  select_options.map(&:first)
end

.for_customer(customer) ⇒ Object



64
65
66
67
68
# File 'app/models/topic_category.rb', line 64

def self.for_customer(customer)
  customer_filter_ids = CustomerFilter::QueryBuilder.filter_ids_for_customer(customer) + [nil]
  TopicCategory.sorted.active
               .where("exists(select 1 from topics where topic_category_id = topic_categories.id and state = 'active' and customer_filter_id IN (?))", customer_filter_ids)
end

.select_optionsObject



48
49
50
# File 'app/models/topic_category.rb', line 48

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

.sortedActiveRecord::Relation<TopicCategory>

A relation of TopicCategories that are sorted. Active Record Scope

Returns:

See Also:



46
# File 'app/models/topic_category.rb', line 46

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

.training_modulesObject



56
57
58
# File 'app/models/topic_category.rb', line 56

def self.training_modules
  active.order(:name).where(training_module: true).distinct.pluck(:name, :slug)
end

Instance Method Details

#contentsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/topic_category.rb', line 74

def contents
  # Aggreate all topics, and articles into a stream
  result = []
  topics.sorted.each do |topic|
    result << { title: topic.title, id: topic.id, content: topic.description, object_class: :topic }
    topic.articles.each do |article|
      # If article only has a solution, use this
      result << { title: article.subject, id: "#{topic.id}-#{article.id}-0", content: article.solution, object_class: :article, object_id: article.id } if article.solution.present?
      article.article_pages.sorted.each do |article_page|
        result << { id: "#{topic.id}-#{article.id}-#{article_page.position}", content: article_page.content, object_class: :article_page, object_id: article_page.id }
      end
    end
  end
  result.map { |r| OpenStruct.new(r).freeze }
end

#courseCourse

Returns:

See Also:



36
# File 'app/models/topic_category.rb', line 36

belongs_to :course, optional: true

#course_select_optionsObject



70
71
72
# File 'app/models/topic_category.rb', line 70

def course_select_options
  Course.select_options(course_id)
end

#customer_topicsActiveRecord::Relation<CustomerTopic>

Returns:

See Also:



39
# File 'app/models/topic_category.rb', line 39

has_many :customer_topics, -> { order(:position) }

#names_for_selectObject



60
61
62
# File 'app/models/topic_category.rb', line 60

def names_for_select
  (TopicCategory.distinct.pluck(:name) + [name.presence]).compact.uniq.sort
end

#nextObject



110
111
112
# File 'app/models/topic_category.rb', line 110

def next
  course.topic_categories.order(:position).where(position: position + 1).first
end

#percentage_progressObject



114
115
116
# File 'app/models/topic_category.rb', line 114

def percentage_progress
  "#{(course.party_topics.completed.size * 100) / course.party_topics.size}%"
end

#stateObject



90
91
92
# File 'app/models/topic_category.rb', line 90

def state
  active ? 'active' : 'inactive'
end

#tagsObject



94
95
96
97
98
99
100
# File 'app/models/topic_category.rb', line 94

def tags
  t = []
  t << 'customer-nurturing' if applies_to_customers
  t << 'employee-training' if applies_to_employees
  t << 'contact-training' if training_module
  t
end

#to_sObject



102
103
104
105
106
107
108
# File 'app/models/topic_category.rb', line 102

def to_s
  if sub_category_name.present?
    "#{name} > #{sub_category_name}"
  else
    name
  end
end

#topicsActiveRecord::Relation<Topic>

Returns:

  • (ActiveRecord::Relation<Topic>)

See Also:



38
# File 'app/models/topic_category.rb', line 38

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