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

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 Models::EventPublishable

#publish_event

Instance Attribute Details

#nameObject (readonly)



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

validates :name, presence: true

Class Method Details

.activeActiveRecord::Relation<TopicCategory>

A relation of TopicCategories that are active. Active Record Scope

Returns:

See Also:



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

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

.categories_for_selectObject



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

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

.for_customer(customer) ⇒ Object



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

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



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

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:



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

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

.training_modulesObject



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

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

Instance Method Details

#contentsObject



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

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
      if article.solution.present?
        result << { title: article.subject, id: "#{topic.id}-#{article.id}-0", content: article.solution, object_class: :article, object_id: article.id }
      end
      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:



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

belongs_to :course, optional: true

#course_select_optionsObject



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

def course_select_options
  Course.select_options(course_id)
end

#customer_topicsActiveRecord::Relation<CustomerTopic>

Returns:

See Also:



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

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

#names_for_selectObject



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

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

#nextObject



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

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

#percentage_progressObject



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

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

#stateObject



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

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

#tagsObject



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

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



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

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:



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

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