docbrown/app/models/rating_vote.rb
rhymes 1a147a3176
[deploy] Add missing foreign keys to models related to users (#10018)
* Add missing foreign keys to models related to users

* Fix specs

* Change FK for page_views and rating_votes to nullify

* Improve rating vote validation and fix Users::Delete specs

* Revert "Spec Fix:Nullify Rating Votes when Deleting Users (#10071)"

This reverts commit 12dfe4ab3b.
2020-08-31 15:39:57 +02:00

29 lines
858 B
Ruby

class RatingVote < ApplicationRecord
belongs_to :article
belongs_to :user, optional: true
validates :context, inclusion: { in: %w[explicit readinglist_reaction comment] }
validates :group, inclusion: { in: %w[experience_level] }
validates :rating, numericality: { greater_than: 0.0, less_than_or_equal_to: 10.0 }
validates :user_id, presence: true, on: :create
validates :user_id, uniqueness: { scope: %i[article_id context] }
validate :permissions
after_create_commit :assign_article_rating
counter_culture :article
counter_culture :user
private
def assign_article_rating
RatingVotes::AssignRatingWorker.perform_async(article_id)
end
def permissions
return unless context == "explicit" && !user&.trusted && user_id != article&.user_id
errors.add(:user_id, "is not permitted to take this action.")
end
end