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

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

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

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

26
  def todo_target_link(todo)
27 28 29 30 31 32
    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
33

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

  def todo_target_path(todo)
38 39
    return unless todo.target.present?

40 41
    anchor = dom_id(todo.note) if todo.note.present?

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

      path.unshift(:pipelines) if todo.build_failed?
49 50

      polymorphic_path(path, anchor: anchor)
51
    end
52 53
  end

A
Alfredo Sumaran 已提交
54
  def todo_target_state_pill(todo)
55 56
    return unless show_todo_state?(todo)

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

65
    content_tag(:span, nil, class: 'target-status') do
66
      content_tag(:span, nil, class: "status-box status-box-#{type}-#{todo.target.state.dasherize}") do
67
        todo.target.state.capitalize
A
Alfredo Sumaran 已提交
68
      end
A
Alfredo Sumaran 已提交
69
    end
A
Alfredo Sumaran 已提交
70 71
  end

D
Douwe Maan 已提交
72 73 74 75 76 77
  def todos_filter_params
    {
      state:      params[:state],
      project_id: params[:project_id],
      author_id:  params[:author_id],
      type:       params[:type],
78
      action_id:  params[:action_id]
D
Douwe Maan 已提交
79 80 81
    }
  end

82
  def todos_filter_empty?
83
    todos_filter_params.values.none?
84 85
  end

D
Douwe Maan 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  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

102
  def todo_actions_options
103 104 105
    [
      { id: '', text: 'Any Action' },
      { id: Todo::ASSIGNED, text: 'Assigned' },
J
Jacopo 已提交
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
A
Alfredo Sumaran 已提交
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)
P
Phil Hughes 已提交
142 143
    return unless todo.target.try(:due_date)

P
Phil Hughes 已提交
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

P
Phil Hughes 已提交
155
    html = "&middot; ".html_safe
P
Phil Hughes 已提交
156
    html << content_tag(:span, class: css_class) do
P
Phil Hughes 已提交
157 158
      "Due #{is_due_today ? "today" : todo.target.due_date.to_s(:medium)}"
    end
159 160
  end

A
Alfredo Sumaran 已提交
161 162
  private

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

A
Alfredo Sumaran 已提交
167
  def show_todo_state?(todo)
D
Douwe Maan 已提交
168
    (todo.target.is_a?(MergeRequest) || todo.target.is_a?(Issue)) && %w(closed merged).include?(todo.target.state)
A
Alfredo Sumaran 已提交
169
  end
J
Jan Provaznik 已提交
170 171 172 173 174 175 176 177 178 179

  def todo_group_options
    groups = current_user.authorized_groups

    groups = groups.map do |group|
      { id: group.id, text: group.full_name }
    end

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