回答編集履歴
4
if と unless 逆に書いてた。
test
CHANGED
@@ -30,7 +30,9 @@
|
|
30
30
|
|
31
31
|
wf = Workflow.where number: number
|
32
32
|
|
33
|
-
return
|
33
|
+
return if wf.all? { |w| Status.find_by(compensation: w.compensation progress: progress).present? }
|
34
|
+
|
35
|
+
|
34
36
|
|
35
37
|
errors[:base] << 'エラーメッセージ'
|
36
38
|
|
3
修正
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
included do
|
10
10
|
|
11
|
-
after_save -> { update_progress('payment'), if: proc { self.number.present? }
|
11
|
+
after_save -> { update_progress('payment') }, if: proc { self.number.present? }
|
12
12
|
|
13
13
|
validate :check_combination_status, if: proc { self.number.present? }
|
14
14
|
|
2
修正
test
CHANGED
@@ -1,37 +1,57 @@
|
|
1
|
-
僕も雰囲気で書いたんですが、こんな感じですかね?
|
2
|
-
|
3
1
|
```rb
|
4
2
|
|
5
|
-
de
|
3
|
+
module ProgressUpdateble
|
6
4
|
|
7
|
-
|
5
|
+
extend ActiveSupport::Concern
|
8
6
|
|
9
7
|
|
10
8
|
|
11
|
-
|
9
|
+
included do
|
12
10
|
|
13
|
-
|
11
|
+
after_save -> { update_progress('payment'), if: proc { self.number.present? } }
|
14
12
|
|
15
|
-
|
13
|
+
validate :check_combination_status, if: proc { self.number.present? }
|
16
|
-
|
17
|
-
wf.update_all progress: status
|
18
|
-
|
19
|
-
else
|
20
|
-
|
21
|
-
raise ActiveRecord::RecordInvalid
|
22
|
-
|
23
|
-
end
|
24
14
|
|
25
15
|
end
|
26
16
|
|
27
17
|
|
28
18
|
|
29
|
-
|
19
|
+
def update_progress(status)
|
30
20
|
|
31
|
-
lo
|
21
|
+
work_flows = Workflow.where(approval_status: 'pending', number: number)
|
32
22
|
|
23
|
+
wf.update_all progress: status
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
def check_combination_status
|
30
|
+
|
31
|
+
wf = Workflow.where number: number
|
32
|
+
|
33
|
+
return unless wf.all? { |w| Status.find_by(compensation: w.compensation progress: progress).present? }
|
34
|
+
|
33
|
-
errors[:base] << '
|
35
|
+
errors[:base] << 'エラーメッセージ'
|
36
|
+
|
37
|
+
end
|
34
38
|
|
35
39
|
end
|
36
40
|
|
37
41
|
```
|
42
|
+
|
43
|
+
`before_save`より`after_save`の方が良さそう。
|
44
|
+
|
45
|
+
うーん、なんかもっといい方法がありそうな気もする。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
`after_save`の`if:`の書き方か場所が違うかも...。
|
50
|
+
|
51
|
+
`after_save -> { ... }`の書き方がよくわからなかった、ごめんよ。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
`update_all`はコールバックをスキップするのを意識しておくといいかも。
|
56
|
+
|
57
|
+
もし修正するときに`save`の無限ループに陥らないためにも。
|
1
修正
test
CHANGED
@@ -10,11 +10,11 @@
|
|
10
10
|
|
11
11
|
Workflow.transaction do # 一応トランザクション貼ってます
|
12
12
|
|
13
|
-
wf = Workflow.where
|
13
|
+
wf = Workflow.where number: number
|
14
14
|
|
15
|
-
if wf.all? { |w| Status.
|
15
|
+
if wf.all? { |w| Status.exists? compensation: w.compensation, progress: progress }
|
16
16
|
|
17
|
-
wf.update_all
|
17
|
+
wf.update_all progress: status
|
18
18
|
|
19
19
|
else
|
20
20
|
|