Add show results and stats form to budgets

This commit is contained in:
Javi Martín
2019-04-24 14:18:19 +02:00
parent e3d6dbacab
commit 4f4dc2c2af
17 changed files with 73 additions and 26 deletions

View File

@@ -1,5 +1,6 @@
class Admin::BudgetsController < Admin::BaseController
include Translatable
include ReportAttributes
include FeatureFlags
feature_flag :budgets
@@ -62,7 +63,7 @@ class Admin::BudgetsController < Admin::BaseController
def budget_params
descriptions = Budget::Phase::PHASE_KINDS.map{|p| "description_#{p}"}.map(&:to_sym)
valid_attributes = [:phase, :currency_symbol] + descriptions
params.require(:budget).permit(*valid_attributes, translation_params(Budget))
params.require(:budget).permit(*valid_attributes, *report_attributes, translation_params(Budget))
end
end

View File

@@ -1,6 +1,7 @@
class Admin::Poll::PollsController < Admin::Poll::BaseController
include Translatable
include ImageAttributes
include ReportAttributes
load_and_authorize_resource
before_action :load_search, only: [:search_booths, :search_officers]
@@ -75,11 +76,10 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController
end
def poll_params
image_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :results_enabled,
:stats_enabled, :budget_id, geozone_ids: [],
image_attributes: image_attributes]
params.require(:poll).permit(*attributes, translation_params(Poll))
attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :budget_id,
geozone_ids: [], image_attributes: image_attributes]
params.require(:poll).permit(*attributes, *report_attributes, translation_params(Poll))
end
def search_params

View File

@@ -0,0 +1,9 @@
module ReportAttributes
extend ActiveSupport::Concern
private
def report_attributes
Report::KINDS.map { |kind| :"#{kind}_enabled" }
end
end

View File

@@ -20,8 +20,9 @@ module Abilities
can [:read], Budget
can [:read], Budget::Group
can [:read, :print, :json_data], Budget::Investment
can [:read_results, :read_executions], Budget, phase: "finished"
can(:read_stats, Budget) { |budget| budget.valuating_or_later? }
can(:read_results, Budget) { |budget| budget.results_enabled? && budget.finished? }
can(:read_stats, Budget) { |budget| budget.stats_enabled? && budget.valuating_or_later? }
can :read_executions, Budget, phase: "finished"
can :new, DirectMessage
can [:read, :debate, :draft_publication, :allegations, :result_publication,
:proposals, :milestones], Legislation::Process, published: true

View File

@@ -3,6 +3,7 @@ class Budget < ApplicationRecord
include Measurable
include Sluggable
include StatsVersionable
include Reportable
translates :name, touch: true
include Globalizable

View File

@@ -65,6 +65,10 @@
</div>
<% end %>
<div class="small-12 column">
<%= render "admin/shared/show_results_fields", form: f %>
</div>
<div class="small-12 column">
<div class="clear small-12 medium-4 large-3 inline-block">
<%= f.submit nil, class: "button success" %>

View File

@@ -1,6 +1,6 @@
<fieldset class="fieldset">
<legend><%= t("admin.polls.new.show_results_and_stats") %></legend>
<legend><%= t("admin.shared.show_results_and_stats") %></legend>
<%= form.check_box :results_enabled %>
<%= form.check_box :stats_enabled %>
<p class="small"><%= t("admin.polls.new.results_and_stats_reminder") %></p>
<p class="small"><%= t("admin.shared.results_and_stats_reminder") %></p>
</fieldset>

View File

@@ -127,6 +127,7 @@ ignore_unused:
- "budgets.index.section_header.*"
- "activerecord.*"
- "activemodel.*"
- "attributes.*"
- "date.order"
- "unauthorized.*"
- "admin.officials.level_*"

View File

@@ -1,4 +1,7 @@
en:
attributes:
results_enabled: "Show results"
stats_enabled: "Show stats"
activerecord:
models:
activity:
@@ -212,8 +215,6 @@ en:
geozone_restricted: "Restricted by geozone"
summary: "Summary"
description: "Description"
results_enabled: "Show results"
stats_enabled: "Show stats"
poll/translation:
name: "Name"
summary: "Summary"

View File

@@ -1048,8 +1048,6 @@ en:
geozone_restricted: "Restricted to districts"
new:
title: "New poll"
show_results_and_stats: "Show results and stats"
results_and_stats_reminder: "Marking these checkboxes the results and/or stats of this poll will be publicly available and every user will see them."
submit_button: "Create poll"
edit:
title: "Edit poll"
@@ -1327,6 +1325,8 @@ en:
created_at: Created at
delete: Delete
color_help: Hexadecimal format
show_results_and_stats: "Show results and stats"
results_and_stats_reminder: "Marking these checkboxes the results and/or stats will be publicly available and every user will see them."
spending_proposals:
index:
geozone_filter_all: All zones

View File

@@ -1,4 +1,7 @@
es:
attributes:
results_enabled: "Mostrar resultados"
stats_enabled: "Mostrar estadísticas"
activerecord:
models:
activity:
@@ -212,8 +215,6 @@ es:
geozone_restricted: "Restringida por zonas"
summary: "Resumen"
description: "Descripción"
results_enabled: "Mostrar resultados"
stats_enabled: "Mostrar estadísticas"
poll/translation:
name: "Nombre"
summary: "Resumen"

View File

@@ -1047,8 +1047,6 @@ es:
geozone_restricted: "Restringida a los distritos"
new:
title: "Nueva votación"
show_results_and_stats: "Mostrar resultados y estadísticas"
results_and_stats_reminder: "Si marcas estas casillas los resultados y/o estadísticas de esta votación serán públicos y podrán verlos todos los usuarios."
submit_button: "Crear votación"
edit:
title: "Editar votación"
@@ -1326,6 +1324,8 @@ es:
created_at: Fecha de creación
delete: Eliminar
color_help: Formato hexadecimal
show_results_and_stats: "Mostrar resultados y estadísticas"
results_and_stats_reminder: "Si marcas estas casillas los resultados y/o estadísticas serán públicos y podrán verlos todos los usuarios."
spending_proposals:
index:
geozone_filter_all: Todos los ámbitos de actuación

View File

@@ -61,6 +61,8 @@ FactoryBot.define do
trait :finished do
phase "finished"
results_enabled true
stats_enabled true
end
end

View File

@@ -522,7 +522,7 @@ feature "Budget Investments" do
context "Results Phase" do
before { budget.update(phase: "finished") }
before { budget.update(phase: "finished", results_enabled: true) }
scenario "show winners by default" do
investment1 = create(:budget_investment, :winner, heading: heading)

View File

@@ -8,9 +8,6 @@ describe Abilities::Everyone do
let(:debate) { create(:debate) }
let(:proposal) { create(:proposal) }
let(:reviewing_ballot_budget) { create(:budget, phase: "reviewing_ballots") }
let(:finished_budget) { create(:budget, phase: "finished") }
it { should be_able_to(:index, Debate) }
it { should be_able_to(:show, debate) }
it { should_not be_able_to(:edit, Debate) }
@@ -32,8 +29,6 @@ describe Abilities::Everyone do
it { should be_able_to(:index, Budget) }
it { should be_able_to(:read_results, finished_budget) }
it { should_not be_able_to(:read_results, reviewing_ballot_budget) }
it { should_not be_able_to(:manage, Dashboard::Action) }
context "when accessing poll results" do
@@ -74,17 +69,43 @@ describe Abilities::Everyone do
end
end
context "when accessing budget results" do
context "budget is not finished" do
let(:budget) { create(:budget, phase: "reviewing_ballots", results_enabled: true) }
it { should_not be_able_to(:read_results, budget) }
end
context "budget is finished" do
let(:budget) { create(:budget, :finished) }
it { should be_able_to(:read_results, budget) }
end
context "results disabled" do
let(:budget) { create(:budget, :finished, results_enabled: false) }
it { should_not be_able_to(:read_results, budget) }
end
end
context "when accessing budget stats" do
context "supports phase is not finished" do
let(:budget) { create(:budget, phase: "selecting") }
let(:budget) { create(:budget, phase: "selecting", stats_enabled: true) }
it { should_not be_able_to(:read_stats, budget) }
end
context "supports phase is finished" do
let(:budget) { create(:budget, phase: "valuating") }
let(:budget) { create(:budget, phase: "valuating", stats_enabled: true) }
it { should be_able_to(:read_stats, budget) }
end
context "stats disabled" do
let(:budget) { create(:budget, phase: "valuating", stats_enabled: false) }
it { should_not be_able_to(:read_stats, budget) }
end
end
end

View File

@@ -5,6 +5,7 @@ describe Budget do
let(:budget) { create(:budget) }
it_behaves_like "sluggable", updatable_slug_trait: :drafting
it_behaves_like "reportable"
describe "name" do
before do

View File

@@ -29,6 +29,8 @@ shared_examples "reportable" do
end
it "uses the `has_one` relation instead of the original column" do
skip "there's no original column" unless reportable.has_attribute?(:results_enabled)
reportable.update(results_enabled: true)
expect(reportable.read_attribute(:results_enabled)).to be false
@@ -63,6 +65,8 @@ shared_examples "reportable" do
end
it "uses the `has_one` relation instead of the original column" do
skip "there's no original column" unless reportable.has_attribute?(:stats_enabled)
reportable.update(stats_enabled: true)
expect(reportable.read_attribute(:stats_enabled)).to be false