todos_helper.rb 4.67 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
module TodosHelper
  def todos_pending_count
5
    @todos_pending_count ||= current_user.todos_pending_count
6 7
  end

8
  def todos_count_format(count)
9
    count > 99 ? '99+' : count.to_s
10 11
  end

12
  def todos_done_count
13
    @todos_done_count ||= current_user.todos_done_count
14 15
  end

16 17
  def todo_action_name(todo)
    case todo.action
18 19
    when Todo::ASSIGNED then todo.self_added? ? 'assigned' : 'assigned you'
    when Todo::MENTIONED then "mentioned #{todo_action_subject(todo)} on"
20
    when Todo::BUILD_FAILED then 'The build failed for'
Phil Hughes's avatar
Phil Hughes committed
21
    when Todo::MARKED then 'added a todo for'
22
    when Todo::APPROVAL_REQUIRED then "set #{todo_action_subject(todo)} as an approver for"
23
    when Todo::UNMERGEABLE then 'Could not merge'
24
    when Todo::DIRECTLY_ADDRESSED then "directly addressed #{todo_action_subject(todo)} on"
25 26 27
    end
  end

28
  def todo_target_link(todo)
29 30 31 32 33 34
    text = raw("#{todo.target_type.titleize.downcase} ") +
      if todo.for_commit?
        content_tag(:span, todo.target_reference, class: 'commit-sha')
      else
        todo.target_reference
      end
35

36
    link_to text, todo_target_path(todo), class: 'has-tooltip', title: todo.target.title
37 38 39
  end

  def todo_target_path(todo)
40 41
    return unless todo.target.present?

42 43
    anchor = dom_id(todo.note) if todo.note.present?

44
    if todo.for_commit?
45
      project_commit_path(todo.project,
46 47
                                    todo.target, anchor: anchor)
    else
48
      path = [todo.parent, todo.target]
49 50

      path.unshift(:pipelines) if todo.build_failed?
51 52

      polymorphic_path(path, anchor: anchor)
53
    end
54 55
  end

Alfredo Sumaran's avatar
Alfredo Sumaran committed
56
  def todo_target_state_pill(todo)
57 58
    return unless show_todo_state?(todo)

59 60 61 62 63 64 65 66
    type =
      case todo.target
      when MergeRequest
        'mr'
      when Issue
        'issue'
      end

67
    content_tag(:span, nil, class: 'target-status') do
68
      content_tag(:span, nil, class: "status-box status-box-#{type}-#{todo.target.state.dasherize}") do
69
        todo.target.state.capitalize
Alfredo Sumaran's avatar
Alfredo Sumaran committed
70
      end
Alfredo Sumaran's avatar
Alfredo Sumaran committed
71
    end
Alfredo Sumaran's avatar
Alfredo Sumaran committed
72 73
  end

74 75 76 77 78 79
  def todos_filter_params
    {
      state:      params[:state],
      project_id: params[:project_id],
      author_id:  params[:author_id],
      type:       params[:type],
80
      action_id:  params[:action_id]
81 82 83
    }
  end

84
  def todos_filter_empty?
85
    todos_filter_params.values.none?
86 87
  end

88 89 90 91 92 93 94 95 96 97 98
  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

99
    "#{request.path}?#{options.to_param}"
100 101
  end

102
  def todo_actions_options
103 104 105
    [
      { id: '', text: 'Any Action' },
      { id: Todo::ASSIGNED, text: 'Assigned' },
Jacopo's avatar
Jacopo committed
106 107
      { id: Todo::MENTIONED, text: 'Mentioned' },
      { id: Todo::MARKED, text: 'Added' },
108 109
      { id: Todo::BUILD_FAILED, text: 'Pipelines' },
      { id: Todo::DIRECTLY_ADDRESSED, text: 'Directly addressed' }
110 111 112 113
    ]
  end

  def todo_projects_options
114
    projects = current_user.authorized_projects.sorted_by_activity.non_archived.with_route
115 116

    projects = projects.map do |project|
117
      { id: project.id, text: project.full_name }
118 119
    end

120
    projects.unshift({ id: '', text: 'Any Project' }).to_json
121 122 123
  end

  def todo_types_options
124
    [
125 126 127
      { id: '', text: 'Any Type' },
      { id: 'Issue', text: 'Issue' },
      { id: 'MergeRequest', text: 'Merge Request' }
128 129
    ]
  end
Alfredo Sumaran's avatar
Alfredo Sumaran committed
130

131 132 133 134 135 136 137 138 139 140
  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

141
  def todo_due_date(todo)
142 143
    return unless todo.target.try(:due_date)

144 145 146 147 148 149 150 151 152 153
    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
154

155
    content = content_tag(:span, class: css_class) do
156 157
      "Due #{is_due_today ? "today" : todo.target.due_date.to_s(:medium)}"
    end
158 159

    "· #{content}".html_safe
160 161
  end

Alfredo Sumaran's avatar
Alfredo Sumaran committed
162 163
  private

164 165 166 167
  def todo_action_subject(todo)
    todo.self_added? ? 'yourself' : 'you'
  end

Alfredo Sumaran's avatar
Alfredo Sumaran committed
168
  def show_todo_state?(todo)
Douwe Maan's avatar
Douwe Maan committed
169
    (todo.target.is_a?(MergeRequest) || todo.target.is_a?(Issue)) && %w(closed merged).include?(todo.target.state)
Alfredo Sumaran's avatar
Alfredo Sumaran committed
170
  end
171 172 173 174 175 176 177 178

  def todo_group_options
    groups = current_user.authorized_groups.map do |group|
      { id: group.id, text: group.full_name }
    end

    groups.unshift({ id: '', text: 'Any Group' }).to_json
  end
179
end