Skip to content

Commit

Permalink
Merge pull request #1066 from AyuntamientoMadrid/upstream
Browse files Browse the repository at this point in the history
Upstream
  • Loading branch information
bertocq authored Dec 18, 2017
2 parents 54364d7 + 15a6a09 commit affea70
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
11 changes: 7 additions & 4 deletions app/models/proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Proposal < ActiveRecord::Base

belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
belongs_to :geozone
has_many :proposal_notifications
has_many :comments, as: :commentable
has_many :comments, as: :commentable, dependent: :destroy
has_many :proposal_notifications, dependent: :destroy

validates :title, presence: true
validates :summary, presence: true
Expand All @@ -53,8 +53,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) }
Expand All @@ -73,12 +73,15 @@ class Proposal < ActiveRecord::Base
scope :public_for_api, -> { where('proposals.proceeding IS NULL or proposals.proceeding = ?', 'Derechos Humanos') }
scope :proceedings, -> { where.not(proceeding: nil) }
scope :not_proceedings, -> { where(proceeding: nil) }
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)
Expand Down
23 changes: 23 additions & 0 deletions spec/models/proposal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,29 @@
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

0 comments on commit affea70

Please sign in to comment.