diff --git a/app/components/valuation/budgets/row_component.html.erb b/app/components/valuation/budgets/row_component.html.erb index 5ba9933f8..279617bd3 100644 --- a/app/components/valuation/budgets/row_component.html.erb +++ b/app/components/valuation/budgets/row_component.html.erb @@ -6,7 +6,7 @@ <%= budget.current_phase.name %> - <%= investments.count %> + <%= valuation_open_investments_count %> <% if investments.any? %> diff --git a/app/components/valuation/budgets/row_component.rb b/app/components/valuation/budgets/row_component.rb index 4b75b196e..6795f90d9 100644 --- a/app/components/valuation/budgets/row_component.rb +++ b/app/components/valuation/budgets/row_component.rb @@ -9,8 +9,14 @@ class Valuation::Budgets::RowComponent < ApplicationComponent end def investments - return Budget::Investment.none unless budget.valuating? + return Budget::Investment.none unless budget.valuating_or_later? - budget.investments.visible_to_valuators.by_valuator(current_user.valuator).valuation_open + budget.investments.visible_to_valuator(current_user.valuator) + end + + def valuation_open_investments_count + return 0 unless budget.valuating? + + investments.valuation_open.count end end diff --git a/app/controllers/valuation/budget_investments_controller.rb b/app/controllers/valuation/budget_investments_controller.rb index f1ff5d834..4fcc990f1 100644 --- a/app/controllers/valuation/budget_investments_controller.rb +++ b/app/controllers/valuation/budget_investments_controller.rb @@ -16,8 +16,9 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController def index @heading_filters = heading_filters - @investments = if current_user.valuator? && @budget.present? - @budget.investments.visible_to_valuators.scoped_filter(params_for_current_valuator, @current_filter) + @investments = if current_user.valuator? + @budget.investments.visible_to_valuator(current_user.valuator) + .scoped_filter(params.permit(:budget_id, :heading_id), @current_filter) .order(cached_votes_up: :desc) .page(params[:page]) else @@ -72,7 +73,7 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController end def heading_filters - investments = @budget.investments.by_valuator(current_user.valuator&.id).visible_to_valuators.distinct + investments = @budget.investments.visible_to_valuator(current_user.valuator).distinct investment_headings = Budget::Heading.where(id: investments.pluck(:heading_id)).sort_by(&:name) all_headings_filter = [ @@ -92,11 +93,6 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController end end - def params_for_current_valuator - Budget::Investment.filter_params(params).to_h.merge({ valuator_id: current_user.valuator.id, - budget_id: @budget.id }) - end - def valuation_params params.require(:budget_investment).permit(allowed_params) end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 76fe714f4..306f89793 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -100,6 +100,7 @@ class Budget scope :by_heading, ->(heading_id) { where(heading_id: heading_id) } scope :by_admin, ->(admin_id) { where(administrator_id: admin_id) } scope :by_tag, ->(tag_name) { tagged_with(tag_name).distinct } + scope :visible_to_valuator, ->(valuator) { visible_to_valuators.by_valuator(valuator) } scope :for_render, -> { includes(:heading) } diff --git a/spec/components/valuation/budgets/row_component_spec.rb b/spec/components/valuation/budgets/row_component_spec.rb index 4d0f1a7af..384ec05e6 100644 --- a/spec/components/valuation/budgets/row_component_spec.rb +++ b/spec/components/valuation/budgets/row_component_spec.rb @@ -5,46 +5,105 @@ describe Valuation::Budgets::RowComponent do before { sign_in(valuator.user) } - it "Displays visible and assigned investments count when budget is in valuating phase" do - budget = create(:budget, :valuating, name: "Sports") - create(:budget_investment, :visible_to_valuators, budget: budget, valuators: [valuator]) - create(:budget_investment, :invisible_to_valuators, budget: budget, valuators: [valuator]) - create(:budget_investment, :visible_to_valuators, budget: budget) + describe "investments count" do + it "counts visible and assigned investments when the budget is in the valuating phase" do + budget = create(:budget, :valuating) + create(:budget_investment, :visible_to_valuators, budget: budget, valuators: [valuator]) + create(:budget_investment, :invisible_to_valuators, budget: budget, valuators: [valuator]) + create(:budget_investment, :visible_to_valuators, budget: budget) - render_inline Valuation::Budgets::RowComponent.new(budget: budget) + render_inline Valuation::Budgets::RowComponent.new(budget: budget) - expect(page).to have_selector(".investments-count", text: "1") + expect(page).to have_selector ".investments-count", text: "1" + end + + it "does not count investments with valuation finished" do + budget = create(:budget, :valuating) + create(:budget_investment, :visible_to_valuators, + budget: budget, + valuators: [valuator], + valuation_finished: true) + + render_inline Valuation::Budgets::RowComponent.new(budget: budget) + + expect(page).to have_selector ".investments-count", text: "0" + end + + it "displays zero when the budget hasn't reached the valuating phase" do + budget = create(:budget, :accepting) + create(:budget_investment, :visible_to_valuators, budget: budget, valuators: [valuator]) + + render_inline Valuation::Budgets::RowComponent.new(budget: budget) + + expect(page).to have_selector ".investments-count", text: "0" + end + + it "displays zero when the valuating phase is over" do + budget = create(:budget, :finished) + create(:budget_investment, :visible_to_valuators, budget: budget, valuators: [valuator]) + + render_inline Valuation::Budgets::RowComponent.new(budget: budget) + + expect(page).to have_selector ".investments-count", text: "0" + end end - it "Displays zero as investments count when budget is not in valuating phase" do - budget = create(:budget, %i[accepting finished].sample, name: "Sports") - create(:budget_investment, :visible_to_valuators, budget: budget, valuators: [valuator]) + describe "link to evaluate investments" do + it "is shown when the valuator has visible investments assigned in the valuating phase" do + budget = create(:budget, :valuating) + create(:budget_investment, :visible_to_valuators, budget: budget, valuators: [valuator]) - render_inline Valuation::Budgets::RowComponent.new(budget: budget) + render_inline Valuation::Budgets::RowComponent.new(budget: budget) - expect(page).to have_selector(".investments-count", text: "0") - end + expect(page).to have_link "Evaluate" + end - it "Displays the link to evaluate investments when valuator has visible investments assigned and budget is - in valuating phase" do - valuating = create(:budget, :valuating) - create(:budget_investment, :visible_to_valuators, budget: valuating, valuators: [valuator]) - valuating_invisible = create(:budget, :valuating) - create(:budget_investment, :invisible_to_valuators, budget: valuating_invisible, valuators: [valuator]) - valuating_unassigned = create(:budget, :valuating) - create(:budget_investment, :visible_to_valuators, budget: valuating_unassigned) - accepting = create(:budget, :accepting) - create(:budget_investment, :visible_to_valuators, budget: accepting, valuators: [valuator]) - finished = create(:budget, :finished) - create(:budget_investment, :visible_to_valuators, budget: finished, valuators: [valuator]) - budgets = [valuating, valuating_invisible, valuating_unassigned, accepting, finished] + it "is shown when the assigned investments have finished valuation" do + budget = create(:budget, :valuating) + create(:budget_investment, :visible_to_valuators, + budget: budget, + valuators: [valuator], + valuation_finished: true) - render_inline Valuation::Budgets::RowComponent.with_collection(budgets) + render_inline Valuation::Budgets::RowComponent.new(budget: budget) - expect(page.find("#budget_#{valuating.id}")).to have_link("Evaluate") - expect(page.find("#budget_#{valuating_invisible.id}")).not_to have_link("Evaluate") - expect(page.find("#budget_#{valuating_unassigned.id}")).not_to have_link("Evaluate") - expect(page.find("#budget_#{accepting.id}")).not_to have_link("Evaluate") - expect(page.find("#budget_#{finished.id}")).not_to have_link("Evaluate") + expect(page).to have_link "Evaluate" + end + + it "is not shown when the assigned investments aren't visible to valuators" do + budget = create(:budget, :valuating) + create(:budget_investment, :invisible_to_valuators, budget: budget, valuators: [valuator]) + + render_inline Valuation::Budgets::RowComponent.new(budget: budget) + + expect(page).not_to have_link "Evaluate" + end + + it "is not shown when the valuator doesn't have assigned investments" do + budget = create(:budget, :valuating) + create(:budget_investment, :visible_to_valuators, budget: budget) + + render_inline Valuation::Budgets::RowComponent.new(budget: budget) + + expect(page).not_to have_link "Evaluate" + end + + it "is not shown when the budget hasn't reached the valuating phase" do + budget = create(:budget, :accepting) + create(:budget_investment, :visible_to_valuators, budget: budget, valuators: [valuator]) + + render_inline Valuation::Budgets::RowComponent.new(budget: budget) + + expect(page).not_to have_link "Evaluate" + end + + it "is shown when the valuating phase is over" do + budget = create(:budget, :finished) + create(:budget_investment, :visible_to_valuators, budget: budget, valuators: [valuator]) + + render_inline Valuation::Budgets::RowComponent.new(budget: budget) + + expect(page).to have_link "Evaluate" + end end end