Add correct user to RelatedContentScore on creation

This commit is contained in:
Bertocq
2017-12-20 02:08:17 +01:00
parent 2e0dd9caea
commit d544d56722
2 changed files with 13 additions and 4 deletions

View File

@@ -18,13 +18,13 @@ class RelatedContent < ActiveRecord::Base
validates :parent_relationable_id, uniqueness: { scope: [:parent_relationable_type, :child_relationable_id, :child_relationable_type] }
after_create :create_opposite_related_content, unless: proc { opposite_related_content.present? }
after_create :create_author_score
scope :not_hidden, -> { where('positive_score - negative_score / LEAST(nullif(positive_score + negative_score, 0), 1) >= ?', RELATED_CONTENT_SCORE_THRESHOLD) }
scope :not_hidden, -> { where(hidden_at: nil) }
def score(value)
RelatedContentsScore.create(user_id: current_user, related_content_id: self, score: value)
RelatedContentsScore.create(user_id: current_user, related_content_id: opposite_related_content, score: value)
def score(value, user)
score_with_opposite(value, user)
end
private
@@ -33,4 +33,13 @@ class RelatedContent < ActiveRecord::Base
related_content = RelatedContent.create!(opposite_related_content: self, parent_relationable: child_relationable, child_relationable: parent_relationable)
self.opposite_related_content = related_content
end
def create_author_score
score(1, author)
end
def score_with_opposite(value, user)
RelatedContentsScore.create(user: user, related_content: self, score: value)
RelatedContentsScore.create(user: user, related_content: opposite_related_content, score: value)
end
end