notes_helper.rb 2.55 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 22 23 24
  def noteable_json(noteable)
    {
      id: noteable.id,
      class: noteable.class.name,
      resources: noteable.class.table_name,
      project_id: noteable.project.id,
    }.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 37 38 39
      line_code: line_code,
      line_type: line_type,
    }

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

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.id, line_type: line_type }
54
    data[:line_code] = discussion.line_code if discussion.respond_to?(:line_code)
55

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

60 61 62 63
  def preload_max_access_for_authors(notes, project)
    user_ids = notes.map(&:author_id)
    project.team.max_member_access_for_user_ids(user_ids)
  end
64

65
  def preload_noteable_for_regular_notes(notes)
66 67 68 69 70 71 72 73 74
    ActiveRecord::Associations::Preloader.new.preload(notes.reject(&:for_commit?), :noteable)
  end

  def prepare_notes_for_rendering(notes)
    preload_noteable_for_regular_notes(notes)
    preload_max_access_for_authors(notes, @project)
    Banzai::NoteRenderer.render(notes, @project, current_user)

    notes
75 76
  end

77 78
  def note_max_access_for_user(note)
    note.project.team.human_max_access(note.author_id)
79
  end
80

81 82
  def discussion_diff_path(discussion)
    return unless discussion.diff_discussion?
83

84 85 86 87
    if discussion.for_merge_request? && discussion.active?
      diffs_namespace_project_merge_request_path(discussion.project.namespace, discussion.project, discussion.noteable, anchor: discussion.line_code)
    elsif discussion.for_commit?
      namespace_project_commit_path(discussion.project.namespace, discussion.project, discussion.noteable, anchor: discussion.line_code)
88 89
    end
  end
90
end