BigW Consortium Gitlab

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

  def todos_done_count
7
    @todos_done_count ||= current_user.todos_done_count
8 9
  end

10 11 12 13
  def todo_action_name(todo)
    case todo.action
    when Todo::ASSIGNED then 'assigned you'
    when Todo::MENTIONED then 'mentioned you on'
14
    when Todo::BUILD_FAILED then 'The build failed for your'
Phil Hughes committed
15
    when Todo::MARKED then 'added a todo for'
16
    when Todo::APPROVAL_REQUIRED then 'set you as an approver for'
17 18 19
    end
  end

20 21
  def todo_target_link(todo)
    target = todo.target_type.titleize.downcase
22 23 24
    link_to "#{target} #{todo.target_reference}", todo_target_path(todo),
      class: 'has-tooltip',
      title: todo.target.title
25 26 27
  end

  def todo_target_path(todo)
28 29
    return unless todo.target.present?

30 31
    anchor = dom_id(todo.note) if todo.note.present?

32 33 34 35
    if todo.for_commit?
      namespace_project_commit_path(todo.project.namespace.becomes(Namespace), todo.project,
                                    todo.target, anchor: anchor)
    else
36 37 38 39 40
      path = [todo.project.namespace.becomes(Namespace), todo.project, todo.target]

      path.unshift(:builds) if todo.build_failed?

      polymorphic_path(path, anchor: anchor)
41
    end
42 43
  end

Alfredo Sumaran committed
44
  def todo_target_state_pill(todo)
45 46 47 48 49
    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
Alfredo Sumaran committed
50
      end
Alfredo Sumaran committed
51
    end
Alfredo Sumaran committed
52 53
  end

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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],
      action_id:  params[:action_id],
    }
  end

  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

80
  def todo_actions_options
81 82 83 84
    [
      { id: '', text: 'Any Action' },
      { id: Todo::ASSIGNED, text: 'Assigned' },
      { id: Todo::MENTIONED, text: 'Mentioned' }
85 86 87 88 89 90 91 92
    ]
  end

  def todo_projects_options
    projects = current_user.authorized_projects.sorted_by_activity.non_archived
    projects = projects.includes(:namespace)

    projects = projects.map do |project|
93
      { id: project.id, text: project.name_with_namespace }
94 95
    end

96
    projects.unshift({ id: '', text: 'Any Project' }).to_json
97 98 99
  end

  def todo_types_options
100
    [
101 102 103
      { id: '', text: 'Any Type' },
      { id: 'Issue', text: 'Issue' },
      { id: 'MergeRequest', text: 'Merge Request' }
104 105
    ]
  end
Alfredo Sumaran committed
106

107 108 109 110 111 112 113 114 115 116
  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

117
  def todo_due_date(todo)
118 119
    return unless todo.target.try(:due_date)

120 121 122 123 124 125 126 127 128 129
    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
130

131
    html = "&middot; ".html_safe
132
    html << content_tag(:span, class: css_class) do
133 134
      "Due #{is_due_today ? "today" : todo.target.due_date.to_s(:medium)}"
    end
135 136
  end

Alfredo Sumaran committed
137 138 139 140 141
  private

  def show_todo_state?(todo)
    (todo.target.is_a?(MergeRequest) || todo.target.is_a?(Issue)) && ['closed', 'merged'].include?(todo.target.state)
  end
142
end