Skip to content

Commit eb6625b

Browse files
committed
Add a note delete button - you can now delete notes from within threads and the activity tab
Signed-off-by: Kai Wagner <[email protected]>
1 parent 5112464 commit eb6625b

File tree

6 files changed

+53
-5
lines changed

6 files changed

+53
-5
lines changed

app/assets/stylesheets/components/activities.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
color: var(--color-text-secondary);
5757
}
5858

59+
.activity-actions {
60+
margin-top: var(--spacing-2);
61+
}
62+
5963
.activity-tags {
6064
display: flex;
6165
flex-wrap: wrap;

app/assets/stylesheets/components/notes.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@
121121
justify-content: flex-end;
122122
}
123123

124+
.note-actions-row {
125+
display: flex;
126+
align-items: center;
127+
gap: var(--spacing-3);
128+
margin-top: var(--spacing-2);
129+
}
130+
131+
.note-delete-button {
132+
padding: 0;
133+
background: none;
134+
border: none;
135+
color: var(--color-text-link);
136+
font-size: var(--font-size-sm);
137+
font-weight: var(--font-weight-medium);
138+
cursor: pointer;
139+
text-decoration: none;
140+
141+
&:hover {
142+
text-decoration: underline;
143+
}
144+
}
145+
124146
.notes-inline-add {
125147
margin: var(--spacing-3) 0;
126148
}

app/controllers/notes_controller.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class NotesController < ApplicationController
44
before_action :require_authentication
5-
before_action :set_note, only: [:update]
5+
before_action :set_note, only: [:update, :destroy]
66

77
def create
88
topic = Topic.find(note_params[:topic_id])
@@ -39,10 +39,27 @@ def update
3939
redirect_back fallback_location: topic_path(@note.topic)
4040
end
4141

42+
def destroy
43+
return if performed?
44+
45+
@note.transaction do
46+
@note.update!(deleted_at: Time.current)
47+
@note.note_mentions.delete_all
48+
@note.note_tags.delete_all
49+
@note.activities.update_all(hidden: true)
50+
end
51+
52+
redirect_back fallback_location: topic_path(@note.topic), notice: "Note deleted"
53+
end
54+
4255
private
4356

4457
def set_note
4558
@note = Note.find(params[:id])
59+
if @note.deleted_at?
60+
redirect_back fallback_location: topic_path(@note.topic), alert: "Note has been deleted"
61+
return
62+
end
4663
unless @note.author_id == current_user.id
4764
redirect_back fallback_location: topic_path(@note.topic), alert: "You can only edit your own notes"
4865
return

app/views/activities/index.html.slim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
.activity-tags
3030
- note.note_tags.each do |tag|
3131
span.note-tag ##{tag.tag}
32+
- if current_user.id == note.author_id
33+
.activity-actions
34+
= button_to "Delete note", note_path(note), method: :delete, class: "note-delete-button", form: { data: { turbo_confirm: "Delete this note?" } }
3235
- else
3336
.activity-body
3437
span.activity-missing Content not available anymore.

app/views/notes/_note.html.slim

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
span.note-mention = note_mention_label(mention)
2020

2121
- if current_user&.id == note.author_id
22-
details.note-edit-toggle
23-
summary Edit
24-
= render "notes/form", note: note, topic: note.topic, message: note.message, submit_text: "Update note"
22+
.note-actions-row
23+
details.note-edit-toggle
24+
summary Edit
25+
= render "notes/form", note: note, topic: note.topic, message: note.message, submit_text: "Update note"
26+
= button_to "Delete", note_path(note), method: :delete, class: "note-delete-button", form: { data: { turbo_confirm: "Delete this note?" } }

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
resources :activities, only: [:index] do
3636
post :mark_all_read, on: :collection
3737
end
38-
resources :notes, only: [:create, :update]
38+
resources :notes, only: [:create, :update, :destroy]
3939

4040
# Authentication
4141
resource :session, only: [:new, :create, :destroy]

0 commit comments

Comments
 (0)