Class: Report

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/report.rb

Instance Method Summary collapse

Instance Method Details

#delete_commentObject


48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/report.rb', line 48

def delete_comment
  if comment = Comment.where(id: item_id).first
    if comment.author.local?
      comment.author.owner.retract(comment)
    elsif comment.parent.author.local?
      comment.parent.author.owner.retract(comment)
    else
      comment.destroy
    end
  end
end

#delete_postObject


38
39
40
41
42
43
44
45
46
# File 'app/models/report.rb', line 38

def delete_post
  if post = Post.where(id: item_id).first
    if post.author.local?
      post.author.owner.retract(post)
    else
      post.destroy
    end
  end
end

#destroy_reported_itemObject


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

def destroy_reported_item
  if item_type == 'post'
    delete_post
  elsif item_type == 'comment'
    delete_comment
  end
  mark_as_reviewed
end

#entry_does_not_existObject


17
18
19
20
21
# File 'app/models/report.rb', line 17

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


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

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

#post_or_comment_does_existObject


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

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

#send_report_notificationObject


64
65
66
# File 'app/models/report.rb', line 64

def send_report_notification
  Workers::Mail::ReportWorker.perform_async(self.item_type, self.item_id)
end