Refactor Related Content Controller score methods
This commit is contained in:
@@ -18,23 +18,21 @@ class RelatedContentsController < ApplicationController
|
||||
end
|
||||
|
||||
def score_positive
|
||||
@related = RelatedContent.find_by(id: params[:id])
|
||||
@related.increment!(:positive_score)
|
||||
@related.opposite_related_content.increment!(:positive_score)
|
||||
|
||||
render template: 'relationable/_refresh_score_actions'
|
||||
score(1)
|
||||
end
|
||||
|
||||
def score_negative
|
||||
@related = RelatedContent.find_by(id: params[:id])
|
||||
@related.increment!(:negative_score)
|
||||
@related.opposite_related_content.increment!(:negative_score)
|
||||
|
||||
render template: 'relationable/_refresh_score_actions'
|
||||
score(-1)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def score(value)
|
||||
RelatedContent.find_by(id: params[:id]).score(value)
|
||||
|
||||
render template: 'relationable/_refresh_score_actions'
|
||||
end
|
||||
|
||||
def valid_url?
|
||||
params[:url].match(VALID_URL)
|
||||
end
|
||||
|
||||
@@ -17,6 +17,11 @@ class RelatedContent < ActiveRecord::Base
|
||||
|
||||
scope :not_hidden, -> { where('positive_score - negative_score / LEAST(nullif(positive_score + negative_score, 0), 1) >= ?', RELATED_CONTENT_SCORE_THRESHOLD) }
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_opposite_related_content
|
||||
|
||||
@@ -83,8 +83,19 @@ shared_examples "relationable" do |relationable_model_name|
|
||||
expect(page).to_not have_css("#score-positive-related-#{related_content.opposite_related_content.id}")
|
||||
end
|
||||
|
||||
expect(related_content.reload.positive_score).to eq(2)
|
||||
expect(related_content.opposite_related_content.reload.positive_score).to eq(2)
|
||||
expect(
|
||||
RelatedContentsReport.where(
|
||||
user_id: current_user.id,
|
||||
related_content_id: related_content.id
|
||||
).first.score
|
||||
).to eq(true)
|
||||
expect(
|
||||
RelatedContentsReport.where(
|
||||
user_id: current_user.id,
|
||||
related_content_id: related_content.opposite_related_content.id
|
||||
).first.score
|
||||
).to eq(true)
|
||||
|
||||
end
|
||||
|
||||
scenario 'related content can be scored negatively', :js do
|
||||
|
||||
Reference in New Issue
Block a user