Commit 5e96ee341c0b0ed933e1e939ca3302965a44913b
1 parent
1fc42d99
Exists in
spb-stable
and in
3 other branches
Implement FilteringService
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
1 changed file
with
119 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,119 @@ | @@ -0,0 +1,119 @@ | ||
1 | +# FilteringService class | ||
2 | +# | ||
3 | +# Used to filter Issues and MergeRequests collections by set of params | ||
4 | +# | ||
5 | +# Arguments: | ||
6 | +# klass - actual class like Issue or MergeRequest | ||
7 | +# current_user - which user use | ||
8 | +# params: | ||
9 | +# scope: 'created-by-me' or 'assigned-to-me' or 'all' | ||
10 | +# state: 'open' or 'closed' or 'all' | ||
11 | +# group_id: integer | ||
12 | +# project_id: integer | ||
13 | +# milestone_id: integer | ||
14 | +# assignee_id: integer | ||
15 | +# search: string | ||
16 | +# label_name: string | ||
17 | +# sort: string | ||
18 | +# | ||
19 | +class FilteringService | ||
20 | + attr_accessor :klass, :current_user, :params | ||
21 | + | ||
22 | + def execute(klass, current_user, params) | ||
23 | + @klass = klass | ||
24 | + @current_user = current_user | ||
25 | + @params = params | ||
26 | + | ||
27 | + items = by_scope | ||
28 | + items = by_state(items) | ||
29 | + items = by_group(items) | ||
30 | + items = by_project(items) | ||
31 | + items = by_search(items) | ||
32 | + items = by_milestone(items) | ||
33 | + items = by_assignee(items) | ||
34 | + items = by_label(items) | ||
35 | + items = sort(items) | ||
36 | + end | ||
37 | + | ||
38 | + private | ||
39 | + | ||
40 | + def by_scope | ||
41 | + table_name = klass.table_name | ||
42 | + | ||
43 | + case params[:scope] | ||
44 | + when 'created-by-me', 'authored' then | ||
45 | + current_user.send(table_name) | ||
46 | + when 'all' then | ||
47 | + klass.of_projects(current_user.authorized_projects.pluck(:id)) | ||
48 | + when 'assigned-to-me' then | ||
49 | + current_user.send("assigned_#{table_name}") | ||
50 | + else | ||
51 | + raise 'You must specify default scope' | ||
52 | + end | ||
53 | + end | ||
54 | + | ||
55 | + def by_state(items) | ||
56 | + case params[:state] | ||
57 | + when 'closed' | ||
58 | + items.closed | ||
59 | + when 'all' | ||
60 | + items | ||
61 | + when 'opened' | ||
62 | + items.opened | ||
63 | + else | ||
64 | + raise 'You must specify default state' | ||
65 | + end | ||
66 | + end | ||
67 | + | ||
68 | + def by_group(items) | ||
69 | + if params[:group_id].present? | ||
70 | + items = items.of_group(Group.find(params[:group_id])) | ||
71 | + end | ||
72 | + | ||
73 | + items | ||
74 | + end | ||
75 | + | ||
76 | + def by_project(items) | ||
77 | + if params[:project_id].present? | ||
78 | + items = items.of_projects(params[:project_id]) | ||
79 | + end | ||
80 | + | ||
81 | + items | ||
82 | + end | ||
83 | + | ||
84 | + def by_search(items) | ||
85 | + if params[:search].present? | ||
86 | + items = items.search(params[:search]) | ||
87 | + end | ||
88 | + | ||
89 | + items | ||
90 | + end | ||
91 | + | ||
92 | + def sort(items) | ||
93 | + items.sort(params[:sort]) | ||
94 | + end | ||
95 | + | ||
96 | + def by_milestone(items) | ||
97 | + if params[:milestone_id].present? | ||
98 | + items = items.where(milestone_id: (params[:milestone_id] == '0' ? nil : params[:milestone_id])) | ||
99 | + end | ||
100 | + | ||
101 | + items | ||
102 | + end | ||
103 | + | ||
104 | + def by_assignee(items) | ||
105 | + if params[:assignee_id].present? | ||
106 | + items = items.where(assignee_id: (params[:assignee_id] == '0' ? nil : params[:assignee_id])) | ||
107 | + end | ||
108 | + | ||
109 | + items | ||
110 | + end | ||
111 | + | ||
112 | + def by_label(items) | ||
113 | + if params[:label_name].present? | ||
114 | + items = items.tagged_with(params[:label_name]) | ||
115 | + end | ||
116 | + | ||
117 | + items | ||
118 | + end | ||
119 | +end |