Fix condition to show the "see results" link

This condition was obsolete since we introduced the `results_enabled`
field in commit 4f4dc2c2a.
This commit is contained in:
Javi Martín
2021-08-30 17:28:18 +02:00
parent 0a14337580
commit 7d818e24ca
11 changed files with 100 additions and 6 deletions

View File

@@ -1,8 +1,6 @@
<div class="budgets-links">
<% if budget.has_winning_investments? %>
<%= action(:results,
text: t("budgets.show.see_results"),
path: budget_results_path(budget)) %>
<% if can?(:read_results, budget) %>
<%= action(:results, text: results_text, path: budget_results_path(budget)) %>
<% end %>
<%= action(:preview, text: t("admin.budgets.actions.preview"), path: budget_path(budget), target: "_blank") %>

View File

@@ -1,5 +1,6 @@
class Admin::Budgets::LinksComponent < ApplicationComponent
attr_reader :budget
delegate :can?, to: :helpers
def initialize(budget)
@budget = budget
@@ -10,4 +11,12 @@ class Admin::Budgets::LinksComponent < ApplicationComponent
def action(action_name, **options)
render Admin::ActionComponent.new(action_name, budget, **options)
end
def results_text
if Abilities::Everyone.new(User.new).can?(:read_results, budget)
t("budgets.show.see_results")
else
t("admin.budgets.actions.preview_results")
end
end
end

View File

@@ -64,6 +64,9 @@ module Abilities
can [:index, :read, :new, :create, :update, :destroy], Budget
can :publish, Budget, id: Budget.drafting.ids
can :calculate_winners, Budget, &:reviewing_ballots?
can :read_results, Budget do |budget|
budget.balloting_finished? && budget.has_winning_investments?
end
can [:read, :create, :update, :destroy], Budget::Group
can [:read, :create, :update, :destroy], Budget::Heading

View File

@@ -158,6 +158,10 @@ class Budget < ApplicationRecord
current_phase&.balloting_or_later?
end
def balloting_finished?
balloting_or_later? && !balloting?
end
def single_group?
groups.one?
end

View File

@@ -1,4 +1,4 @@
<% if @budget.has_winning_investments? %>
<% if can?(:read_results, @budget) %>
<div class="float-right">
<%= link_to t("admin.budget_investments.index.see_results"),
budget_results_path(@budget, heading_id: @budget.headings.first),

View File

@@ -71,6 +71,7 @@ en:
actions:
edit: "Edit budget"
preview: "Preview"
preview_results: "Preview results"
index:
title: Participatory budgets
new_link: Create new budget

View File

@@ -71,6 +71,7 @@ es:
actions:
edit: "Editar presupuesto"
preview: "Previsualizar"
preview_results: "Previsualizar resultados"
index:
title: Presupuestos participativos
new_link: Crear nuevo presupuesto

View File

@@ -0,0 +1,59 @@
require "rails_helper"
describe Admin::Budgets::LinksComponent, controller: Admin::BaseController do
before { sign_in(create(:administrator).user) }
describe "see results link" do
let(:budget) { create(:budget, :finished) }
let(:component) { Admin::Budgets::LinksComponent.new(budget) }
it "is shown for budgets with results enabled" do
budget.update!(results_enabled: true)
render_inline component
expect(page).to have_link "See results"
expect(page).not_to have_link "Preview results"
end
it "is not shown for budgets with results disabled" do
budget.update!(results_enabled: false)
render_inline component
expect(page).not_to have_link "See results"
expect(page).not_to have_link "Preview results"
end
context "after calculating winners" do
let(:budget) { create(:budget, :with_winner) }
it "is shown as a preview link after finishing the process" do
budget.update!(phase: "finished", results_enabled: false)
render_inline component
expect(page).to have_link "Preview results"
expect(page).not_to have_link "See results"
end
it "is shown as a preview link after balloting has finished" do
budget.update!(phase: "reviewing_ballots", results_enabled: false)
render_inline component
expect(page).to have_link "Preview results"
expect(page).not_to have_link "See results"
end
it "is not shown while balloting" do
budget.update!(phase: "balloting", results_enabled: true)
render_inline component
expect(page).not_to have_link "Preview results"
expect(page).not_to have_link "See results"
end
end
end
end

View File

@@ -64,6 +64,10 @@ FactoryBot.define do
trait :approval do
voting_style { "approval" }
end
trait :with_winner do
after(:create) { |budget| create(:budget_investment, :winner, budget: budget) }
end
end
factory :budget_group, class: "Budget::Group" do

View File

@@ -77,7 +77,14 @@ describe Abilities::Administrator do
it { should be_able_to(:create, Budget) }
it { should be_able_to(:update, Budget) }
it { should be_able_to(:read_results, Budget) }
it { should be_able_to(:read_results, create(:budget, :reviewing_ballots, :with_winner)) }
it { should be_able_to(:read_results, create(:budget, :finished, :with_winner)) }
it { should be_able_to(:read_results, create(:budget, :finished, results_enabled: true)) }
it { should_not be_able_to(:read_results, create(:budget, :balloting, :with_winner, results_enabled: true)) }
it { should_not be_able_to(:read_results, create(:budget, :reviewing_ballots, results_enabled: true)) }
it { should_not be_able_to(:read_results, create(:budget, :finished, results_enabled: false)) }
it { should be_able_to(:calculate_winners, create(:budget, :reviewing_ballots)) }
it { should_not be_able_to(:calculate_winners, create(:budget, :balloting)) }
it { should_not be_able_to(:calculate_winners, create(:budget, :finished)) }

View File

@@ -351,6 +351,14 @@ describe "Admin budgets", :admin do
visit admin_budget_path(budget)
expect(page).not_to have_link "See results"
click_link "Edit budget"
select "Finished budget", from: "Active phase"
check "Show results"
click_button "Update Budget"
expect(page).to have_content "Participatory budget updated successfully"
expect(page).to have_link "See results"
end