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 class Admin::BudgetsController < Admin::BaseController
include Translatable include Translatable
include ReportAttributes
include FeatureFlags include FeatureFlags
feature_flag :budgets feature_flag :budgets
@@ -62,7 +63,7 @@ class Admin::BudgetsController < Admin::BaseController
def budget_params def budget_params
descriptions = Budget::Phase::PHASE_KINDS.map{|p| "description_#{p}"}.map(&:to_sym) descriptions = Budget::Phase::PHASE_KINDS.map{|p| "description_#{p}"}.map(&:to_sym)
valid_attributes = [:phase, :currency_symbol] + descriptions 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
end end

View File

@@ -1,6 +1,7 @@
class Admin::Poll::PollsController < Admin::Poll::BaseController class Admin::Poll::PollsController < Admin::Poll::BaseController
include Translatable include Translatable
include ImageAttributes include ImageAttributes
include ReportAttributes
load_and_authorize_resource load_and_authorize_resource
before_action :load_search, only: [:search_booths, :search_officers] before_action :load_search, only: [:search_booths, :search_officers]
@@ -75,11 +76,10 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController
end end
def poll_params def poll_params
image_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy] attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :budget_id,
attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :results_enabled, geozone_ids: [], image_attributes: image_attributes]
:stats_enabled, :budget_id, geozone_ids: [],
image_attributes: image_attributes] params.require(:poll).permit(*attributes, *report_attributes, translation_params(Poll))
params.require(:poll).permit(*attributes, translation_params(Poll))
end end
def search_params 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
can [:read], Budget::Group can [:read], Budget::Group
can [:read, :print, :json_data], Budget::Investment can [:read, :print, :json_data], Budget::Investment
can [:read_results, :read_executions], Budget, phase: "finished" can(:read_results, Budget) { |budget| budget.results_enabled? && budget.finished? }
can(:read_stats, Budget) { |budget| budget.valuating_or_later? } can(:read_stats, Budget) { |budget| budget.stats_enabled? && budget.valuating_or_later? }
can :read_executions, Budget, phase: "finished"
can :new, DirectMessage can :new, DirectMessage
can [:read, :debate, :draft_publication, :allegations, :result_publication, can [:read, :debate, :draft_publication, :allegations, :result_publication,
:proposals, :milestones], Legislation::Process, published: true :proposals, :milestones], Legislation::Process, published: true

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
<fieldset class="fieldset"> <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 :results_enabled %>
<%= form.check_box :stats_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> </fieldset>

View File

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

View File

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

View File

@@ -1048,8 +1048,6 @@ en:
geozone_restricted: "Restricted to districts" geozone_restricted: "Restricted to districts"
new: new:
title: "New poll" 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" submit_button: "Create poll"
edit: edit:
title: "Edit poll" title: "Edit poll"
@@ -1327,6 +1325,8 @@ en:
created_at: Created at created_at: Created at
delete: Delete delete: Delete
color_help: Hexadecimal format 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: spending_proposals:
index: index:
geozone_filter_all: All zones geozone_filter_all: All zones

View File

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

View File

@@ -1047,8 +1047,6 @@ es:
geozone_restricted: "Restringida a los distritos" geozone_restricted: "Restringida a los distritos"
new: new:
title: "Nueva votación" 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" submit_button: "Crear votación"
edit: edit:
title: "Editar votación" title: "Editar votación"
@@ -1326,6 +1324,8 @@ es:
created_at: Fecha de creación created_at: Fecha de creación
delete: Eliminar delete: Eliminar
color_help: Formato hexadecimal 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: spending_proposals:
index: index:
geozone_filter_all: Todos los ámbitos de actuación geozone_filter_all: Todos los ámbitos de actuación

View File

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

View File

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

View File

@@ -8,9 +8,6 @@ describe Abilities::Everyone do
let(:debate) { create(:debate) } let(:debate) { create(:debate) }
let(:proposal) { create(:proposal) } 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(:index, Debate) }
it { should be_able_to(:show, debate) } it { should be_able_to(:show, debate) }
it { should_not be_able_to(:edit, 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(: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) } it { should_not be_able_to(:manage, Dashboard::Action) }
context "when accessing poll results" do context "when accessing poll results" do
@@ -74,17 +69,43 @@ describe Abilities::Everyone do
end end
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 "when accessing budget stats" do
context "supports phase is not finished" 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) } it { should_not be_able_to(:read_stats, budget) }
end end
context "supports phase is finished" do 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) } it { should be_able_to(:read_stats, budget) }
end 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
end end

View File

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

View File

@@ -29,6 +29,8 @@ shared_examples "reportable" do
end end
it "uses the `has_one` relation instead of the original column" do 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) reportable.update(results_enabled: true)
expect(reportable.read_attribute(:results_enabled)).to be false expect(reportable.read_attribute(:results_enabled)).to be false
@@ -63,6 +65,8 @@ shared_examples "reportable" do
end end
it "uses the `has_one` relation instead of the original column" do 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) reportable.update(stats_enabled: true)
expect(reportable.read_attribute(:stats_enabled)).to be false expect(reportable.read_attribute(:stats_enabled)).to be false