Add selected attribute to proposals

This commit is contained in:
Julian Herrero
2019-04-11 16:41:06 +02:00
parent 24bab90cff
commit 9948804e21
5 changed files with 28 additions and 0 deletions

View File

@@ -74,6 +74,7 @@ class Proposal < ApplicationRecord
scope :successful, -> { where("cached_votes_up >= ?", Proposal.votes_needed_for_success) } scope :successful, -> { where("cached_votes_up >= ?", Proposal.votes_needed_for_success) }
scope :unsuccessful, -> { 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 :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 :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 :published, -> { where.not(published_at: nil) }
scope :draft, -> { where(published_at: nil) } scope :draft, -> { where(published_at: nil) }

View File

@@ -0,0 +1,5 @@
class AddSelectedToProposal < ActiveRecord::Migration
def change
add_column :proposals, :selected, :bool, default: false, index: true
end
end

View File

@@ -1194,6 +1194,7 @@ ActiveRecord::Schema.define(version: 20190429125842) do
t.text "retired_explanation" t.text "retired_explanation"
t.integer "community_id" t.integer "community_id"
t.datetime "published_at" 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", "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 ["author_id"], name: "index_proposals_on_author_id", using: :btree
t.index ["cached_votes_up"], name: "index_proposals_on_cached_votes_up", using: :btree t.index ["cached_votes_up"], name: "index_proposals_on_cached_votes_up", using: :btree

View File

@@ -33,6 +33,10 @@ FactoryBot.define do
created_at { 25.months.ago } created_at { 25.months.ago }
end end
trait :selected do
selected true
end
trait :with_hot_score do trait :with_hot_score do
before(:save) { |d| d.calculate_hot_score } before(:save) { |d| d.calculate_hot_score }
end end

View File

@@ -866,6 +866,23 @@ describe Proposal do
end end
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 describe "public_for_api scope" do
it "returns proposals" do it "returns proposals" do
proposal = create(:proposal) proposal = create(:proposal)