notes_helper.rb 3.2 KB
Newer Older
1
module NotesHelper
2
  def note_target_fields(note)
3 4 5 6
    if note.noteable
      hidden_field_tag(:target_type, note.noteable.class.name.underscore) +
        hidden_field_tag(:target_id, note.noteable.id)
    end
7 8
  end

9
  def note_editable?(note)
10
    Ability.can_edit_note?(current_user, note)
11 12
  end

13 14 15 16
  def note_supports_slash_commands?(note)
    Notes::SlashCommandsService.supported?(note, current_user)
  end

17 18 19 20 21
  def noteable_json(noteable)
    {
      id: noteable.id,
      class: noteable.class.name,
      resources: noteable.class.table_name,
22
      project_id: noteable.project.id
23 24
    }.to_json
  end
25

26
  def diff_view_data
27
    return {} unless @new_diff_note_attrs
28

29
    @new_diff_note_attrs.slice(:noteable_id, :noteable_type, :commit_id)
30 31
  end

32 33
  def diff_view_line_data(line_code, position, line_type)
    return if @diff_notes_disabled
34

35
    data = {
36
      line_code: line_code,
37
      line_type: line_type
38 39
    }

40
    if @use_legacy_diff_notes
D
Douwe Maan 已提交
41
      data[:note_type] = LegacyDiffNote.name
42
    else
D
Douwe Maan 已提交
43
      data[:note_type] = DiffNote.name
44
      data[:position] = position.to_json
45
    end
46

D
Douwe Maan 已提交
47
    data
48
  end
49

50
  def link_to_reply_discussion(discussion, line_type = nil)
51 52
    return unless current_user

53
    data = { discussion_id: discussion.reply_id, line_type: line_type }
D
Douwe Maan 已提交
54

55 56
    button_tag 'Reply...', class: 'btn btn-text-field js-discussion-reply-button',
                           data: data, title: 'Add a reply'
57
  end
58

59 60
  def note_max_access_for_user(note)
    note.project.team.human_max_access(note.author_id)
61
  end
62

63 64 65 66 67 68 69 70 71 72
  def discussion_path(discussion)
    if discussion.for_merge_request?
      return unless discussion.diff_discussion?

      version_params = discussion.merge_request_version_params
      return unless version_params

      path_params = version_params.merge(anchor: discussion.line_code)

      diffs_namespace_project_merge_request_path(discussion.project.namespace, discussion.project, discussion.noteable, path_params)
73
    elsif discussion.for_commit?
D
Douwe Maan 已提交
74
      anchor = discussion.line_code if discussion.diff_discussion?
D
Douwe Maan 已提交
75

D
Douwe Maan 已提交
76
      namespace_project_commit_path(discussion.project.namespace, discussion.project, discussion.noteable, anchor: anchor)
77 78
    end
  end
79 80 81 82 83 84 85 86 87 88 89 90 91 92

  def notes_url
    if @snippet.is_a?(PersonalSnippet)
      snippet_notes_path(@snippet)
    else
      namespace_project_noteable_notes_path(
        namespace_id: @project.namespace,
        project_id: @project,
        target_id: @noteable.id,
        target_type: @noteable.class.name.underscore
      )
    end
  end

93
  def note_url(note, project = @project)
94 95 96
    if note.noteable.is_a?(PersonalSnippet)
      snippet_note_path(note.noteable, note)
    else
97
      namespace_project_note_path(project.namespace, project, note)
98 99 100
    end
  end

101 102 103 104
  def noteable_note_url(note)
    Gitlab::UrlBuilder.build(note)
  end

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  def form_resources
    if @snippet.is_a?(PersonalSnippet)
      [@note]
    else
      [@project.namespace.becomes(Namespace), @project, @note]
    end
  end

  def new_form_url
    return nil unless @snippet.is_a?(PersonalSnippet)

    snippet_notes_path(@snippet)
  end

  def can_create_note?
    if @snippet.is_a?(PersonalSnippet)
      can?(current_user, :comment_personal_snippet, @snippet)
    else
      can?(current_user, :create_note, @project)
    end
  end
126
end