Class: Topic
Overview
== Schema Information
Table name: topics
Database name: primary
id :integer not null, primary key
applies_to_company :boolean default(FALSE)
applies_to_contact :boolean default(FALSE)
applies_to_employee :boolean default(FALSE)
contact_roles :string(255) is an Array
description :text
email_snippet :text
format :string
integer :integer is an Array
multiple_responses_allowed :boolean
position :integer
slug :string
state :string(255) not null
string :string(255) is an Array
title :string(255) not null
training_module :boolean default(FALSE), not null
created_at :datetime not null
updated_at :datetime not null
creator_id :integer
customer_filter_id :integer
survey_id :integer
topic_category_id :integer
topic_exam_id :integer
updater_id :integer
Indexes
idx_state_survey_id (state,survey_id)
idx_state_topic_category_id (state,topic_category_id)
index_topics_on_topic_exam_id (topic_exam_id)
topics_customer_filter_id_idx (customer_filter_id)
topics_topic_category_id_idx (topic_category_id)
Foreign Keys
fk_rails_... (topic_exam_id => topic_exams.id)
topics_customer_filter_id_fk (customer_filter_id => customer_filters.id)
topics_topic_category_id_fk (topic_category_id => topic_categories.id)
Constant Summary
Models::Auditable::ALWAYS_IGNORED
Constants included
from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
#creator, #updater
Has and belongs to many
collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
ransackable_associations, ransackable_attributes, ransortable_attributes, #to_relation
config
#after_commit
#publish_event
Instance Attribute Details
#title ⇒ Object
75
|
# File 'app/models/topic.rb', line 75
validates :title, presence: true, uniqueness: { scope: %i[topic_category_id survey_id] }, length: { maximum: 200 }
|
Class Method Details
.active ⇒ ActiveRecord::Relation<Topic>
A relation of Topics that are active. Active Record Scope
105
|
# File 'app/models/topic.rb', line 105
scope :active, -> { where(state: 'active') }
|
.assigned_to ⇒ ActiveRecord::Relation<Topic>
A relation of Topics that are assigned to. Active Record Scope
129
|
# File 'app/models/topic.rb', line 129
scope :assigned_to, ->(party) { joins(:party_topics).where(party_topics: { party_id: party.id }) }
|
.for_audience ⇒ ActiveRecord::Relation<Topic>
A relation of Topics that are for audience. Active Record Scope
120
121
122
123
124
125
126
127
|
# File 'app/models/topic.rb', line 120
scope :for_audience, ->(*audiences) {
hsh_cnd = {
'employees' => "topics.applies_to_employee = true",
'contacts' => "topics.applies_to_contact = true",
'companies' => "topics.applies_to_company = true"
}.with_indifferent_access.slice(*audiences)
where(hsh_cnd.values.join(' OR '))
}
|
A relation of Topics that are for contact role. Active Record Scope
112
|
# File 'app/models/topic.rb', line 112
scope :for_contact_role, ->(*roles) { where(applies_to_contact: true).where("? = ANY (contact_roles)", roles) }
|
.for_customer(customer) ⇒ Object
142
143
144
145
146
147
148
|
# File 'app/models/topic.rb', line 142
def self.for_customer(customer)
customer_filter_ids = CustomerFilter::QueryBuilder.filter_ids_for_customer(customer) + [nil]
Topic.active.sorted
.where.any_of({ applies_to_company: true }, { applies_to_contact: true })
.where(customer_filter_id: customer_filter_ids)
.includes(:topic_category)
end
|
.for_customer_grouped(customer) ⇒ Object
138
139
140
|
# File 'app/models/topic.rb', line 138
def self.for_customer_grouped(customer)
for_customer(customer).group_by(&:topic_category)
end
|
.for_employee ⇒ ActiveRecord::Relation<Topic>
A relation of Topics that are for employee. Active Record Scope
114
115
116
117
118
|
# File 'app/models/topic.rb', line 114
scope :for_employee, ->(employee) {
eager_load(:employee_topics)
.where("party_topics.party_id = ? or party_topics.id is null", employee.id)
.for_employee_role_ids(employee.role_ids)
}
|
.for_employee_grouped_by_category(employee) ⇒ Object
150
151
152
|
# File 'app/models/topic.rb', line 150
def self.for_employee_grouped_by_category(employee)
for_employee(employee).sorted.group_by(&:topic_category)
end
|
.for_employee_role_ids ⇒ ActiveRecord::Relation<Topic>
A relation of Topics that are for employee role ids. Active Record Scope
107
108
109
110
|
# File 'app/models/topic.rb', line 107
scope :for_employee_role_ids, ->(*role_ids) {
where(applies_to_employee: true)
.where("NOT EXISTS(select 1 from roles_topics rt where rt.topic_id = topics.id) OR EXISTS(select 1 from roles_topics rt where rt.topic_id = topics.id AND rt.role_id IN (?))", [role_ids].flatten)
}
|
158
159
160
|
# File 'app/models/topic.rb', line 158
def self.format_select_options
[['Normal', 'normal'], ['Numeric horizontal', 'numeric_horizontal']]
end
|
.ransackable_scopes(_auth_object = nil) ⇒ Object
134
135
136
|
# File 'app/models/topic.rb', line 134
def self.ransackable_scopes(_auth_object = nil)
%i[for_audience for_contact_role for_employee_role_ids keywords]
end
|
.select_options_with_categories ⇒ Object
154
155
156
|
# File 'app/models/topic.rb', line 154
def self.select_options_with_categories
Topic.joins(:topic_category).active.where(applies_to_employee: true).order(TopicCategory[:name], Topic[:title]).map { |t| ["#{t.topic_category.name} > #{t.title}", t.id] }
end
|
.sorted ⇒ ActiveRecord::Relation<Topic>
A relation of Topics that are sorted. Active Record Scope
101
102
103
104
|
# File 'app/models/topic.rb', line 101
scope :sorted, -> {
joins(:topic_category)
.order('topic_categories.position, topics.position, topics.title')
}
|
Instance Method Details
#applies_to_customer?(customer) ⇒ Boolean
166
167
168
169
170
171
172
|
# File 'app/models/topic.rb', line 166
def applies_to_customer?(customer)
if customer_filter
customer_filter.applies_to_customer?(customer)
else
true
end
end
|
#applies_to_party?(party) ⇒ Boolean
228
229
230
231
|
# File 'app/models/topic.rb', line 228
def applies_to_party?(party)
(applies_to_company? and party.is_a? Customer) or
(applies_to_contact? and party.is_a? Contact)
end
|
#articles ⇒ ActiveRecord::Relation<Article>
65
|
# File 'app/models/topic.rb', line 65
has_and_belongs_to_many :articles
|
63
|
# File 'app/models/topic.rb', line 63
has_many :contact_training_topics, inverse_of: :topic
|
55
|
# File 'app/models/topic.rb', line 55
belongs_to :customer_filter, inverse_of: :topics, optional: true
|
#customer_topic? ⇒ Boolean
199
200
201
|
# File 'app/models/topic.rb', line 199
def customer_topic?
applies_to_company || applies_to_contact
end
|
#customer_topics ⇒ ActiveRecord::Relation<CustomerTopic>
62
|
# File 'app/models/topic.rb', line 62
has_many :customer_topics, inverse_of: :topic
|
#deep_dup ⇒ Object
92
93
94
95
96
97
98
99
|
# File 'app/models/topic.rb', line 92
def deep_dup
deep_clone(
include: [:topic_responses],
except: %i[created_at updated_at]
) do |_original, copy|
copy.state = 'draft' if copy.is_a?(Topic)
end
end
|
#employee_topic? ⇒ Boolean
203
204
205
|
# File 'app/models/topic.rb', line 203
def employee_topic?
applies_to_employee
end
|
#employee_topics ⇒ ActiveRecord::Relation<EmployeeTopic>
61
|
# File 'app/models/topic.rb', line 61
has_many :employee_topics, inverse_of: :topic
|
#legacy_uploads ⇒ ActiveRecord::Relation<Upload>
66
|
# File 'app/models/topic.rb', line 66
has_and_belongs_to_many :legacy_uploads, class_name: 'Upload'
|
#multiple_responses? ⇒ Boolean
237
238
239
|
# File 'app/models/topic.rb', line 237
def multiple_responses?
topic_responses.where(TopicResponse[:score].gt(0))&.size&.> 1
end
|
#next ⇒ Object
233
234
235
|
# File 'app/models/topic.rb', line 233
def next
topic_category.topics.order(:position).where(position: position + 1).first
end
|
#party_topics ⇒ ActiveRecord::Relation<PartyTopic>
60
|
# File 'app/models/topic.rb', line 60
has_many :party_topics, inverse_of: :topic
|
#possible_events ⇒ Object
174
175
176
|
# File 'app/models/topic.rb', line 174
def possible_events
state_transitions.map(&:event).sort
end
|
#possible_events_for_select ⇒ Object
178
179
180
|
# File 'app/models/topic.rb', line 178
def possible_events_for_select
possible_events.map { |evt| [evt.to_s.titleize, evt] }
end
|
#responses_list ⇒ Object
207
208
209
|
# File 'app/models/topic.rb', line 207
def responses_list
topic_responses.map(&:response).join(',') if topic_responses.present?
end
|
#responses_list=(new_tags) ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'app/models/topic.rb', line 211
def responses_list=(new_tags)
responses = process_tag_list(new_tags)
valid_responses = []
responses.each_with_index do |r, index|
robj = topic_responses.where(TopicResponse[:response].matches(r)).first
robj ||= topic_responses.build(response: r, active: true, position: index)
valid_responses << robj
end
(topic_responses.to_a - valid_responses).each(&:mark_for_destruction)
end
|
#roles ⇒ ActiveRecord::Relation<Role>
67
|
# File 'app/models/topic.rb', line 67
has_and_belongs_to_many :roles
|
58
|
# File 'app/models/topic.rb', line 58
belongs_to :survey, inverse_of: :topics, optional: true
|
186
187
188
189
190
191
192
193
|
# File 'app/models/topic.rb', line 186
def tags
t = []
t << 'company-profiling' if applies_to_company
t << 'contact-profiling' if applies_to_contact
t << 'employee-training' if applies_to_employee
t << 'contact-training' if training_module
t
end
|
#to_s ⇒ Object
182
183
184
|
# File 'app/models/topic.rb', line 182
def to_s
title
end
|
56
|
# File 'app/models/topic.rb', line 56
belongs_to :topic_category, inverse_of: :topics, optional: true
|
#topic_category_name ⇒ Object
195
196
197
|
# File 'app/models/topic.rb', line 195
def topic_category_name
topic_category.try(:name)
end
|
57
|
# File 'app/models/topic.rb', line 57
belongs_to :topic_exam, inverse_of: :topics, optional: true
|
#topic_responses ⇒ ActiveRecord::Relation<TopicResponse>
59
|
# File 'app/models/topic.rb', line 59
has_many :topic_responses, -> { order(:position) }, autosave: true, inverse_of: :topic
|
#topic_thumbnail ⇒ Object
162
163
164
|
# File 'app/models/topic.rb', line 162
def topic_thumbnail
uploads.first.try(:thumbnail_url)
end
|
#uploads ⇒ ActiveRecord::Relation<Upload>
69
|
# File 'app/models/topic.rb', line 69
has_many :uploads, through: :articles
|
#valid_for_customer?(customer) ⇒ Boolean
224
225
226
|
# File 'app/models/topic.rb', line 224
def valid_for_customer?(customer)
customer_filter.applies_to_customer? customer
end
|