Class: Report

Inherits:
ApplicationRecord show all
Defined in:
app/models/report.rb

Instance Method Summary collapse

Instance Method Details

#destroy_reported_itemObject


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/report.rb', line 36

def destroy_reported_item
  case item
  when Post
    if item.author.local?
      item.author.owner.retract(item)
    else
      item.destroy
    end
  when Comment
    if item.author.local?
      item.author.owner.retract(item)
    elsif item.parent.author.local?
      item.parent.author.owner.retract(item)
    else
      item.destroy
    end
  end
  mark_as_reviewed
end

#entry_does_not_existObject


24
25
26
27
28
# File 'app/models/report.rb', line 24

def entry_does_not_exist
  if Report.where(item_id: item_id, item_type: item_type).exists?(user_id: user_id)
    errors[:base] << 'You cannot report the same post twice.'
  end
end

#mark_as_reviewedObject


56
57
58
# File 'app/models/report.rb', line 56

def mark_as_reviewed
  Report.where(item_id: item_id, item_type: item_type).update_all(reviewed: true)
end

#post_or_comment_does_existObject


30
31
32
33
34
# File 'app/models/report.rb', line 30

def post_or_comment_does_exist
  if Post.find_by_id(item_id).nil? && Comment.find_by_id(item_id).nil?
    errors[:base] << 'Post or comment was already deleted or doesn\'t exists.'
  end
end

#reported_authorObject


20
21
22
# File 'app/models/report.rb', line 20

def reported_author
  item.author if item
end

#send_report_notificationObject


60
61
62
# File 'app/models/report.rb', line 60

def send_report_notification
  Workers::Mail::ReportWorker.perform_async(id)
end