Sha256: e68cfab6e501a392aa3fd0e323a84de20b4ce2721c54a0ef2c3eadedaea094f1

Contents?: true

Size: 1.39 KB

Versions: 16

Compression:

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
decidim-comments-0.23.6 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.23.5 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.23.4 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.23.3 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.23.2 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.23.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.23.1.rc1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.23.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.22.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.21.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.20.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.20.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.19.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.18.1 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.19.0 app/commands/decidim/comments/vote_comment.rb
decidim-comments-0.18.0 app/commands/decidim/comments/vote_comment.rb