diff --git a/app/models/budget/ballot/line.rb b/app/models/budget/ballot/line.rb index 85434ad2d..730398edc 100644 --- a/app/models/budget/ballot/line.rb +++ b/app/models/budget/ballot/line.rb @@ -11,7 +11,7 @@ class Budget validate :insufficient_funds #needed? validate :different_geozone, :if => :district_proposal? - validate :unfeasible + validate :unselected before_validation :set_denormalized_ids @@ -23,8 +23,8 @@ class Budget errors.add(:heading, "different heading assigned") if (ballot.heading.present? && investment.heading != ballot.heading) end - def unfeasible - errors.add(:unfeasible, "unfeasible investment") unless investment.feasible? + def unselected + errors.add(:unselected, "unselected investment") unless investment.selected? end def heading_proposal? diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 23586ec3a..240509609 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -44,6 +44,7 @@ class Budget scope :not_unfeasible, -> { where.not(feasibility: "unfeasible") } scope :undecided, -> { where(feasibility: "undecided") } scope :with_supports, -> { where('cached_votes_up > 0') } + scope :selected, -> { where(selected: true) } scope :by_group, -> (group_id) { where(group_id: group_id) } scope :by_heading, -> (heading_id) { where(heading_id: heading_id) } @@ -205,8 +206,13 @@ class Budget budget.formatted_amount(price) end - def self.apply_filters_and_search(params) - investments = params[:unfeasible].present? ? unfeasible : not_unfeasible + def self.apply_filters_and_search(budget, params) + investments = all + if budget.balloting? + investments = investments.selected + else + investments = params[:unfeasible].present? ? investments.unfeasible : investments.not_unfeasible + end investments = investments.by_heading(params[:heading_id]) if params[:heading_id].present? investments = investments.search(params[:search]) if params[:search].present? investments diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 9563e4e83..2d8ccb55a 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -362,7 +362,7 @@ tags = Faker::Lorem.words(10) external_url: Faker::Internet.url, description: "

#{Faker::Lorem.paragraphs.join('

')}

", created_at: rand((Time.now - 1.week) .. Time.now), - feasibility: %w{undecided feasible unfeasible}.sample, + feasibility: %w{undecided unfeasible feasible feasible feasible feasible}.sample, unfeasibility_explanation: "

#{Faker::Lorem.paragraphs.join('

')}

", valuation_finished: [false, true].sample, tag_list: tags.sample(3).join(','), @@ -371,6 +371,11 @@ tags = Faker::Lorem.words(10) puts " #{investment.title}" end +puts "Selecting Investments" +Budget.balloting.reorder("RANDOM()").limit(3).each do |budget| + budget.investments.feasible.reorder("RANDOM()").limit(10).update_all(selected: true) +end + puts "Creating Valuation Assignments" (1..17).to_a.sample.times do diff --git a/db/migrate/20161221172447_add_selected_to_budget_investment.rb b/db/migrate/20161221172447_add_selected_to_budget_investment.rb new file mode 100644 index 000000000..de983aee4 --- /dev/null +++ b/db/migrate/20161221172447_add_selected_to_budget_investment.rb @@ -0,0 +1,5 @@ +class AddSelectedToBudgetInvestment < ActiveRecord::Migration + def change + add_column :budget_investments, :selected, :bool, default: false, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 3668e1ebf..f891b40ff 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161102133838) do +ActiveRecord::Schema.define(version: 20161221172447) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -141,6 +141,7 @@ ActiveRecord::Schema.define(version: 20161102133838) do t.string "responsible_name" t.integer "budget_id" t.integer "group_id" + t.boolean "selected", default: false end add_index "budget_investments", ["administrator_id"], name: "index_budget_investments_on_administrator_id", using: :btree diff --git a/spec/factories.rb b/spec/factories.rb index 5126a8643..4a1dc8e9f 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -248,6 +248,10 @@ FactoryGirl.define do unfeasibility_explanation "set to unfeasible on creation" end + trait :selected do + selected true + end + trait :finished do valuation_finished true end