diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index 9673cdc88..e223ffa56 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -236,6 +236,11 @@ a { border-bottom: 2px solid $brand; color: $brand; } + + &.bold { + font-weight: bold; + color: $brand; + } } &.no-margin-top { diff --git a/app/controllers/admin/budgets_controller.rb b/app/controllers/admin/budgets_controller.rb index b37351028..7caf4f35c 100644 --- a/app/controllers/admin/budgets_controller.rb +++ b/app/controllers/admin/budgets_controller.rb @@ -20,7 +20,7 @@ class Admin::BudgetsController < Admin::BaseController def calculate_winners return unless @budget.balloting_process? - @budget.headings.each { |heading| Budget::Result.new(@budget, heading).calculate_winners } + @budget.headings.each { |heading| Budget::Result.new(@budget, heading).delay.calculate_winners } redirect_to admin_budget_budget_investments_path(budget_id: @budget.id, filter: 'winners'), notice: I18n.t("admin.budgets.winners.calculated") end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 5fcf2e882..6a83b28a2 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -204,7 +204,7 @@ class Budget end def recalculate_heading_winners - Budget::Result.new(budget, heading).calculate_winners if incompatible_changed? && winner? && incompatible? + Budget::Result.new(budget, heading).calculate_winners if incompatible_changed? end def set_responsible_name diff --git a/app/models/budget/result.rb b/app/models/budget/result.rb index aa6c4c7ac..d034e50ef 100644 --- a/app/models/budget/result.rb +++ b/app/models/budget/result.rb @@ -15,7 +15,6 @@ class Budget set_winner if inside_budget? end end - handle_asynchronously :calculate_winners def investments heading.investments.selected.sort_by_ballots diff --git a/app/views/admin/budget_investments/edit.html.erb b/app/views/admin/budget_investments/edit.html.erb index 1e21ee706..c33039021 100644 --- a/app/views/admin/budget_investments/edit.html.erb +++ b/app/views/admin/budget_investments/edit.html.erb @@ -63,6 +63,7 @@
+ <% if @investment.incompatible? || @investment.winner? %>

<%= t("admin.budget_investments.edit.compatibility") %>

<%= f.label :incompatible do %> @@ -70,6 +71,7 @@ <%= t("admin.budget_investments.edit.mark_as_incompatible") %> <% end %>
+ <% end %>

<%= t("admin.budget_investments.edit.selection") %>

<%= f.label :selected do %> diff --git a/app/views/budgets/results/_results_table.html.erb b/app/views/budgets/results/_results_table.html.erb index 993060562..f391e8d50 100644 --- a/app/views/budgets/results/_results_table.html.erb +++ b/app/views/budgets/results/_results_table.html.erb @@ -1,4 +1,6 @@ -
+

<%= title %> @@ -28,7 +30,9 @@ <% amount_available = heading_price %> <% investments.each do |investment| %> - + <% if investment.winner? %> diff --git a/app/views/budgets/results/show.html.erb b/app/views/budgets/results/show.html.erb index 2343e652c..eb31124ec 100644 --- a/app/views/budgets/results/show.html.erb +++ b/app/views/budgets/results/show.html.erb @@ -26,7 +26,8 @@ <% @budget.headings.each do |heading| %> -
  • + <% active_class = heading.id.to_s == params[:heading_id] ? 'bold' : '' %> +
  • <%= link_to heading.name, budget_results_path(@budget, heading_id: heading.id) %>
  • @@ -34,7 +35,7 @@

    - <%= link_to t("budgets.results.hide_discarded_link"), "#", class: "js-toggle-link button hollow margin-bottom", data: {'toggle-selector' => '.js-discarded', 'toggle-text' => t("budgets.results.show_all_link")} %> + <%= link_to t("budgets.results.show_all_link"), "#", class: "js-toggle-link button hollow margin-bottom", data: {'toggle-selector' => '.js-discarded', 'toggle-text' => t("budgets.results.hide_discarded_link")} %> <%= render 'results_table', results_type: :compatible, @@ -42,8 +43,10 @@ heading_price: @heading.price, investments: @investments.compatible %> - <%= render 'results_table', results_type: :incompatible, - title: t("budgets.results.incompatibles"), - heading_price: @heading.price, - investments: @investments.incompatible %> + <% if @investments.incompatible.present? %> + <%= render 'results_table', results_type: :incompatible, + title: t("budgets.results.incompatibles"), + heading_price: @heading.price, + investments: @investments.incompatible %> + <% end %>
    diff --git a/spec/features/admin/budget_investments_spec.rb b/spec/features/admin/budget_investments_spec.rb index 57dedcb54..925f105b0 100644 --- a/spec/features/admin/budget_investments_spec.rb +++ b/spec/features/admin/budget_investments_spec.rb @@ -312,7 +312,7 @@ feature 'Admin budget investments' do context "Edit" do scenario "Change title, incompatible, description or heading" do - budget_investment = create(:budget_investment, :selected) + budget_investment = create(:budget_investment, :incompatible) create(:budget_heading, group: budget_investment.group, name: "Barbate") visit admin_budget_budget_investment_path(budget_investment.budget, budget_investment) @@ -321,7 +321,7 @@ feature 'Admin budget investments' do fill_in 'budget_investment_title', with: 'Potatoes' fill_in 'budget_investment_description', with: 'Carrots' select "#{budget_investment.group.name}: Barbate", from: 'budget_investment[heading_id]' - check "budget_investment_incompatible" + uncheck "budget_investment_incompatible" check "budget_investment_selected" click_button 'Update' @@ -329,10 +329,21 @@ feature 'Admin budget investments' do expect(page).to have_content 'Potatoes' expect(page).to have_content 'Carrots' expect(page).to have_content 'Barbate' - expect(page).to have_content 'Incompatible' + expect(page).to have_content 'Compatibility: Compatible' expect(page).to have_content 'Selected' end + scenario "Compatible non-winner can't edit incompatibility" do + budget_investment = create(:budget_investment, :selected) + create(:budget_heading, group: budget_investment.group, name: "Tetuan") + + visit admin_budget_budget_investment_path(budget_investment.budget, budget_investment) + click_link 'Edit' + + expect(page).not_to have_content 'Compatibility' + expect(page).not_to have_content 'Mark as incompatible' + end + scenario "Add administrator" do budget_investment = create(:budget_investment) administrator = create(:administrator, user: create(:user, username: 'Marta', email: 'marta@admins.org')) diff --git a/spec/features/budgets/results_spec.rb b/spec/features/budgets/results_spec.rb index cd9cb0912..3b2d05c65 100644 --- a/spec/features/budgets/results_spec.rb +++ b/spec/features/budgets/results_spec.rb @@ -17,6 +17,21 @@ feature 'Results' do visit budget_path(budget) click_link "See results" + within("#budget-investments-compatible") do + expect(page).to have_content investment1.title + expect(page).to have_content investment2.title + expect(page).not_to have_content investment3.title + expect(page).not_to have_content investment4.title + + expect(investment1.title).to appear_before(investment2.title) + end + end + + scenario "Show non winner & incomaptible investments", :js do + visit budget_path(budget) + click_link "See results" + click_link "Show all" + within("#budget-investments-compatible") do expect(page).to have_content investment1.title expect(page).to have_content investment2.title @@ -31,21 +46,6 @@ feature 'Results' do end end - scenario "Displays non winner investments", :js do - visit budget_path(budget) - click_link "See results" - click_link "Hide discarded" - - within("#budget-investments-compatible") do - expect(page).to have_content investment1.title - expect(page).to have_content investment2.title - expect(page).not_to have_content investment3.title - expect(page).not_to have_content investment4.title - - expect(investment1.title).to appear_before(investment2.title) - end - end - scenario "If budget is in a phase different from finished results can't be accessed" do budget.update phase: (Budget::PHASES - ["finished"]).sample visit budget_path(budget) @@ -55,4 +55,14 @@ feature 'Results' do expect(page).to have_content "You do not have permission to carry out the action" end + scenario "No incompatible investments", :js do + investment3.incompatible = false + investment3.save + + visit budget_path(budget) + click_link "See results" + + expect(page).not_to have_content "Incompatibles" + end + end diff --git a/spec/models/budget/result_spec.rb b/spec/models/budget/result_spec.rb index 85eb14f3e..a650e25c5 100644 --- a/spec/models/budget/result_spec.rb +++ b/spec/models/budget/result_spec.rb @@ -15,7 +15,7 @@ describe Budget::Result do investment4 = create(:budget_investment, :selected, heading: heading, price: 500, ballot_lines_count: 600, winner: false) investment5 = create(:budget_investment, :selected, heading: heading, price: 100, ballot_lines_count: 500, winner: false) - Budget::Result.new(budget, heading).calculate_winners + described_class.new(budget, heading).calculate_winners expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id]) end @@ -29,7 +29,7 @@ describe Budget::Result do investment4 = create(:budget_investment, :winner, heading: heading, price: 500, ballot_lines_count: 600) investment5 = create(:budget_investment, :winner, heading: heading, price: 100, ballot_lines_count: 500) - Budget::Result.new(budget, heading).calculate_winners + described_class.new(budget, heading).calculate_winners expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id]) end @@ -49,6 +49,20 @@ describe Budget::Result do expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment4.id]) end end - end + context "When an incompatible is flagged as compatible again" do + it "recalculates winners taking it in consideration" do + investment1 = create(:budget_investment, :winner, heading: heading, price: 200, ballot_lines_count: 900) + investment2 = create(:budget_investment, :winner, heading: heading, price: 300, ballot_lines_count: 800) + investment3 = create(:budget_investment, :incompatible, heading: heading, price: 500, ballot_lines_count: 700) + investment4 = create(:budget_investment, :winner, heading: heading, price: 500, ballot_lines_count: 600) + investment5 = create(:budget_investment, :winner, heading: heading, price: 100, ballot_lines_count: 500) + + investment3.incompatible = false + investment3.save + + expect(heading.investments.winners.pluck(:id)).to match_array([investment1.id, investment2.id, investment3.id]) + end + end + end end