diff --git a/app/models/proposal.rb b/app/models/proposal.rb index dfc81e2ea..c8b7cb0dc 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -74,6 +74,7 @@ class Proposal < ApplicationRecord 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 :selected, -> { where(selected: true) } scope :not_supported_by_user, ->(user) { where.not(id: user.find_voted_items(votable_type: "Proposal").compact.map(&:id)) } scope :published, -> { where.not(published_at: nil) } scope :draft, -> { where(published_at: nil) } diff --git a/db/migrate/20190410132842_add_selected_to_proposal.rb b/db/migrate/20190410132842_add_selected_to_proposal.rb new file mode 100644 index 000000000..a97d6619c --- /dev/null +++ b/db/migrate/20190410132842_add_selected_to_proposal.rb @@ -0,0 +1,5 @@ +class AddSelectedToProposal < ActiveRecord::Migration + def change + add_column :proposals, :selected, :bool, default: false, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index b9bfc50a7..9821ab24c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1194,6 +1194,7 @@ ActiveRecord::Schema.define(version: 20190429125842) do t.text "retired_explanation" t.integer "community_id" t.datetime "published_at" + t.boolean "selected", default: false t.index ["author_id", "hidden_at"], name: "index_proposals_on_author_id_and_hidden_at", using: :btree t.index ["author_id"], name: "index_proposals_on_author_id", using: :btree t.index ["cached_votes_up"], name: "index_proposals_on_cached_votes_up", using: :btree diff --git a/spec/factories/proposals.rb b/spec/factories/proposals.rb index 0821eb168..7cbd44acb 100644 --- a/spec/factories/proposals.rb +++ b/spec/factories/proposals.rb @@ -33,6 +33,10 @@ FactoryBot.define do created_at { 25.months.ago } end + trait :selected do + selected true + end + trait :with_hot_score do before(:save) { |d| d.calculate_hot_score } end diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index e9d316350..51a8128ac 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -866,6 +866,23 @@ describe Proposal do end end + describe "selected" do + let!(:unselected_proposal) { create(:proposal) } + let!(:selected_proposal) { create(:proposal, :selected) } + + it "selected? is true" do + expect(unselected_proposal.selected?).to be false + expect(selected_proposal.selected?).to be true + end + + it "scope selected" do + selected = Proposal.selected + + expect(selected.size).to be 1 + expect(selected.first).to eq selected_proposal + end + end + describe "public_for_api scope" do it "returns proposals" do proposal = create(:proposal)