BigW Consortium Gitlab

issues_helper.rb 4.95 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_project_issues(project = @project, options = {})
skv committed
17
    return '' if project.nil?
18

19 20 21 22 23 24 25 26 27 28 29
    url =
      if options[:only_path]
        project.issues_tracker.project_path
      else
        project.issues_tracker.project_url
      end

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

32
  def url_for_new_issue(project = @project, options = {})
skv committed
33
    return '' if project.nil?
34

35 36 37 38 39 40 41 42 43 44 45
    url =
      if options[:only_path]
        project.issues_tracker.new_issue_path
      else
        project.issues_tracker.new_issue_url
      end

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

48
  def url_for_issue(issue_iid, project = @project, options = {})
skv committed
49
    return '' if project.nil?
Andrew8xx8 committed
50

51 52 53 54 55 56 57 58 59 60 61
    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
    ''
62 63
  end

64
  def bulk_update_milestone_options
65
    milestones = @project.milestones.active.reorder(due_date: :asc, title: :asc).to_a
66 67 68
    milestones.unshift(Milestone::None)

    options_from_collection_for_select(milestones, 'id', 'title', params[:milestone_id])
69 70
  end

71
  def milestone_options(object)
72
    milestones = object.project.milestones.active.reorder(due_date: :asc, title: :asc).to_a
73
    milestones.unshift(object.milestone) if object.milestone.present? && object.milestone.closed?
74 75 76
    milestones.unshift(Milestone::None)

    options_from_collection_for_select(milestones, 'id', 'title', object.milestone_id)
77
  end
78

79 80 81
  def project_options(issuable, current_user, ability: :read_project)
    projects = current_user.authorized_projects
    projects = projects.select do |project|
82
      current_user.can?(ability, project)
83 84
    end

85 86 87
    no_project = OpenStruct.new(id: 0, name_with_namespace: 'No project')
    projects.unshift(no_project)
    projects.delete(issuable.project)
88

89
    options_from_collection_for_select(projects, :id, :name_with_namespace)
90 91
  end

92
  def status_box_class(item)
Dmitriy Zaporozhets committed
93
    if item.respond_to?(:expired?) && item.expired?
94
      'status-box-expired'
Dmitriy Zaporozhets committed
95
    elsif item.respond_to?(:merged?) && item.merged?
96
      'status-box-merged'
Dmitriy Zaporozhets committed
97
    elsif item.closed?
98
      'status-box-closed'
99
    else
100
      'status-box-open'
101 102
    end
  end
103

104
  def issue_button_visibility(issue, closed)
105 106 107
    return 'hidden' if issue.closed? == closed
  end

108
  def merge_requests_sentence(merge_requests)
109 110 111 112 113
    # 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 ')
114 115
  end

116 117 118 119
  def confidential_icon(issue)
    icon('eye-slash') if issue.confidential?
  end

120
  def emoji_icon(name, unicode = nil, aliases = [], sprite: true)
121
    unicode ||= Emoji.emoji_filename(name) rescue ""
122

123 124 125 126 127 128 129 130 131 132 133 134
    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
Rémy Coutable committed
135
    else
136 137 138 139 140 141 142 143 144 145
      # 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
146 147
  end

Valery Sizov committed
148 149
  def emoji_author_list(notes, current_user)
    list = notes.map do |note|
150 151
      note.author == current_user ? "me" : note.author.name
    end
Valery Sizov committed
152 153 154 155

    list.join(", ")
  end

Valery Sizov committed
156
  def note_active_class(notes, current_user)
Valery Sizov committed
157 158 159 160 161
    if current_user && notes.pluck(:author_id).include?(current_user.id)
      "active"
    else
      ""
    end
Valery Sizov committed
162 163
  end

Valery Sizov committed
164 165 166 167 168 169 170 171 172 173 174 175
  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

176 177
  def due_date_options
    options = [
Rémy Coutable committed
178 179 180 181 182
      Issue::AnyDueDate,
      Issue::NoDueDate,
      Issue::DueThisWeek,
      Issue::DueThisMonth,
      Issue::Overdue
183
    ]
184 185

    options_from_collection_for_select(options, 'name', 'title', params[:due_date])
186 187
  end

188
  # Required for Banzai::Filter::IssueReferenceFilter
189
  module_function :url_for_issue
gitlabhq committed
190
end