BigW Consortium Gitlab

todos_helper.rb 3.84 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
  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

64
  def todos_filter_empty?
65
    todos_filter_params.values.none?
66 67
  end

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  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

84
  def todo_actions_options
85 86 87 88
    [
      { id: '', text: 'Any Action' },
      { id: Todo::ASSIGNED, text: 'Assigned' },
      { id: Todo::MENTIONED, text: 'Mentioned' }
89 90 91 92 93 94 95 96
    ]
  end

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

    projects = projects.map do |project|
97
      { id: project.id, text: project.name_with_namespace }
98 99
    end

100
    projects.unshift({ id: '', text: 'Any Project' }).to_json
101 102 103
  end

  def todo_types_options
104
    [
105 106 107
      { id: '', text: 'Any Type' },
      { id: 'Issue', text: 'Issue' },
      { id: 'MergeRequest', text: 'Merge Request' }
108 109
    ]
  end
Alfredo Sumaran committed
110

111 112 113 114 115 116 117 118 119 120
  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

121
  def todo_due_date(todo)
122 123
    return unless todo.target.try(:due_date)

124 125 126 127 128 129 130 131 132 133
    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
134

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

Alfredo Sumaran committed
141 142 143 144 145
  private

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