Skip to content

Commit

Permalink
Merge pull request #5078 from consul/link_to_evaluate
Browse files Browse the repository at this point in the history
Show link to evaluate investments with valuation finished
  • Loading branch information
javierm committed Feb 20, 2023
2 parents 1b61eff + 54fbdf4 commit f7dfe30
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 45 deletions.
2 changes: 1 addition & 1 deletion app/components/valuation/budgets/row_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= budget.current_phase.name %>
</td>
<td class="investments-count">
<%= investments.count %>
<%= valuation_open_investments_count %>
</td>
<td>
<% if investments.any? %>
Expand Down
10 changes: 8 additions & 2 deletions app/components/valuation/budgets/row_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ def initialize(budget:)
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
12 changes: 4 additions & 8 deletions app/controllers/valuation/budget_investments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,7 +73,7 @@ def load_investment
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 = [
Expand All @@ -92,11 +93,6 @@ def heading_filters
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
Expand Down
1 change: 1 addition & 0 deletions app/models/budget/investment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Investment < ApplicationRecord
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) }

Expand Down
127 changes: 93 additions & 34 deletions spec/components/valuation/budgets/row_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,105 @@

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")
end
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)

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])
expect(page).to have_selector ".investments-count", text: "0"
end

render_inline Valuation::Budgets::RowComponent.new(budget: budget)
it "displays zero when the valuating phase is over" do
budget = create(:budget, :finished)
create(:budget_investment, :visible_to_valuators, budget: budget, valuators: [valuator])

expect(page).to have_selector(".investments-count", text: "0")
render_inline Valuation::Budgets::RowComponent.new(budget: budget)

expect(page).to have_selector ".investments-count", text: "0"
end
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]

render_inline Valuation::Budgets::RowComponent.with_collection(budgets)

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")
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)

expect(page).to have_link "Evaluate"
end

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.new(budget: budget)

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

0 comments on commit f7dfe30

Please sign in to comment.