Commit d89130b0974782824fb11d4e0cc0fd9b12fe7cab
1 parent
cce80d04
Exists in
spb-stable
and in
3 other branches
Collect all event creation logic in one place called EventCreateService
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
2 changed files
with
159 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,56 @@ | @@ -0,0 +1,56 @@ | ||
1 | +# EventCreateService class | ||
2 | +# | ||
3 | +# Used for creating events feed on dashboard after certain user action | ||
4 | +# | ||
5 | +# Ex. | ||
6 | +# EventCreateService.new.new_issue(issue, current_user) | ||
7 | +# | ||
8 | +class EventCreateService | ||
9 | + def open_issue(issue, current_user) | ||
10 | + create_event(issue, current_user, Event::CREATED) | ||
11 | + end | ||
12 | + | ||
13 | + def close_issue(issue, current_user) | ||
14 | + create_event(issue, current_user, Event::CLOSED) | ||
15 | + end | ||
16 | + | ||
17 | + def reopen_issue(issue, current_user) | ||
18 | + create_event(issue, current_user, Event::REOPENED) | ||
19 | + end | ||
20 | + | ||
21 | + def open_mr(merge_request, current_user) | ||
22 | + create_event(merge_request, current_user, Event::CREATED) | ||
23 | + end | ||
24 | + | ||
25 | + def close_mr(merge_request, current_user) | ||
26 | + create_event(merge_request, current_user, Event::CLOSED) | ||
27 | + end | ||
28 | + | ||
29 | + def reopen_mr(merge_request, current_user) | ||
30 | + create_event(merge_request, current_user, Event::REOPENED) | ||
31 | + end | ||
32 | + | ||
33 | + def merge_mr(merge_request, current_user) | ||
34 | + create_event(merge_request, current_user, Event::MERGED) | ||
35 | + end | ||
36 | + | ||
37 | + def open_milestone(milestone, current_user) | ||
38 | + create_event(milestone, current_user, Event::CREATED) | ||
39 | + end | ||
40 | + | ||
41 | + def close_milestone(milestone, current_user) | ||
42 | + create_event(milestone, current_user, Event::CLOSED) | ||
43 | + end | ||
44 | + | ||
45 | + private | ||
46 | + | ||
47 | + def create_event(record, current_user, status) | ||
48 | + Event.create( | ||
49 | + project: record.project, | ||
50 | + target_id: record.id, | ||
51 | + target_type: record.class.name, | ||
52 | + action: status, | ||
53 | + author_id: current_user.id | ||
54 | + ) | ||
55 | + end | ||
56 | +end |
@@ -0,0 +1,103 @@ | @@ -0,0 +1,103 @@ | ||
1 | +require 'spec_helper' | ||
2 | + | ||
3 | +describe EventCreateService do | ||
4 | + let(:service) { EventCreateService.new } | ||
5 | + | ||
6 | + describe 'Issues' do | ||
7 | + describe :open_issue do | ||
8 | + let(:issue) { create(:issue) } | ||
9 | + | ||
10 | + it { service.open_issue(issue, issue.author).should be_true } | ||
11 | + | ||
12 | + it "should create new event" do | ||
13 | + expect { service.open_issue(issue, issue.author) }.to change { Event.count } | ||
14 | + end | ||
15 | + end | ||
16 | + | ||
17 | + describe :close_issue do | ||
18 | + let(:issue) { create(:issue) } | ||
19 | + | ||
20 | + it { service.close_issue(issue, issue.author).should be_true } | ||
21 | + | ||
22 | + it "should create new event" do | ||
23 | + expect { service.close_issue(issue, issue.author) }.to change { Event.count } | ||
24 | + end | ||
25 | + end | ||
26 | + | ||
27 | + describe :reopen_issue do | ||
28 | + let(:issue) { create(:issue) } | ||
29 | + | ||
30 | + it { service.reopen_issue(issue, issue.author).should be_true } | ||
31 | + | ||
32 | + it "should create new event" do | ||
33 | + expect { service.reopen_issue(issue, issue.author) }.to change { Event.count } | ||
34 | + end | ||
35 | + end | ||
36 | + end | ||
37 | + | ||
38 | + describe 'Merge Requests' do | ||
39 | + describe :open_mr do | ||
40 | + let(:merge_request) { create(:merge_request) } | ||
41 | + | ||
42 | + it { service.open_mr(merge_request, merge_request.author).should be_true } | ||
43 | + | ||
44 | + it "should create new event" do | ||
45 | + expect { service.open_mr(merge_request, merge_request.author) }.to change { Event.count } | ||
46 | + end | ||
47 | + end | ||
48 | + | ||
49 | + describe :close_mr do | ||
50 | + let(:merge_request) { create(:merge_request) } | ||
51 | + | ||
52 | + it { service.close_mr(merge_request, merge_request.author).should be_true } | ||
53 | + | ||
54 | + it "should create new event" do | ||
55 | + expect { service.close_mr(merge_request, merge_request.author) }.to change { Event.count } | ||
56 | + end | ||
57 | + end | ||
58 | + | ||
59 | + describe :merge_mr do | ||
60 | + let(:merge_request) { create(:merge_request) } | ||
61 | + | ||
62 | + it { service.merge_mr(merge_request, merge_request.author).should be_true } | ||
63 | + | ||
64 | + it "should create new event" do | ||
65 | + expect { service.merge_mr(merge_request, merge_request.author) }.to change { Event.count } | ||
66 | + end | ||
67 | + end | ||
68 | + | ||
69 | + describe :reopen_mr do | ||
70 | + let(:merge_request) { create(:merge_request) } | ||
71 | + | ||
72 | + it { service.reopen_mr(merge_request, merge_request.author).should be_true } | ||
73 | + | ||
74 | + it "should create new event" do | ||
75 | + expect { service.reopen_mr(merge_request, merge_request.author) }.to change { Event.count } | ||
76 | + end | ||
77 | + end | ||
78 | + end | ||
79 | + | ||
80 | + describe 'Milestone' do | ||
81 | + let(:user) { create :user } | ||
82 | + | ||
83 | + describe :open_milestone do | ||
84 | + let(:milestone) { create(:milestone) } | ||
85 | + | ||
86 | + it { service.open_milestone(milestone, user).should be_true } | ||
87 | + | ||
88 | + it "should create new event" do | ||
89 | + expect { service.open_milestone(milestone, user) }.to change { Event.count } | ||
90 | + end | ||
91 | + end | ||
92 | + | ||
93 | + describe :close_mr do | ||
94 | + let(:milestone) { create(:milestone) } | ||
95 | + | ||
96 | + it { service.close_milestone(milestone, user).should be_true } | ||
97 | + | ||
98 | + it "should create new event" do | ||
99 | + expect { service.close_milestone(milestone, user) }.to change { Event.count } | ||
100 | + end | ||
101 | + end | ||
102 | + end | ||
103 | +end |