BigW Consortium Gitlab

issues_helper.rb 4.22 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)
Dmitriy Zaporozhets committed
61
    if item.respond_to?(:expired?) && item.expired?
62
      'status-box-expired'
Dmitriy Zaporozhets committed
63
    elsif item.respond_to?(:merged?) && item.merged?
64
      'status-box-merged'
Dmitriy Zaporozhets committed
65
    elsif item.closed?
66
      'status-box-closed'
67
    else
68
      'status-box-open'
69 70
    end
  end
71

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

76
  def merge_requests_sentence(merge_requests)
77 78 79 80 81
    # 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 ')
82 83
  end

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

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

91 92 93 94 95 96 97 98 99 100 101 102
    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
103
    else
104 105 106 107 108 109 110 111 112 113
      # 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
114 115
  end

116
  def award_user_list(awards, current_user)
117 118 119
    awards.map do |award|
      award.user == current_user ? 'me' : award.user.name
    end.join(', ')
Valery Sizov committed
120 121
  end

122 123
  def award_active_class(awards, current_user)
    if current_user && awards.find { |a| a.user_id == current_user.id }
Valery Sizov committed
124 125 126 127
      "active"
    else
      ""
    end
Valery Sizov committed
128 129
  end

Valery Sizov committed
130 131 132 133 134 135 136 137 138 139 140 141
  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

142 143
  def due_date_options
    options = [
Rémy Coutable committed
144 145 146 147 148
      Issue::AnyDueDate,
      Issue::NoDueDate,
      Issue::DueThisWeek,
      Issue::DueThisMonth,
      Issue::Overdue
149
    ]
150 151

    options_from_collection_for_select(options, 'name', 'title', params[:due_date])
152 153
  end

154
  # Required for Banzai::Filter::IssueReferenceFilter
155
  module_function :url_for_issue
gitlabhq committed
156
end