Merge pull request #1678 from consul/feature/1601#budget_winner_investments_button

Add button to Calculate Budget Winner investments
This commit is contained in:
Raimond Garcia
2017-06-26 17:41:08 +02:00
committed by GitHub
11 changed files with 71 additions and 10 deletions

View File

@@ -14,10 +14,15 @@ class Admin::BudgetsController < Admin::BaseController
@budget = Budget.includes(groups: :headings).find(params[:id]) @budget = Budget.includes(groups: :headings).find(params[:id])
end end
def new def new; end
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 end
def update def update

View File

@@ -44,7 +44,7 @@ module Abilities
can [:read, :update, :valuate, :destroy, :summary], SpendingProposal 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::Group
can [:read, :create, :update, :destroy], Budget::Heading can [:read, :create, :update, :destroy], Budget::Heading
can [:hide, :update, :toggle_selection], Budget::Investment can [:hide, :update, :toggle_selection], Budget::Investment

View File

@@ -67,8 +67,12 @@ class Budget < ActiveRecord::Base
phase == "finished" phase == "finished"
end end
def balloting_process?
balloting? || reviewing_ballots?
end
def balloting_or_later? def balloting_or_later?
balloting? || reviewing_ballots? || finished? balloting_process? || finished?
end end
def on_hold? def on_hold?

View File

@@ -12,11 +12,10 @@ class Budget
reset_winners reset_winners
investments.each do |investment| investments.each do |investment|
@current_investment = investment @current_investment = investment
if inside_budget? set_winner if inside_budget?
set_winner
end
end end
end end
handle_asynchronously :calculate_winners
def investments def investments
heading.investments.selected.sort_by_ballots heading.investments.selected.sort_by_ballots

View File

@@ -16,5 +16,15 @@
<%= f.select :currency_symbol, budget_currency_symbol_select_options %> <%= f.select :currency_symbol, budget_currency_symbol_select_options %>
</div> </div>
</div> </div>
<div class="margin-top">
<%= f.submit nil, class: "button success" %> <%= f.submit nil, class: "button success" %>
<% if @budget.balloting_process? %>
<div class="float-right">
<%= link_to t("admin.budgets.winners.calculate"),
calculate_winners_admin_budget_path(@budget),
method: :put,
class: "button hollow" %>
</div>
<% end %>
</div>
<% end %> <% end %>

View File

@@ -101,6 +101,9 @@ en:
no_heading: This group has no assigned heading. no_heading: This group has no assigned heading.
table_heading: Heading table_heading: Heading
table_amount: Amount table_amount: Amount
winners:
calculate: Calculate Winner Investments
calculated: Winners being calculated, it may take a minute.
budget_investments: budget_investments:
index: index:
heading_filter_all: All headings heading_filter_all: All headings

View File

@@ -101,6 +101,9 @@ es:
no_heading: Este grupo no tiene ninguna partida asignada. no_heading: Este grupo no tiene ninguna partida asignada.
table_heading: Partida table_heading: Partida
table_amount: Cantidad table_amount: Cantidad
winners:
calculate: Calcular propuestas ganadoras
calculated: Calculando ganadoras, puede tardar un minuto.
budget_investments: budget_investments:
index: index:
heading_filter_all: Todas las partidas heading_filter_all: Todas las partidas

View File

@@ -101,6 +101,9 @@ fr:
no_heading: Ce groupe n'a pas de rubrique assignée. no_heading: Ce groupe n'a pas de rubrique assignée.
table_heading: Rubrique table_heading: Rubrique
table_amount: Montant table_amount: Montant
winners:
calculate: Calculate Winner Investments
calculated: Winners being calculated, it may take a minute.
budget_investments: budget_investments:
index: index:
heading_filter_all: Toutes les rubriques heading_filter_all: Toutes les rubriques

View File

@@ -102,6 +102,9 @@ nl:
no_heading: This group has no assigned heading. no_heading: This group has no assigned heading.
table_heading: Heading table_heading: Heading
table_amount: Amount table_amount: Amount
winners:
calculate: Calculate Winner Investments
calculated: Winners being calculated, it may take a minute.
budget_investments: budget_investments:
index: index:
heading_filter_all: All headings heading_filter_all: All headings

View File

@@ -193,6 +193,10 @@ Rails.application.routes.draw do
end end
resources :budgets do resources :budgets do
member do
put :calculate_winners
end
resources :budget_groups do resources :budget_groups do
resources :budget_headings do resources :budget_headings do
end end

View File

@@ -109,6 +109,33 @@ feature 'Admin budgets' do
end end
context "Calculate Budget's Winner Investments" do
scenario 'For a Budget in reviewing balloting' do
budget = create(:budget, phase: 'reviewing_ballots')
group = create(:budget_group, budget: budget)
heading = create(:budget_heading, group: group, price: 4)
unselected_investment = create(:budget_investment, :unselected, heading: heading, price: 1, ballot_lines_count: 3)
winner_investment = create(:budget_investment, :winner, heading: heading, price: 3, ballot_lines_count: 2)
selected_investment = create(:budget_investment, :selected, heading: heading, price: 2, ballot_lines_count: 1)
visit edit_admin_budget_path(budget)
click_link 'Calculate Winner Investments'
expect(page).to have_content 'Winners being calculated, it may take a minute.'
expect(page).to have_content winner_investment.title
expect(page).not_to have_content unselected_investment.title
expect(page).not_to have_content selected_investment.title
end
scenario 'For a finished Budget' do
budget = create(:budget, phase: 'finished')
visit edit_admin_budget_path(budget)
expect(page).not_to have_content 'Calculate Winner Investments'
end
end
context 'Manage groups and headings' do context 'Manage groups and headings' do
scenario 'Create group', :js do scenario 'Create group', :js do