Sha256: 967fa4e53f9e2de5ec871ba4c1436ad98267c502a30acc9a55d435e41faec913

Contents?: true

Size: 946 Bytes

Versions: 8

Compression:

Stored size: 946 Bytes

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
      belongs_to :comment, foreign_key: "decidim_comment_id", class_name: Comment
      belongs_to :author, foreign_key: "decidim_author_id", class_name: Decidim::User

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

      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

8 entries across 8 versions & 2 rubygems

Version Path
decidim-comments-0.1.0 app/models/decidim/comments/comment_vote.rb
decidim-0.1.0 decidim-comments/app/models/decidim/comments/comment_vote.rb
decidim-comments-0.0.8.1 app/models/decidim/comments/comment_vote.rb
decidim-0.0.8.1 decidim-comments/app/models/decidim/comments/comment_vote.rb
decidim-comments-0.0.7 app/models/decidim/comments/comment_vote.rb
decidim-0.0.7 decidim-comments/app/models/decidim/comments/comment_vote.rb
decidim-comments-0.0.6 app/models/decidim/comments/comment_vote.rb
decidim-0.0.6 decidim-comments/app/models/decidim/comments/comment_vote.rb