forked from hackorum-dev/hackorum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes_controller.rb
More file actions
116 lines (99 loc) · 3.17 KB
/
notes_controller.rb
File metadata and controls
116 lines (99 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
75
76
77
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# frozen_string_literal: true
class NotesController < ApplicationController
before_action :require_authentication
before_action :set_note, only: [:update, :destroy]
def create
topic = Topic.find(note_params[:topic_id])
message = resolve_message(topic)
note = NoteBuilder.new(author: current_user).create!(
topic:,
message:,
body: note_params[:body],
tags: parsed_tags,
mention_names: parsed_mentions
)
redirect_to topic_path(topic, anchor: note_anchor(note)), notice: "Note added"
rescue NoteBuilder::Error, ActiveRecord::RecordInvalid => e
flash[:alert] = e.message
flash[:note_error] = {
body: note_params[:body],
message_id: note_params[:message_id].presence,
topic_id: note_params[:topic_id],
tags_input: note_params[:tags_input],
mentions_input: note_params[:mentions_input],
error: e.message
}
redirect_back fallback_location: topic_path(topic)
end
def update
return if performed?
NoteBuilder.new(author: current_user).update!(
note: @note,
body: note_params[:body],
tags: parsed_tags,
mention_names: parsed_mentions
)
redirect_to topic_path(@note.topic, anchor: note_anchor(@note)), notice: "Note updated"
rescue NoteBuilder::Error, ActiveRecord::RecordInvalid => e
flash[:alert] = e.message
flash[:note_error] = {
body: note_params[:body],
message_id: note_params[:message_id].presence,
topic_id: note_params[:topic_id],
note_id: @note.id,
tags_input: note_params[:tags_input],
mentions_input: note_params[:mentions_input],
error: e.message
}
redirect_back fallback_location: topic_path(@note.topic)
end
def destroy
return if performed?
@note.transaction do
@note.update!(deleted_at: Time.current)
@note.note_mentions.delete_all
@note.note_tags.delete_all
@note.activities.update_all(hidden: true)
end
redirect_back fallback_location: topic_path(@note.topic), notice: "Note deleted"
end
private
def set_note
@note = Note.find(params[:id])
if @note.deleted_at?
redirect_back fallback_location: topic_path(@note.topic), alert: "Note has been deleted"
return
end
unless @note.author_id == current_user.id
redirect_back fallback_location: topic_path(@note.topic), alert: "You can edit or delete your own notes"
return
end
end
def note_params
params.require(:note).permit(:body, :topic_id, :message_id, :tags_input, :mentions_input)
end
def resolve_message(topic)
return nil if note_params[:message_id].blank?
topic.messages.find(note_params[:message_id])
end
def note_anchor(note)
if note.message_id
view_context.message_dom_id(note.message)
else
"thread-notes"
end
end
def parsed_tags
split_tokens(note_params[:tags_input]) { |token| token.delete_prefix("#") }
end
def parsed_mentions
split_tokens(note_params[:mentions_input]) { |token| token.delete_prefix("@") }
end
def split_tokens(raw, &block)
Array(raw.to_s.split(/[,\s]+/))
.map { |token| token.strip }
.reject(&:blank?)
.map(&block)
.reject(&:blank?)
end
end