retrospring/app/models/question.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
1.1 KiB
Ruby
Raw Normal View History

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