problem_destroy.rb
876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class ProblemDestroy
attr_reader :problem
def initialize(problem)
@problem = problem
end
def execute
delete_errs
delete_comments
problem.delete
end
##
# Destroy all problem pass in args
#
# @params [ Array[Problem] ] problems the list of problem need to be delete
# can be a single Problem
# @return [ Integer ]
# the number of problem destroy
#
def self.execute(problems)
Array(problems).each{ |problem|
ProblemDestroy.new(problem).execute
}.count
end
private
def errs_id
problem.errs.only(:id).map(&:id)
end
def comments_id
problem.comments.only(:id).map(&:id)
end
def delete_errs
Notice.delete_all(:err_id => { '$in' => errs_id })
Err.delete_all(:_id => { '$in' => errs_id })
end
def delete_comments
Comment.delete_all(:_id => { '$in' => comments_id })
end
end