BigW Consortium Gitlab

issues_helper.rb 4.49 KB
Newer Older
gitlabhq committed
1
module IssuesHelper
2
  def issue_css_classes(issue)
Dmitriy Zaporozhets committed
3
    classes = "issue"
Andrew8xx8 committed
4
    classes << " closed" if issue.closed?
Dmitriy Zaporozhets committed
5 6 7
    classes << " today" if issue.today?
    classes
  end
8

9 10 11 12
  # Returns an OpenStruct object suitable for use by <tt>options_from_collection_for_select</tt>
  # to allow filtering issues by an unassigned User or Milestone
  def unassigned_filter
    # Milestone uses :title, Issue uses :name
13
    OpenStruct.new(id: 0, title: 'None (backlog)', name: 'Unassigned')
14
  end
15

16
  def url_for_issue(issue_iid, project = @project, options = {})
skv committed
17
    return '' if project.nil?
Andrew8xx8 committed
18

19 20 21 22 23 24 25 26 27 28 29
    url =
      if options[:only_path]
        project.issues_tracker.issue_path(issue_iid)
      else
        project.issues_tracker.issue_url(issue_iid)
      end

    # Ensure we return a valid URL to prevent possible XSS.
    URI.parse(url).to_s
  rescue URI::InvalidURIError
    ''
30 31
  end

32
  def bulk_update_milestone_options
33
    milestones = @project.milestones.active.reorder(due_date: :asc, title: :asc).to_a
34 35 36
    milestones.unshift(Milestone::None)

    options_from_collection_for_select(milestones, 'id', 'title', params[:milestone_id])
37 38
  end

39
  def milestone_options(object)
40
    milestones = object.project.milestones.active.reorder(due_date: :asc, title: :asc).to_a
41
    milestones.unshift(object.milestone) if object.milestone.present? && object.milestone.closed?
42 43 44
    milestones.unshift(Milestone::None)

    options_from_collection_for_select(milestones, 'id', 'title', object.milestone_id)
45
  end
46

47 48 49
  def project_options(issuable, current_user, ability: :read_project)
    projects = current_user.authorized_projects
    projects = projects.select do |project|
50
      current_user.can?(ability, project)
51 52
    end

53 54 55
    no_project = OpenStruct.new(id: 0, name_with_namespace: 'No project')
    projects.unshift(no_project)
    projects.delete(issuable.project)
56

57
    options_from_collection_for_select(projects, :id, :name_with_namespace)
58 59
  end

60
  def status_box_class(item)
61
    if item.try(:expired?)
62
      'status-box-expired'
63
    elsif item.try(:merged?)
64
      'status-box-merged'
Dmitriy Zaporozhets committed
65
    elsif item.closed?
66
      'status-box-closed'
67
    elsif item.try(:upcoming?)
68
      'status-box-upcoming'
69
    else
70
      'status-box-open'
71 72
    end
  end
73

74
  def issue_button_visibility(issue, closed)
75 76 77
    return 'hidden' if issue.closed? == closed
  end

78
  def merge_requests_sentence(merge_requests)
79 80 81 82 83
    # Sorting based on the `!123` or `group/project!123` reference will sort
    # local merge requests first.
    merge_requests.map do |merge_request|
      merge_request.to_reference(@project)
    end.sort.to_sentence(last_word_connector: ', or ')
84 85
  end

86 87 88 89
  def confidential_icon(issue)
    icon('eye-slash') if issue.confidential?
  end

90
  def emoji_icon(name, unicode = nil, aliases = [], sprite: true)
91
    unicode ||= Gitlab::Emoji.emoji_filename(name) rescue ""
92

93 94 95 96 97 98 99 100 101 102 103 104
    data = {
      aliases: aliases.join(" "),
      emoji: name,
      unicode_name: unicode
    }

    if sprite
      # Emoji icons for the emoji menu, these use a spritesheet.
      content_tag :div, "",
        class: "icon emoji-icon emoji-#{unicode}",
        title: name,
        data: data
105
    else
106 107 108 109 110 111 112 113 114 115
      # Emoji icons displayed separately, used for the awards already given
      # to an issue or merge request.
      content_tag :img, "",
        class: "icon emoji",
        title: name,
        height: "20px",
        width: "20px",
        src: url_to_image("#{unicode}.png"),
        data: data
    end
Valery Sizov committed
116 117
  end

118
  def award_user_list(awards, current_user, limit: 10)
119
    names = awards.map do |award|
120
      award.user == current_user ? 'You' : award.user.name
121 122
    end

123
    current_user_name = names.delete('You')
124
    names = names.insert(0, current_user_name).compact.first(limit)
125

126
    names << "#{awards.size - names.size} more." if awards.size > names.size
127

128
    names.to_sentence
Valery Sizov committed
129 130
  end

131 132 133 134
  def award_state_class(awards, current_user)
    if !current_user
      "disabled"
    elsif current_user && awards.find { |a| a.user_id == current_user.id }
Valery Sizov committed
135 136 137 138
      "active"
    else
      ""
    end
Valery Sizov committed
139 140
  end

Valery Sizov committed
141 142 143 144 145 146 147 148 149 150 151 152
  def awards_sort(awards)
    awards.sort_by do |award, notes|
      if award == "thumbsup"
        0
      elsif award == "thumbsdown"
        1
      else
        2
      end
    end.to_h
  end

153 154
  def due_date_options
    options = [
Rémy Coutable committed
155 156 157 158 159
      Issue::AnyDueDate,
      Issue::NoDueDate,
      Issue::DueThisWeek,
      Issue::DueThisMonth,
      Issue::Overdue
160
    ]
161 162

    options_from_collection_for_select(options, 'name', 'title', params[:due_date])
163 164
  end

165
  # Required for Banzai::Filter::IssueReferenceFilter
166
  module_function :url_for_issue
gitlabhq committed
167
end