BigW Consortium Gitlab

issues_helper.rb 5.23 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
    if options[:only_path]
      project.issues_tracker.project_path
    else
      project.issues_tracker.project_url
    end
24 25
  end

26
  def url_for_new_issue(project = @project, options = {})
skv committed
27
    return '' if project.nil?
28

29 30 31 32 33
    if options[:only_path]
      project.issues_tracker.new_issue_path
    else
      project.issues_tracker.new_issue_url
    end
34 35
  end

36
  def url_for_issue(issue_iid, project = @project, options = {})
skv committed
37
    return '' if project.nil?
Andrew8xx8 committed
38

39 40 41 42 43
    if options[:only_path]
      project.issues_tracker.issue_path(issue_iid)
    else
      project.issues_tracker.issue_url(issue_iid)
    end
44 45
  end

46
  def bulk_update_milestone_options
47
    milestones = @project.milestones.active.reorder(due_date: :asc, title: :asc).to_a
48 49 50
    milestones.unshift(Milestone::None)

    options_from_collection_for_select(milestones, 'id', 'title', params[:milestone_id])
51 52
  end

53
  def milestone_options(object)
54
    milestones = object.project.milestones.active.reorder(due_date: :asc, title: :asc).to_a
55
    milestones.unshift(object.milestone) if object.milestone.present? && object.milestone.closed?
56 57 58
    milestones.unshift(Milestone::None)

    options_from_collection_for_select(milestones, 'id', 'title', object.milestone_id)
59
  end
60

61 62 63
  def project_options(issuable, current_user, ability: :read_project)
    projects = current_user.authorized_projects
    projects = projects.select do |project|
64
      current_user.can?(ability, project)
65 66
    end

67 68 69
    no_project = OpenStruct.new(id: 0, name_with_namespace: 'No project')
    projects.unshift(no_project)
    projects.delete(issuable.project)
70

71
    options_from_collection_for_select(projects, :id, :name_with_namespace)
72 73
  end

74
  def status_box_class(item)
Dmitriy Zaporozhets committed
75
    if item.respond_to?(:expired?) && item.expired?
76
      'status-box-expired'
Dmitriy Zaporozhets committed
77
    elsif item.respond_to?(:merged?) && item.merged?
78
      'status-box-merged'
Dmitriy Zaporozhets committed
79
    elsif item.closed?
80
      'status-box-closed'
81
    else
82
      'status-box-open'
83 84
    end
  end
85

86
  def issue_button_visibility(issue, closed)
87 88 89
    return 'hidden' if issue.closed? == closed
  end

90 91
  def issue_to_atom(xml, issue)
    xml.entry do
Vinnie Okada committed
92 93 94 95
      xml.id      namespace_project_issue_url(issue.project.namespace,
                                              issue.project, issue)
      xml.link    href: namespace_project_issue_url(issue.project.namespace,
                                                    issue.project, issue)
96
      xml.title   truncate(issue.title, length: 80)
97
      xml.updated issue.created_at.xmlschema
98
      xml.media   :thumbnail, width: "40", height: "40", url: image_url(avatar_icon(issue.author_email))
99 100 101 102 103 104 105
      xml.author do |author|
        xml.name issue.author_name
        xml.email issue.author_email
      end
      xml.summary issue.title
    end
  end
Robert Speicher committed
106

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

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

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

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

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

    list.join(", ")
  end

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

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

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

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

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