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

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

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#responseString (readonly)

Returns:

  • (String)


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

validates :response, presence: true

Class Method Details

.activeActiveRecord::Relation<TopicResponse>

A relation of TopicResponses that are active. Active Record Scope

Returns:

See Also:



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

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

.format_for_select(relation = nil) ⇒ Array<Array<String, Integer>>

Parameters:

  • relation (ActiveRecord::Relation, nil) (defaults to: nil)

Returns:

  • (Array<Array<String, Integer>>)


81
82
83
84
85
86
87
# File 'app/models/topic_response.rb', line 81

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_selectArray<Array<String, Integer>>

Returns:

  • (Array<Array<String, Integer>>)


75
76
77
# File 'app/models/topic_response.rb', line 75

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:



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

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:



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

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

.used_responses_by_contact_for_selectArray<Array<String, Integer>>

Returns:

  • (Array<Array<String, Integer>>)


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

def self.used_responses_by_contact_for_select
  format_for_select used_responses_by_contact
end

.used_responses_for_selectArray<Array<String, Integer>>

Returns:

  • (Array<Array<String, Integer>>)


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

def self.used_responses_for_select
  format_for_select used_responses
end

Instance Method Details

#customer_topicsActiveRecord::Relation<CustomerTopic>

Returns:



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

has_and_belongs_to_many :customer_topics, inverse_of: :topic_responses

#employeeEmployee?

Returns:



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

belongs_to :employee, optional: true

#topicTopic

Returns:



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

belongs_to :topic