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
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
#publish_event
Instance Attribute Details
#title ⇒ Object
77
|
# File 'app/models/topic.rb', line 77
validates :title, presence: true, uniqueness: { scope: [: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
143
144
145
146
147
148
149
|
# File 'app/models/topic.rb', line 143
def self.for_customer(customer)
customer_filter_ids = CustomerFilter::QueryBuilder.filter_ids_for_customer(customer) + [nil]
Topic.active.sorted.
where("applies_to_company IS TRUE or applies_to_contact IS TRUE").
where(customer_filter_id: customer_filter_ids).
includes(:topic_category)
end
|
.for_customer_grouped(customer) ⇒ Object
139
140
141
|
# File 'app/models/topic.rb', line 139
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
151
152
153
|
# File 'app/models/topic.rb', line 151
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)
}
|
159
160
161
|
# File 'app/models/topic.rb', line 159
def self.format_select_options
[['Normal', 'normal'], ['Numeric horizontal', 'numeric_horizontal']]
end
|
.ransackable_scopes(auth_object = nil) ⇒ Object
135
136
137
|
# File 'app/models/topic.rb', line 135
def self.ransackable_scopes(auth_object = nil)
[:for_audience, :for_contact_role, :for_employee_role_ids, :keywords]
end
|
.select_options_with_categories ⇒ Object
155
156
157
|
# File 'app/models/topic.rb', line 155
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
103
104
|
# File 'app/models/topic.rb', line 103
scope :sorted, -> { joins(:topic_category).
order('topic_categories.position, topics.position, topics.title') }
|
Instance Method Details
#applies_to_customer?(customer) ⇒ Boolean
167
168
169
170
171
172
173
|
# File 'app/models/topic.rb', line 167
def applies_to_customer?(customer)
if customer_filter
return customer_filter.applies_to_customer?(customer)
else
return true
end
end
|
#applies_to_party?(party) ⇒ Boolean
229
230
231
232
|
# File 'app/models/topic.rb', line 229
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>
66
|
# File 'app/models/topic.rb', line 66
has_and_belongs_to_many :articles
|
64
|
# File 'app/models/topic.rb', line 64
has_many :contact_training_topics, inverse_of: :topic
|
56
|
# File 'app/models/topic.rb', line 56
belongs_to :customer_filter, inverse_of: :topics, optional: true
|
#customer_topic? ⇒ Boolean
200
201
202
|
# File 'app/models/topic.rb', line 200
def customer_topic?
(applies_to_company || applies_to_contact)
end
|
#customer_topics ⇒ ActiveRecord::Relation<CustomerTopic>
63
|
# File 'app/models/topic.rb', line 63
has_many :customer_topics, inverse_of: :topic
|
#deep_dup ⇒ Object
94
95
96
97
98
99
100
101
|
# File 'app/models/topic.rb', line 94
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
204
205
206
|
# File 'app/models/topic.rb', line 204
def employee_topic?
applies_to_employee
end
|
#employee_topics ⇒ ActiveRecord::Relation<EmployeeTopic>
62
|
# File 'app/models/topic.rb', line 62
has_many :employee_topics, inverse_of: :topic
|
#legacy_uploads ⇒ ActiveRecord::Relation<Upload>
67
|
# File 'app/models/topic.rb', line 67
has_and_belongs_to_many :legacy_uploads, class_name: 'Upload'
|
#multiple_responses? ⇒ Boolean
238
239
240
|
# File 'app/models/topic.rb', line 238
def multiple_responses?
topic_responses.where('score > ?', 0)&.size > 1
end
|
#next ⇒ Object
234
235
236
|
# File 'app/models/topic.rb', line 234
def next
topic_category.topics.order(:position).where(position: position + 1).first
end
|
55
|
# File 'app/models/topic.rb', line 55
belongs_to :party, optional: true
|
#party_topics ⇒ ActiveRecord::Relation<PartyTopic>
61
|
# File 'app/models/topic.rb', line 61
has_many :party_topics, inverse_of: :topic
|
#possible_events ⇒ Object
175
176
177
|
# File 'app/models/topic.rb', line 175
def possible_events
state_transitions.map(&:event).sort
end
|
#possible_events_for_select ⇒ Object
179
180
181
|
# File 'app/models/topic.rb', line 179
def possible_events_for_select
possible_events.map { |evt| [evt.to_s.titleize, evt] }
end
|
#responses_list ⇒ Object
208
209
210
|
# File 'app/models/topic.rb', line 208
def responses_list
topic_responses.map(&:response).join(',') if topic_responses.present?
end
|
#responses_list=(new_tags) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'app/models/topic.rb', line 212
def responses_list=(new_tags)
responses = process_tag_list(new_tags)
valid_responses = []
responses.each_with_index do |r,index|
robj = topic_responses.where('topic_responses.response ILIKE ?', 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>
68
|
# File 'app/models/topic.rb', line 68
has_and_belongs_to_many :roles
|
59
|
# File 'app/models/topic.rb', line 59
belongs_to :survey, inverse_of: :topics, optional: true
|
187
188
189
190
191
192
193
194
|
# File 'app/models/topic.rb', line 187
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
183
184
185
|
# File 'app/models/topic.rb', line 183
def to_s
title
end
|
57
|
# File 'app/models/topic.rb', line 57
belongs_to :topic_category, inverse_of: :topics, optional: true
|
#topic_category_name ⇒ Object
196
197
198
|
# File 'app/models/topic.rb', line 196
def topic_category_name
topic_category.try(:name)
end
|
58
|
# File 'app/models/topic.rb', line 58
belongs_to :topic_exam, inverse_of: :topics, optional: true
|
#topic_responses ⇒ ActiveRecord::Relation<TopicResponse>
60
|
# File 'app/models/topic.rb', line 60
has_many :topic_responses, -> { order(:position) }, autosave: true, inverse_of: :topic
|
#topic_thumbnail ⇒ Object
163
164
165
|
# File 'app/models/topic.rb', line 163
def topic_thumbnail
uploads.first.try(:thumbnail_url)
end
|
#uploads ⇒ ActiveRecord::Relation<Upload>
70
|
# File 'app/models/topic.rb', line 70
has_many :uploads, through: :articles
|
#valid_for_customer?(customer) ⇒ Boolean
225
226
227
|
# File 'app/models/topic.rb', line 225
def valid_for_customer?(customer)
customer_filter.applies_to_customer?customer
end
|