Class: Decidim::Comments::VoteComment
- Inherits:
-
Rectify::Command
- Object
- Rectify::Command
- Decidim::Comments::VoteComment
- Defined in:
- decidim-comments/app/commands/decidim/comments/vote_comment.rb
Overview
A command with all the business logic to upvote a comment
Instance Method Summary collapse
-
#call ⇒ Object
Executes the command.
-
#initialize(comment, author, options = { weight: 1 }) ⇒ VoteComment
constructor
Public: Initializes the command.
Constructor Details
#initialize(comment, author, options = { weight: 1 }) ⇒ VoteComment
Public: Initializes the command.
comment - A comment author - A user options - An optional hash of options (default: { weight: 1 })
* weight: The vote's weight. Valid values 1 and -1.
13 14 15 16 17 |
# File 'decidim-comments/app/commands/decidim/comments/vote_comment.rb', line 13 def initialize(comment, , = { weight: 1 }) @comment = comment = @weight = [:weight] end |
Instance Method Details
#call ⇒ Object
Executes the command. Broadcasts these events:
-
:ok when everything is valid.
-
:invalid if the vote wasn’t create
Returns nothing.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'decidim-comments/app/commands/decidim/comments/vote_comment.rb', line 25 def call if @weight == 1 @comment.up_votes.create!(author: ) elsif @weight == -1 @comment.down_votes.create!(author: ) else return broadcast(:invalid) end broadcast(:ok, @comment) rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique broadcast(:invalid) end |