-
Notifications
You must be signed in to change notification settings - Fork 2
/
destroy_paragraph.rb
33 lines (29 loc) · 1003 Bytes
/
destroy_paragraph.rb
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
# frozen_string_literal: true
module Decidim
module EnhancedTextwork
# A command with all the business logic when a user destroys a draft paragraph.
class DestroyParagraph < Rectify::Command
# Public: Initializes the command.
#
# paragraph - The paragraph to destroy.
# current_user - The current user.
def initialize(paragraph, current_user)
@paragraph = paragraph
@current_user = current_user
end
# Executes the command. Broadcasts these events:
#
# - :ok when everything is valid and the paragraph is deleted.
# - :invalid if the paragraph is not a draft.
# - :invalid if the paragraph's author is not the current user.
#
# Returns nothing.
def call
return broadcast(:invalid) unless @paragraph.draft?
return broadcast(:invalid) unless @paragraph.authored_by?(@current_user)
@paragraph.destroy!
broadcast(:ok, @paragraph)
end
end
end
end