This commit is contained in:
Yuki 2015-04-30 05:34:43 +05:30
parent 5b3b3684c9
commit 563293be44
4 changed files with 40 additions and 9 deletions

View file

@ -17,10 +17,12 @@ class Answer < ActiveRecord::Base
before_destroy do
# mark a report as deleted if it exists
rep = Report.where(target_id: self.id).first
unless rep.nil?
rep.deleted = true
rep.save
rep = Report.where(target_id: self.id, type: Reports::Answer)
rep.each do |r|
unless r.nil?
r.deleted = true
r.save
end
end
self.user.decrement! :answered_count

View file

@ -14,6 +14,14 @@ class Comment < ActiveRecord::Base
end
before_destroy do
rep = Report.where(target_id: self.id, type: Reports::Comment)
rep.each do |r|
unless r.nil?
r.deleted = true
r.save
end
end
Subscription.denotify self, answer
user.decrement! :commented_count
answer.decrement! :comment_count

View file

@ -6,12 +6,14 @@ class Question < ActiveRecord::Base
validates :content, length: { maximum: 255 }
before_destroy do
rep = Report.where(target_id: self.id).first
unless rep.nil?
rep.deleted = true
rep.save
rep = Report.where(target_id: self.id, type: Reports::Question)
rep.each do |r|
unless r.nil?
r.deleted = true
r.save
end
end
user.decrement! :asked_count unless self.author_is_anonymous
end

View file

@ -59,6 +59,25 @@ class User < ActiveRecord::Base
end unless website.blank?
end
# when a user deleted himself, all reports relating to the user are invalid
before_destroy do
rep = Report.where(target_id: self.id, type: Reports::User)
rep.each do |r|
unless r.nil?
r.deleted = true
r.save
end
end
rep = Report.where(user_id: self.id)
rep.each do |r|
unless r.nil?
r.deleted = true
r.save
end
end
end
def login=(login)
@login = login
end