Commit 4b0fb02816e91eac09ce84c2383b0275b35aef97

Authored by Marin Jankovski
1 parent 70fb26a6

Do not load all projects all the time.

Showing 1 changed file with 20 additions and 14 deletions   Show diff stats
app/services/filtering_service.rb
@@ -24,10 +24,10 @@ class FilteringService @@ -24,10 +24,10 @@ class FilteringService
24 @current_user = current_user 24 @current_user = current_user
25 @params = params 25 @params = params
26 26
27 - items = by_scope 27 + items = init_collection
  28 + items = by_scope(items)
28 items = by_state(items) 29 items = by_state(items)
29 items = by_group(items) 30 items = by_group(items)
30 - items = by_project(items)  
31 items = by_search(items) 31 items = by_search(items)
32 items = by_milestone(items) 32 items = by_milestone(items)
33 items = by_assignee(items) 33 items = by_assignee(items)
@@ -37,24 +37,30 @@ class FilteringService @@ -37,24 +37,30 @@ class FilteringService
37 37
38 private 38 private
39 39
40 - def by_scope 40 + def init_collection
41 table_name = klass.table_name 41 table_name = klass.table_name
42 42
  43 + return klass.of_projects(Project.public_only) unless current_user
  44 +
  45 + if project
  46 + if current_user.can?(:read_project, project)
  47 + project.send(table_name)
  48 + else
  49 + []
  50 + end
  51 + else
  52 + klass.of_projects(current_user.authorized_projects)
  53 + end
  54 + end
  55 +
  56 + def by_scope(items)
43 case params[:scope] 57 case params[:scope]
44 when 'created-by-me', 'authored' then 58 when 'created-by-me', 'authored' then
45 - current_user.send(table_name) 59 + klass.where(author_id: current_user.id)
46 when 'all' then 60 when 'all' then
47 - if current_user  
48 - if project && (project.public? || project.internal?)  
49 - klass.of_projects(Project.public_or_internal_only(current_user))  
50 - else  
51 - klass.of_projects(current_user.authorized_projects.pluck(:id))  
52 - end  
53 - else  
54 - klass.of_projects(Project.public_only)  
55 - end 61 + klass
56 when 'assigned-to-me' then 62 when 'assigned-to-me' then
57 - current_user.send("assigned_#{table_name}") 63 + klass.where(assignee_id: current_user.id)
58 else 64 else
59 raise 'You must specify default scope' 65 raise 'You must specify default scope'
60 end 66 end