Class: TopicResponse

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/topic_response.rb

Overview

== Schema Information

Table name: topic_responses
Database name: primary

id :integer not null, primary key
active :boolean default(TRUE), not null
description :text
position :integer
response :string(255)
score :integer default(0), not null
employee_id :integer
topic_id :integer not null

Indexes

idx_topic_id_active (topic_id,active)
idx_topic_id_score (topic_id,score)

Foreign Keys

topic_responses_topic_id_fk (topic_id => topics.id) ON DELETE => cascade

Constant Summary collapse

USED_RESPONSE_SQL =

Used response sql.

<<-EOS.freeze
  exists(
    select 1 from party_topics_topic_responses pttr
    where pttr.topic_response_id = topic_responses.id
  )
EOS
USED_RESPONSE_CONTACT_SQL =

Used response contact sql.

<<-EOS.freeze
  exists(
    select 1 from party_topics_topic_responses pttr
    inner join party_topics pt on pt.id = pttr.customer_topic_id
    inner join parties p on p.id = pt.party_id
    where p.type = 'Contact'
          and pttr.topic_response_id = topic_responses.id
  )
EOS

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 and belongs to many collapse

Class 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

#responseObject (readonly)



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

validates :response, presence: true

Class Method Details

.activeActiveRecord::Relation<TopicResponse>

A relation of TopicResponses that are active. Active Record Scope

Returns:

See Also:



59
# File 'app/models/topic_response.rb', line 59

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

.format_for_select(relation = nil) ⇒ Object



73
74
75
76
77
78
79
# File 'app/models/topic_response.rb', line 73

def self.format_for_select(relation = nil)
  relation ||= active
  relation.joins(topic: :topic_category)
          .select("distinct topic_categories.name as topic_category, topics.title as topic_title, topic_responses.response, topic_responses.id")
          .order("topics.title, topic_responses.response")
          .map { |tr| ["#{tr.topic_category} : #{tr.topic_title} : #{tr.response}", tr.id] }
end

.possible_responses_for_selectObject



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

def self.possible_responses_for_select
  format_for_select all
end

.used_responsesActiveRecord::Relation<TopicResponse>

A relation of TopicResponses that are used responses. Active Record Scope

Returns:

See Also:



57
# File 'app/models/topic_response.rb', line 57

scope :used_responses, -> { where(USED_RESPONSE_SQL) }

.used_responses_by_contactActiveRecord::Relation<TopicResponse>

A relation of TopicResponses that are used responses by contact. Active Record Scope

Returns:

See Also:



58
# File 'app/models/topic_response.rb', line 58

scope :used_responses_by_contact, -> { where(USED_RESPONSE_CONTACT_SQL) }

.used_responses_by_contact_for_selectObject



65
66
67
# File 'app/models/topic_response.rb', line 65

def self.used_responses_by_contact_for_select
  format_for_select used_responses_by_contact
end

.used_responses_for_selectObject



61
62
63
# File 'app/models/topic_response.rb', line 61

def self.used_responses_for_select
  format_for_select used_responses
end

Instance Method Details

#customer_topicsActiveRecord::Relation<CustomerTopic>

Returns:

See Also:



32
# File 'app/models/topic_response.rb', line 32

has_and_belongs_to_many :customer_topics, inverse_of: :topic_responses

#employeeEmployee

Returns:

See Also:



31
# File 'app/models/topic_response.rb', line 31

belongs_to :employee, optional: true

#topicTopic

Returns:

See Also:



30
# File 'app/models/topic_response.rb', line 30

belongs_to :topic