diff --git a/app/controllers/admin/budgets_controller.rb b/app/controllers/admin/budgets_controller.rb index dcf6b0839..771a8c91d 100644 --- a/app/controllers/admin/budgets_controller.rb +++ b/app/controllers/admin/budgets_controller.rb @@ -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 diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index e257e3052..48503a17d 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -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 diff --git a/app/controllers/concerns/report_attributes.rb b/app/controllers/concerns/report_attributes.rb new file mode 100644 index 000000000..520dab316 --- /dev/null +++ b/app/controllers/concerns/report_attributes.rb @@ -0,0 +1,9 @@ +module ReportAttributes + extend ActiveSupport::Concern + + private + + def report_attributes + Report::KINDS.map { |kind| :"#{kind}_enabled" } + end +end diff --git a/app/models/abilities/everyone.rb b/app/models/abilities/everyone.rb index 46dc19585..9eab1e14b 100644 --- a/app/models/abilities/everyone.rb +++ b/app/models/abilities/everyone.rb @@ -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 diff --git a/app/models/budget.rb b/app/models/budget.rb index f11c378ff..fffb4f342 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -3,6 +3,7 @@ class Budget < ApplicationRecord include Measurable include Sluggable include StatsVersionable + include Reportable translates :name, touch: true include Globalizable diff --git a/app/views/admin/budgets/_form.html.erb b/app/views/admin/budgets/_form.html.erb index e570da1bd..4d611d56c 100644 --- a/app/views/admin/budgets/_form.html.erb +++ b/app/views/admin/budgets/_form.html.erb @@ -65,6 +65,10 @@ <% end %> +
+ <%= render "admin/shared/show_results_fields", form: f %> +
+
<%= f.submit nil, class: "button success" %> diff --git a/app/views/admin/shared/_show_results_fields.html.erb b/app/views/admin/shared/_show_results_fields.html.erb index 47e9b3b92..48c60ae86 100644 --- a/app/views/admin/shared/_show_results_fields.html.erb +++ b/app/views/admin/shared/_show_results_fields.html.erb @@ -1,6 +1,6 @@
- <%= t("admin.polls.new.show_results_and_stats") %> + <%= t("admin.shared.show_results_and_stats") %> <%= form.check_box :results_enabled %> <%= form.check_box :stats_enabled %> -

<%= t("admin.polls.new.results_and_stats_reminder") %>

+

<%= t("admin.shared.results_and_stats_reminder") %>

diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index 736f12c0d..c4c4b55e6 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -127,6 +127,7 @@ ignore_unused: - "budgets.index.section_header.*" - "activerecord.*" - "activemodel.*" + - "attributes.*" - "date.order" - "unauthorized.*" - "admin.officials.level_*" diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index f173fb379..83e920012 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -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" diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 3916ccdd6..81bc293fe 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -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 diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index a46e832e9..66c94a089 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -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" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 5b51391b6..a636e6b73 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -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 diff --git a/spec/factories/budgets.rb b/spec/factories/budgets.rb index 0d9faa1ee..66cc1d24f 100644 --- a/spec/factories/budgets.rb +++ b/spec/factories/budgets.rb @@ -61,6 +61,8 @@ FactoryBot.define do trait :finished do phase "finished" + results_enabled true + stats_enabled true end end diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index 75967555a..4d01a2c12 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -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) diff --git a/spec/models/abilities/everyone_spec.rb b/spec/models/abilities/everyone_spec.rb index 671294ac5..59ba8cdbe 100644 --- a/spec/models/abilities/everyone_spec.rb +++ b/spec/models/abilities/everyone_spec.rb @@ -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 diff --git a/spec/models/budget_spec.rb b/spec/models/budget_spec.rb index 87063b549..123035732 100644 --- a/spec/models/budget_spec.rb +++ b/spec/models/budget_spec.rb @@ -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 diff --git a/spec/models/concerns/reportable.rb b/spec/models/concerns/reportable.rb index 06ae8c0c1..c18caa8dd 100644 --- a/spec/models/concerns/reportable.rb +++ b/spec/models/concerns/reportable.rb @@ -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