Class: TopicCategory
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
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
#creator, #updater
Class Method Summary
collapse
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
#publish_event
Instance Attribute Details
#name ⇒ Object
42
|
# File 'app/models/topic_category.rb', line 42
validates :name, presence: true
|
Class Method Details
.active ⇒ ActiveRecord::Relation<TopicCategory>
A relation of TopicCategories that are active. Active Record Scope
44
|
# File 'app/models/topic_category.rb', line 44
scope :active, -> { where(active: true) }
|
.categories_for_select ⇒ Object
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_options ⇒ Object
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
|
.sorted ⇒ ActiveRecord::Relation<TopicCategory>
A relation of TopicCategories that are sorted. Active Record Scope
45
|
# File 'app/models/topic_category.rb', line 45
scope :sorted, -> { order(:position) }
|
.training_modules ⇒ Object
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
#contents ⇒ Object
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
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.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
|
35
|
# File 'app/models/topic_category.rb', line 35
belongs_to :course, optional: true
|
#course_select_options ⇒ Object
69
70
71
|
# File 'app/models/topic_category.rb', line 69
def course_select_options
Course.select_options(course_id)
end
|
#customer_topics ⇒ ActiveRecord::Relation<CustomerTopic>
38
|
# File 'app/models/topic_category.rb', line 38
has_many :customer_topics, -> { order(:position) }
|
#names_for_select ⇒ Object
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
|
#next ⇒ Object
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_progress ⇒ Object
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
|
#state ⇒ Object
91
92
93
|
# File 'app/models/topic_category.rb', line 91
def state
active ? 'active' : 'inactive'
end
|
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_s ⇒ Object
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
|
#topics ⇒ ActiveRecord::Relation<Topic>
37
|
# File 'app/models/topic_category.rb', line 37
has_many :topics, -> { order(:position) }, inverse_of: :topic_category
|