2024-01-27 04:31:36 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-18 15:59:18 -07:00
|
|
|
class Question < ApplicationRecord
|
2020-04-20 14:03:57 -07:00
|
|
|
include Question::AnswerMethods
|
|
|
|
|
2020-04-30 09:39:33 -07:00
|
|
|
belongs_to :user, optional: true
|
2024-01-14 04:14:04 -08:00
|
|
|
has_many :anonymous_blocks, dependent: :nullify
|
2014-12-28 12:42:21 -08:00
|
|
|
has_many :answers, dependent: :destroy
|
2024-01-27 04:03:45 -08:00
|
|
|
has_many :inbox_entries, dependent: :destroy
|
2014-12-08 06:34:37 -08:00
|
|
|
|
2023-01-08 05:03:54 -08:00
|
|
|
validates :content, length: { minimum: 1 }
|
|
|
|
|
|
|
|
SHORT_QUESTION_MAX_LENGTH = 512
|
2014-12-14 03:03:57 -08:00
|
|
|
|
2014-12-28 12:58:11 -08:00
|
|
|
before_destroy do
|
2024-01-27 04:31:36 -08:00
|
|
|
rep = Report.where(target_id: self.id, type: "Reports::Question")
|
2015-04-29 17:04:43 -07:00
|
|
|
rep.each do |r|
|
|
|
|
unless r.nil?
|
|
|
|
r.deleted = true
|
|
|
|
r.save
|
|
|
|
end
|
2015-04-28 01:46:02 -07:00
|
|
|
end
|
2015-04-29 17:04:43 -07:00
|
|
|
|
2022-02-04 13:25:08 -08:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
2022-07-24 13:25:13 -07:00
|
|
|
user&.decrement! :asked_count unless author_is_anonymous || direct
|
2022-02-04 13:25:08 -08:00
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
2014-12-28 12:58:11 -08:00
|
|
|
end
|
|
|
|
|
2014-12-14 03:19:52 -08:00
|
|
|
def can_be_removed?
|
2024-01-27 04:31:36 -08:00
|
|
|
return false if self.answers.count.positive?
|
2024-01-27 04:02:58 -08:00
|
|
|
return false if InboxEntry.where(question: self).count > 1
|
2024-01-27 04:31:36 -08:00
|
|
|
|
2014-12-14 03:19:52 -08:00
|
|
|
true
|
2014-12-14 03:03:57 -08:00
|
|
|
end
|
2022-08-13 11:01:13 -07:00
|
|
|
|
|
|
|
def generated? = %w[justask retrospring_exporter].include?(author_identifier)
|
|
|
|
|
|
|
|
def anonymous? = author_is_anonymous && author_identifier.present?
|
2023-01-08 05:03:54 -08:00
|
|
|
|
|
|
|
def long? = content.length > SHORT_QUESTION_MAX_LENGTH
|
2014-10-27 22:36:38 -07:00
|
|
|
end
|