Class: Decidim::Comments::VoteComment

Inherits:
Rectify::Command
  • Object
show all
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

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, author, options = { weight: 1 })
  @comment = comment
  @author = author
  @weight = options[:weight]
end

Instance Method Details

#callObject

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: @author)
  elsif @weight == -1
    @comment.down_votes.create!(author: @author)
  else
    return broadcast(:invalid)
  end
  broadcast(:ok, @comment)
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  broadcast(:invalid)
end