Sha256: 8200e48011c2ba0a695be317633e98529a3fb53760189528c922fbce5f83f8a5

Contents?: true

Size: 1.12 KB

Versions: 15

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true
module Decidim
  module Comments
    # A command with all the business logic to upvote a comment
    class VoteComment < Rectify::Command
      # 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.
      def initialize(comment, author, options = { weight: 1 })
        @comment = comment
        @author = author
        @weight = options[:weight]
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid.
      # - :invalid if the vote wasn't create
      #
      # Returns nothing.
      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
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
decidim-comments-0.1.0 app/commands/decidim/comments/vote_comment.rb
decidim-0.1.0 decidim-comments/app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.0.8.1 app/commands/decidim/comments/vote_comment.rb
decidim-0.0.8.1 decidim-comments/app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.0.7 app/commands/decidim/comments/vote_comment.rb
decidim-0.0.7 decidim-comments/app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.0.6 app/commands/decidim/comments/vote_comment.rb
decidim-0.0.6 decidim-comments/app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.0.5 app/commands/decidim/comments/vote_comment.rb
decidim-0.0.5 decidim-comments/app/commands/decidim/comments/vote_comment.rb
decidim-0.0.4 decidim-comments/app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.0.3 app/commands/decidim/comments/vote_comment.rb
decidim-0.0.3 decidim-comments/app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.0.2 app/commands/decidim/comments/vote_comment.rb
decidim-0.0.2 decidim-comments/app/commands/decidim/comments/vote_comment.rb