todos_helper.rb 4.1 KB
Newer Older
1 2
module TodosHelper
  def todos_pending_count
3
    @todos_pending_count ||= current_user.todos_pending_count
4 5
  end

6 7 8 9
  def todos_count_format(count)
    count > 99 ? '99+' : count
  end

10
  def todos_done_count
11
    @todos_done_count ||= current_user.todos_done_count
12 13
  end

14 15 16 17
  def todo_action_name(todo)
    case todo.action
    when Todo::ASSIGNED then 'assigned you'
    when Todo::MENTIONED then 'mentioned you on'
18
    when Todo::BUILD_FAILED then 'The build failed for'
P
Phil Hughes 已提交
19
    when Todo::MARKED then 'added a todo for'
S
Sean McGivern 已提交
20
    when Todo::APPROVAL_REQUIRED then 'set you as an approver for'
21
    when Todo::UNMERGEABLE then 'Could not merge'
22
    when Todo::DIRECTLY_ADDRESSED then 'directly addressed you on'
23 24 25
    end
  end

26 27
  def todo_target_link(todo)
    target = todo.target_type.titleize.downcase
28 29 30
    link_to "#{target} #{todo.target_reference}", todo_target_path(todo),
      class: 'has-tooltip',
      title: todo.target.title
31 32 33
  end

  def todo_target_path(todo)
34 35
    return unless todo.target.present?

36 37
    anchor = dom_id(todo.note) if todo.note.present?

38 39 40 41
    if todo.for_commit?
      namespace_project_commit_path(todo.project.namespace.becomes(Namespace), todo.project,
                                    todo.target, anchor: anchor)
    else
42 43
      path = [todo.project.namespace.becomes(Namespace), todo.project, todo.target]

N
Nick Thomas 已提交
44
      path.unshift(:pipelines) if todo.build_failed?
45 46

      polymorphic_path(path, anchor: anchor)
47
    end
48 49
  end

A
Alfredo Sumaran 已提交
50
  def todo_target_state_pill(todo)
51 52 53 54 55
    return unless show_todo_state?(todo)

    content_tag(:span, nil, class: 'target-status') do
      content_tag(:span, nil, class: "status-box status-box-#{todo.target.state.dasherize}") do
        todo.target.state.capitalize
A
Alfredo Sumaran 已提交
56
      end
A
Alfredo Sumaran 已提交
57
    end
A
Alfredo Sumaran 已提交
58 59
  end

D
Douwe Maan 已提交
60 61 62 63 64 65 66 67 68 69
  def todos_filter_params
    {
      state:      params[:state],
      project_id: params[:project_id],
      author_id:  params[:author_id],
      type:       params[:type],
      action_id:  params[:action_id],
    }
  end

70
  def todos_filter_empty?
71
    todos_filter_params.values.none?
72 73
  end

D
Douwe Maan 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  def todos_filter_path(options = {})
    without = options.delete(:without)

    options = todos_filter_params.merge(options)

    if without.present?
      without.each do |key|
        options.delete(key)
      end
    end

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

90
  def todo_actions_options
91 92 93
    [
      { id: '', text: 'Any Action' },
      { id: Todo::ASSIGNED, text: 'Assigned' },
J
Jacopo 已提交
94 95
      { id: Todo::MENTIONED, text: 'Mentioned' },
      { id: Todo::MARKED, text: 'Added' },
96 97
      { id: Todo::BUILD_FAILED, text: 'Pipelines' },
      { id: Todo::DIRECTLY_ADDRESSED, text: 'Directly addressed' }
98 99 100 101
    ]
  end

  def todo_projects_options
102
    projects = current_user.authorized_projects.sorted_by_activity.non_archived.with_route
103 104

    projects = projects.map do |project|
105
      { id: project.id, text: project.name_with_namespace }
106 107
    end

108
    projects.unshift({ id: '', text: 'Any Project' }).to_json
109 110 111
  end

  def todo_types_options
112
    [
113 114 115
      { id: '', text: 'Any Type' },
      { id: 'Issue', text: 'Issue' },
      { id: 'MergeRequest', text: 'Merge Request' }
116 117
    ]
  end
A
Alfredo Sumaran 已提交
118

119 120 121 122 123 124 125 126 127 128
  def todo_actions_dropdown_label(selected_action_id, default_action)
    selected_action = todo_actions_options.find { |action| action[:id] == selected_action_id.to_i}
    selected_action ? selected_action[:text] : default_action
  end

  def todo_types_dropdown_label(selected_type, default_type)
    selected_type = todo_types_options.find { |type| type[:id] == selected_type && type[:id] != '' }
    selected_type ? selected_type[:text] : default_type
  end

129
  def todo_due_date(todo)
P
Phil Hughes 已提交
130 131
    return unless todo.target.try(:due_date)

P
Phil Hughes 已提交
132 133 134 135 136 137 138 139 140 141
    is_due_today = todo.target.due_date.today?
    is_overdue = todo.target.overdue?
    css_class =
      if is_due_today
        'text-warning'
      elsif is_overdue
        'text-danger'
      else
        ''
      end
142

P
Phil Hughes 已提交
143
    html = "&middot; ".html_safe
P
Phil Hughes 已提交
144
    html << content_tag(:span, class: css_class) do
P
Phil Hughes 已提交
145 146
      "Due #{is_due_today ? "today" : todo.target.due_date.to_s(:medium)}"
    end
147 148
  end

A
Alfredo Sumaran 已提交
149 150 151
  private

  def show_todo_state?(todo)
D
Douwe Maan 已提交
152
    (todo.target.is_a?(MergeRequest) || todo.target.is_a?(Issue)) && %w(closed merged).include?(todo.target.state)
A
Alfredo Sumaran 已提交
153
  end
154
end