todos_helper.rb 3.17 KB
Newer Older
1 2
module TodosHelper
  def todos_pending_count
3
    TodosFinder.new(current_user, state: :pending).execute.count
4 5 6
  end

  def todos_done_count
7
    TodosFinder.new(current_user, state: :done).execute.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's avatar
Phil Hughes committed
15
    when Todo::MARKED then 'added a todo for'
16 17 18
    end
  end

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

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

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

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

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

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

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

Douwe Maan's avatar
Douwe Maan committed
53 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
  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

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  def todo_actions_options
    actions = [
      OpenStruct.new(id: '', title: 'Any Action'),
      OpenStruct.new(id: Todo::ASSIGNED, title: 'Assigned'),
      OpenStruct.new(id: Todo::MENTIONED, title: 'Mentioned')
    ]

    options_from_collection_for_select(actions, 'id', 'title', params[:action_id])
  end

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

    projects = projects.map do |project|
      OpenStruct.new(id: project.id, title: project.name_with_namespace)
    end

    projects.unshift(OpenStruct.new(id: '', title: 'Any Project'))

    options_from_collection_for_select(projects, 'id', 'title', params[:project_id])
  end

  def todo_types_options
    types = [
      OpenStruct.new(title: 'Any Type', name: ''),
      OpenStruct.new(title: 'Issue', name: 'Issue'),
      OpenStruct.new(title: 'Merge Request', name: 'MergeRequest')
    ]

    options_from_collection_for_select(types, 'name', 'title', params[:type])
  end
Alfredo Sumaran's avatar
Alfredo Sumaran committed
111 112 113 114 115 116

  private

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