Sha256: 94336ca6180b708b449db49b6aa749e61e30b0c01535baf004ba5b1c28fe844c

Contents?: true

Size: 1.03 KB

Versions: 39

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Decidim
  module Comments
    # A comment can include user votes. A user should be able to upVote, votes with
    # weight 1 and downVote, votes with weight -1.
    class CommentVote < ApplicationRecord
      include Decidim::DataPortability

      belongs_to :comment, foreign_key: "decidim_comment_id", class_name: "Comment"
      belongs_to :author, foreign_key: "decidim_author_id", foreign_type: "decidim_author_type", polymorphic: true

      validates :comment, uniqueness: { scope: :author }
      validates :weight, inclusion: { in: [-1, 1] }
      validate :author_and_comment_same_organization

      def self.export_serializer
        Decidim::Comments::CommentVoteSerializer
      end

      private

      # Private: check if the comment and the author have the same organization
      def author_and_comment_same_organization
        return unless author.present? && comment.present?

        errors.add(:comment, :invalid) unless author.organization == comment.organization
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
decidim-comments-0.26.10 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.9 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.8 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.7 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.5 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.4 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.3 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.2 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.1 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.0 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.0.rc2 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.26.0.rc1 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.25.2 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.25.1 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.25.0 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.25.0.rc4 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.25.0.rc3 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.25.0.rc2 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.25.0.rc1 app/models/decidim/comments/comment_vote.rb
decidim-comments-0.24.3 app/models/decidim/comments/comment_vote.rb