From 45a3d8daf0152c4f18dc49a03f04329ebe62a1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 29 Apr 2019 17:45:01 +0200 Subject: [PATCH] Add option to enable advanced stats --- app/models/concerns/statisticable.rb | 4 ++++ app/models/report.rb | 2 +- .../admin/shared/_show_results_fields.html.erb | 1 + app/views/budgets/stats/show.html.erb | 4 ++-- app/views/polls/stats.html.erb | 4 ++-- config/locales/en/activerecord.yml | 1 + config/locales/es/activerecord.yml | 1 + ...0429125842_add_advanced_stats_to_reports.rb | 5 +++++ db/schema.rb | 7 ++++--- spec/features/budgets/stats_spec.rb | 18 +++++++++++++++++- spec/features/polls/polls_spec.rb | 11 +++++++++++ 11 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20190429125842_add_advanced_stats_to_reports.rb diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index acb5d00c9..b45de3f6f 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -93,6 +93,10 @@ module Statisticable "v#{resource.find_or_create_stats_version.updated_at.to_i}" end + def advanced? + resource.advanced_stats_enabled? + end + private def base_stats_methods diff --git a/app/models/report.rb b/app/models/report.rb index 5e1f3dcb1..857cb462f 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -1,5 +1,5 @@ class Report < ApplicationRecord - KINDS = %i[results stats] + KINDS = %i[results stats advanced_stats] belongs_to :process, polymorphic: true end diff --git a/app/views/admin/shared/_show_results_fields.html.erb b/app/views/admin/shared/_show_results_fields.html.erb index 48c60ae86..54cd2b70c 100644 --- a/app/views/admin/shared/_show_results_fields.html.erb +++ b/app/views/admin/shared/_show_results_fields.html.erb @@ -2,5 +2,6 @@ <%= t("admin.shared.show_results_and_stats") %> <%= form.check_box :results_enabled %> <%= form.check_box :stats_enabled %> + <%= form.check_box :advanced_stats_enabled %>

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

diff --git a/app/views/budgets/stats/show.html.erb b/app/views/budgets/stats/show.html.erb index f4ad25de8..bbbfda4bb 100644 --- a/app/views/budgets/stats/show.html.erb +++ b/app/views/budgets/stats/show.html.erb @@ -41,12 +41,12 @@
<%= render "shared/stats/participation", stats: @stats %> - <%= render "advanced_stats", stats: @stats %> + <%= render "advanced_stats", stats: @stats if @stats.advanced? %>
diff --git a/app/views/polls/stats.html.erb b/app/views/polls/stats.html.erb index 0c2f2226b..8e43d5b58 100644 --- a/app/views/polls/stats.html.erb +++ b/app/views/polls/stats.html.erb @@ -8,12 +8,12 @@
<%= render "shared/stats/participation", stats: @stats %> - <%= render "advanced_stats", stats: @stats %> + <%= render "advanced_stats", stats: @stats if @stats.advanced? %>

diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 83e920012..97c9431ca 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -2,6 +2,7 @@ en: attributes: results_enabled: "Show results" stats_enabled: "Show stats" + advanced_stats_enabled: "Show advanced stats" activerecord: models: activity: diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index 66c94a089..13dd8783c 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -2,6 +2,7 @@ es: attributes: results_enabled: "Mostrar resultados" stats_enabled: "Mostrar estadísticas" + advanced_stats_enabled: "Mostrar estadísticas avanzadas" activerecord: models: activity: diff --git a/db/migrate/20190429125842_add_advanced_stats_to_reports.rb b/db/migrate/20190429125842_add_advanced_stats_to_reports.rb new file mode 100644 index 000000000..025a2dad2 --- /dev/null +++ b/db/migrate/20190429125842_add_advanced_stats_to_reports.rb @@ -0,0 +1,5 @@ +class AddAdvancedStatsToReports < ActiveRecord::Migration[5.0] + def change + add_column :reports, :advanced_stats, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index d655284bf..b9bfc50a7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20190424114803) do +ActiveRecord::Schema.define(version: 20190429125842) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1239,8 +1239,9 @@ ActiveRecord::Schema.define(version: 20190424114803) do t.boolean "results" t.string "process_type" t.integer "process_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "advanced_stats" t.index ["process_type", "process_id"], name: "index_reports_on_process_type_and_process_id", using: :btree end diff --git a/spec/features/budgets/stats_spec.rb b/spec/features/budgets/stats_spec.rb index c014389cb..f459a74a4 100644 --- a/spec/features/budgets/stats_spec.rb +++ b/spec/features/budgets/stats_spec.rb @@ -7,6 +7,22 @@ feature "Stats" do let(:heading) { create(:budget_heading, group: group, price: 1000) } describe "Show" do - end + describe "advanced stats" do + let(:budget) { create(:budget, :finished) } + scenario "advanced stats enabled" do + budget.update(advanced_stats_enabled: true) + + visit budget_stats_path(budget) + + expect(page).to have_content "Advanced statistics" + end + + scenario "advanced stats disabled" do + visit budget_stats_path(budget) + + expect(page).not_to have_content "Advanced statistics" + end + end + end end diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb index 8470cd100..12ff3eb93 100644 --- a/spec/features/polls/polls_spec.rb +++ b/spec/features/polls/polls_spec.rb @@ -432,7 +432,18 @@ feature "Polls" do expect(page).to have_content("Questions") visit stats_poll_path(poll) + expect(page).to have_content("Participation data") + expect(page).not_to have_content "Advanced statistics" + end + + scenario "Advanced stats enabled" do + poll = create(:poll, :expired, stats_enabled: true, advanced_stats_enabled: true) + + visit stats_poll_path(poll) + + expect(page).to have_content "Participation data" + expect(page).to have_content "Advanced statistics" end scenario "Don't show poll results and stats if not enabled" do