summaryrefslogtreecommitdiff
path: root/app/helpers/dashboard_helper.rb
blob: acc0eeb76b36ab83bba42fb6c35888b5ce6d596f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module DashboardHelper
  def filter_path(entity, options={})
    exist_opts = {
      state: params[:state],
      scope: params[:scope],
      project_id: params[:project_id],
    }

    options = exist_opts.merge(options)

    path = request.path
    path << "?#{options.to_param}"
    path
  end

  def entities_per_project(project, entity)
    case entity.to_sym
    when :issue then @issues.where(project_id: project.id)
    when :merge_request then @merge_requests.where(target_project_id: project.id)
    else
      []
    end.count
  end

  def projects_dashboard_filter_path(options={})
    exist_opts = {
      sort: params[:sort],
      scope: params[:scope],
      group: params[:group],
    }

    options = exist_opts.merge(options)

    path = request.path
    path << "?#{options.to_param}"
    path
  end

  def assigned_entities_count(current_user, entity, scope = nil)
    items = current_user.send('assigned_' + entity.pluralize)
    get_count(items, scope)
  end

  def authored_entities_count(current_user, entity, scope = nil)
    items = current_user.send(entity.pluralize)
    get_count(items, scope)
  end

  def authorized_entities_count(current_user, entity, scope = nil)
    items = entity.classify.constantize
    get_count(items, scope, true, current_user)
  end

  protected

  def get_count(items, scope, get_authorized = false, current_user = nil)
    items = items.opened
    if scope.kind_of?(Group)
      items = items.of_group(scope)
    elsif scope.kind_of?(Project)
      items = items.of_projects(scope)
    elsif get_authorized
      items = items.of_projects(current_user.authorized_projects)
    end
    items.count
  end
end