From 63317714c360bca132a6ded25cb4bffe1f7c26e6 Mon Sep 17 00:00:00 2001 From: Angel Perez Date: Fri, 15 Dec 2017 11:12:13 -0400 Subject: [PATCH] Proposal recommendations won't include archived or already supported proposals --- app/models/proposal.rb | 13 ++++++++----- spec/models/proposal_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 7b914ce25..6623e507e 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -28,8 +28,8 @@ class Proposal < ActiveRecord::Base belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' belongs_to :geozone - has_many :comments, as: :commentable - has_many :proposal_notifications + has_many :comments, as: :commentable, dependent: :destroy + has_many :proposal_notifications, dependent: :destroy validates :title, presence: true validates :question, presence: true @@ -41,7 +41,7 @@ class Proposal < ActiveRecord::Base validates :description, length: { maximum: Proposal.description_max_length } validates :question, length: { in: 10..Proposal.question_max_length } validates :responsible_name, length: { in: 6..Proposal.responsible_name_max_length } - validates :retired_reason, inclusion: {in: RETIRE_OPTIONS, allow_nil: true} + validates :retired_reason, inclusion: { in: RETIRE_OPTIONS, allow_nil: true } validates :terms_of_service, acceptance: { allow_nil: false }, on: :create @@ -51,8 +51,8 @@ class Proposal < ActiveRecord::Base before_save :calculate_hot_score, :calculate_confidence_score - scope :for_render, -> { includes(:tags) } - scope :sort_by_hot_score, -> { reorder(hot_score: :desc) } + scope :for_render, -> { includes(:tags) } + scope :sort_by_hot_score, -> { reorder(hot_score: :desc) } scope :sort_by_confidence_score, -> { reorder(confidence_score: :desc) } scope :sort_by_created_at, -> { reorder(created_at: :desc) } scope :sort_by_most_commented, -> { reorder(comments_count: :desc) } @@ -69,12 +69,15 @@ class Proposal < ActiveRecord::Base scope :successful, -> { where("cached_votes_up >= ?", Proposal.votes_needed_for_success) } scope :unsuccessful, -> { where("cached_votes_up < ?", Proposal.votes_needed_for_success) } scope :public_for_api, -> { all } + scope :not_supported_by_user, ->(user) { where.not(id: user.find_voted_items(votable_type: "Proposal").compact.map(&:id)) } def self.recommendations(user) tagged_with(user.interests, any: true) .where("author_id != ?", user.id) .unsuccessful .not_followed_by_user(user) + .not_archived + .not_supported_by_user(user) end def self.not_followed_by_user(user) diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index f1da4c673..6d35d54ff 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -952,6 +952,29 @@ describe Proposal do expect(result).to eq [proposal2] end + it "should not return archived proposals" do + proposal1 = create(:proposal, cached_votes_up: 5, tag_list: "Sport") + proposal2 = create(:proposal, cached_votes_up: 5, tag_list: "Sport") + archived_proposal = create(:proposal, :archived) + create(:follow, followable: proposal1, user: user) + + result = Proposal.recommendations(user) + expect(result.size).to eq(1) + expect(result).to eq([proposal2]) + end + + it "should not return already supported proposals" do + proposal1 = create(:proposal, cached_votes_up: 5, tag_list: "Health") + proposal2 = create(:proposal, cached_votes_up: 5, tag_list: "Health") + proposal3 = create(:proposal, cached_votes_up: 5, tag_list: "Health") + create(:vote, votable: proposal1, voter: user) + create(:follow, followable: proposal2, user: user) + + result = Proposal.recommendations(user) + expect(result.size).to eq(1) + expect(result).to eq([proposal3]) + end + end end