diff --git a/app/controllers/admin/budgets_controller.rb b/app/controllers/admin/budgets_controller.rb index 0b373c0bd..b37351028 100644 --- a/app/controllers/admin/budgets_controller.rb +++ b/app/controllers/admin/budgets_controller.rb @@ -14,10 +14,15 @@ class Admin::BudgetsController < Admin::BaseController @budget = Budget.includes(groups: :headings).find(params[:id]) end - def new - end + def new; end - def edit + def edit; end + + def calculate_winners + return unless @budget.balloting_process? + @budget.headings.each { |heading| Budget::Result.new(@budget, heading).calculate_winners } + redirect_to admin_budget_budget_investments_path(budget_id: @budget.id, filter: 'winners'), + notice: I18n.t("admin.budgets.winners.calculated") end def update diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 1aba9dd7c..9d779299d 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -44,7 +44,7 @@ module Abilities can [:read, :update, :valuate, :destroy, :summary], SpendingProposal - can [:index, :read, :new, :create, :update, :destroy], Budget + can [:index, :read, :new, :create, :update, :destroy, :calculate_winners], Budget can [:read, :create, :update, :destroy], Budget::Group can [:read, :create, :update, :destroy], Budget::Heading can [:hide, :update, :toggle_selection], Budget::Investment diff --git a/app/models/budget.rb b/app/models/budget.rb index a5932921c..bc7231f29 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -67,8 +67,12 @@ class Budget < ActiveRecord::Base phase == "finished" end + def balloting_process? + balloting? || reviewing_ballots? + end + def balloting_or_later? - balloting? || reviewing_ballots? || finished? + balloting_process? || finished? end def on_hold? diff --git a/app/models/budget/result.rb b/app/models/budget/result.rb index f29bc72cc..f835bce0c 100644 --- a/app/models/budget/result.rb +++ b/app/models/budget/result.rb @@ -12,11 +12,10 @@ class Budget reset_winners investments.each do |investment| @current_investment = investment - if inside_budget? - set_winner - end + set_winner if inside_budget? end end + handle_asynchronously :calculate_winners def investments heading.investments.selected.sort_by_ballots @@ -52,4 +51,4 @@ class Budget end end -end \ No newline at end of file +end diff --git a/config/routes.rb b/config/routes.rb index 5c5284a91..6d0f2959c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -193,6 +193,10 @@ Rails.application.routes.draw do end resources :budgets do + member do + put :calculate_winners + end + resources :budget_groups do resources :budget_headings do end