2020-04-19 00:59:18 +02:00
|
|
|
class Question < ApplicationRecord
|
2020-04-20 23:03:57 +02:00
|
|
|
include Question::AnswerMethods
|
|
|
|
|
2020-04-30 18:39:33 +02:00
|
|
|
belongs_to :user, optional: true
|
2014-12-28 21:42:21 +01:00
|
|
|
has_many :answers, dependent: :destroy
|
2014-12-28 00:34:56 +01:00
|
|
|
has_many :inboxes, dependent: :destroy
|
2014-12-08 15:34:37 +01:00
|
|
|
|
2022-07-08 20:40:32 +02:00
|
|
|
validates :content, length: { minimum: 1, maximum: 512 }
|
2014-12-14 12:03:57 +01:00
|
|
|
|
2014-12-28 21:58:11 +01:00
|
|
|
before_destroy do
|
2015-04-30 02:22:24 +02: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
|
|
|
|
2022-02-04 22:25:08 +01:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
|
|
|
user&.decrement! :asked_count unless author_is_anonymous
|
|
|
|
# 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?
|
|
|
|
return false if self.answers.count > 0
|
|
|
|
return false if Inbox.where(question: self).count > 1
|
|
|
|
true
|
2014-12-14 12:03:57 +01:00
|
|
|
end
|
2014-10-28 06:36:38 +01:00
|
|
|
end
|