Module: NotesActions

Extended by:
ActiveSupport::Concern
Includes:
Gitlab::Utils::StrongMemoize, RendersNotes
Included in:
Projects::NotesController, Snippets::NotesController
Defined in:
app/controllers/concerns/notes_actions.rb

Constant Summary collapse

MICROSECOND =

last_fetched_at is an integer number of microseconds, which is the same precision as PostgreSQL “timestamp” fields.

1_000_000

Instance Method Summary collapse

Methods included from RendersNotes

#prepare_notes_for_rendering

Instance Method Details

#createObject

rubocop:disable Gitlab/ModuleWithInstanceVariables



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/concerns/notes_actions.rb', line 40

def create
  @note = Notes::CreateService.new(note_project, current_user, create_note_params).execute

  respond_to do |format|
    format.json do
      json = {
        commands_changes: @note.commands_changes&.slice(:emoji_award, :time_estimate, :spend_time)
      }

      if @note.persisted? && return_discussion?
        json[:valid] = true

        discussion = @note.discussion
        prepare_notes_for_rendering(discussion.notes)
        json[:discussion] = discussion_serializer.represent(discussion, context: self)
      else
        prepare_notes_for_rendering([@note])

        json.merge!(note_json(@note))
      end

      quick_actions = @note.quick_actions_status
      json[:quick_actions_status] = quick_actions.to_h if quick_actions

      if @note.errors.present?
        render json: { errors: errors_on_create(@note) }, status: :unprocessable_entity
      elsif quick_actions&.error?
        render json: { quick_actions_status: quick_actions.to_h }, status: :unprocessable_entity
      else
        render json: json
      end
    end
    format.html { redirect_back_or_default }
  end
end

#destroyObject

rubocop:enable Gitlab/ModuleWithInstanceVariables



105
106
107
108
109
110
111
# File 'app/controllers/concerns/notes_actions.rb', line 105

def destroy
  Notes::DestroyService.new(project, current_user).execute(note) if note.editable?

  respond_to do |format|
    format.js { head :ok }
  end
end

#indexObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/concerns/notes_actions.rb', line 25

def index
  notes, meta = gather_all_notes
  notes = prepare_notes_for_rendering(notes)
  notes = notes.select { |n| n.readable_by?(current_user) }
  notes =
    if use_note_serializer?
      note_serializer.represent(notes)
    else
      notes.map { |note| note_json(note) }
    end

  render json: meta.merge(notes: notes)
end

#updateObject

rubocop:disable Gitlab/ModuleWithInstanceVariables



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/concerns/notes_actions.rb', line 78

def update
  @note = Notes::UpdateService.new(project, current_user, update_note_params).execute(note)
  if @note.destroyed?
    head :gone
    return
  end

  respond_to do |format|
    format.json do
      if @note.errors.present?
        render json: { errors: @note.errors.full_messages.to_sentence }, status: :unprocessable_entity
      else
        prepare_notes_for_rendering([@note])

        if use_rapid_diffs_serializer?
          render json: { note: RapidDiffs::NoteEntity.represent(@note, rapid_diffs_serializer_options) }
        else
          render json: note_json(@note)
        end
      end
    end

    format.html { redirect_back_or_default }
  end
end