docbrown/app/models/rating_vote.rb
Ben Halpern 0817e2ac9e
Create implicit experience level rating vote upon creating of readinglist reaction (#6522) [deploy]
* Do not render or link to empty rss feeds

* s

* Move calculation to worker

* remove xit test

* Also allow implicit rating votes in comments

* Fix alignment issue

* Remove comment logic
2020-03-10 10:59:49 -04:00

25 lines
775 B
Ruby

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