Added new related contents score method

This commit is contained in:
María Checa
2017-12-19 13:14:12 +01:00
parent 853e655e0c
commit 7d8b5838b9
4 changed files with 12 additions and 32 deletions

View File

@@ -17,22 +17,20 @@ class RelatedContentsController < ApplicationController
redirect_to @relationable
end
def flag
def score_positive
@related = RelatedContent.find_by(id: params[:id])
@related.increment!(:positive_score)
@related.opposite_related_content.increment!(:positive_score)
Flag.flag(current_user, @related)
Flag.flag(current_user, @related.opposite_related_content)
render template: 'relationable/_refresh_flag_actions'
render template: 'relationable/_refresh_score_actions'
end
def unflag
def score_negative
@related = RelatedContent.find_by(id: params[:id])
@related.increment!(:negative_score)
@related.opposite_related_content.increment!(:negative_score)
Flag.unflag(current_user, @related)
Flag.unflag(current_user, @related.opposite_related_content)
render template: 'relationable/_refresh_flag_actions'
render template: 'relationable/_refresh_score_actions'
end
private

View File

@@ -12,12 +12,4 @@ module Relationable
def relationed_contents
related_contents.not_hidden.map { |related_content| related_content.child_relationable }
end
def report_related_content(relationable)
related_content = related_contents.find_by(child_relationable: relationable)
if related_content.present?
related_content.increment!(:flags_count)
related_content.opposite_related_content.increment!(:flags_count)
end
end
end

View File

@@ -1,7 +1,5 @@
class RelatedContent < ActiveRecord::Base
include Flaggable
RELATED_CONTENTS_REPORT_THRESHOLD = Setting['related_contents_report_threshold'].to_i
RELATED_CONTENTS_REPORT_THRESHOLD = Setting['related_contents_report_threshold'].to_f
RELATIONABLE_MODELS = %w{proposals debates}.freeze
belongs_to :parent_relationable, polymorphic: true, touch: true
@@ -17,11 +15,7 @@ class RelatedContent < ActiveRecord::Base
after_create :create_opposite_related_content, unless: proc { opposite_related_content.present? }
after_destroy :destroy_opposite_related_content, if: proc { opposite_related_content.present? }
scope :not_hidden, -> { where('flags_count <= ?', RELATED_CONTENTS_REPORT_THRESHOLD) }
def hidden_by_reports?
flags_count > RELATED_CONTENTS_REPORT_THRESHOLD
end
scope :not_hidden, -> { where('positive_score - negative_score / LEAST(nullif(positive_score + negative_score, 0), 1) >= ?', RELATED_CONTENTS_REPORT_THRESHOLD) }
private
@@ -29,8 +23,4 @@ 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 destroy_opposite_related_content
opposite_related_content.destroy
end
end

View File

@@ -464,8 +464,8 @@ Rails.application.routes.draw do
resources :related_contents, only: [:create] do
member do
put :flag
put :unflag
put :score_positive
put :score_negative
end
end