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 24 @current_user = current_user
25 25 @params = params
26 26  
27   - items = by_scope
  27 + items = init_collection
  28 + items = by_scope(items)
28 29 items = by_state(items)
29 30 items = by_group(items)
30   - items = by_project(items)
31 31 items = by_search(items)
32 32 items = by_milestone(items)
33 33 items = by_assignee(items)
... ... @@ -37,24 +37,30 @@ class FilteringService
37 37  
38 38 private
39 39  
40   - def by_scope
  40 + def init_collection
41 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 57 case params[:scope]
44 58 when 'created-by-me', 'authored' then
45   - current_user.send(table_name)
  59 + klass.where(author_id: current_user.id)
46 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 62 when 'assigned-to-me' then
57   - current_user.send("assigned_#{table_name}")
  63 + klass.where(assignee_id: current_user.id)
58 64 else
59 65 raise 'You must specify default scope'
60 66 end
... ...