Sha256: 1489e669f7a462642b638df71a52e5caab281655a344dfc9dcbc15f298a7c6d5

Contents?: true

Size: 1.12 KB

Versions: 78

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

78 entries across 78 versions & 2 rubygems

Version Path
decidim-comments-0.17.2 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.17.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.16.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.17.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.16.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.15.2 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.15.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.15.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.14.4 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.14.3 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.14.2 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.14.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.13.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.12.2 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.13.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.12.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.13.0.pre1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.12.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.11.2 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.12.0.pre app/commands/decidim/comments/vote_comment.rb