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/controllers/dashboard/polls_controller.rb b/app/controllers/dashboard/polls_controller.rb index 116f1d99d..f97f91281 100644 --- a/app/controllers/dashboard/polls_controller.rb +++ b/app/controllers/dashboard/polls_controller.rb @@ -15,8 +15,7 @@ class Dashboard::PollsController < Dashboard::BaseController def create authorize! :manage_polls, proposal - @poll = Poll.new(poll_params.merge(author: current_user, related: proposal, - stats_enabled: false)) + @poll = Poll.new(poll_params.merge(author: current_user, related: proposal)) if @poll.save redirect_to proposal_dashboard_polls_path(proposal), notice: t("flash.actions.create.poll") else @@ -54,7 +53,7 @@ class Dashboard::PollsController < Dashboard::BaseController end def poll_attributes - [:name, :starts_at, :ends_at, :description, :results_enabled, :stats_enabled, + [:name, :starts_at, :ends_at, :description, :results_enabled, questions_attributes: question_attributes] 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/models/concerns/reportable.rb b/app/models/concerns/reportable.rb new file mode 100644 index 000000000..45cc7b328 --- /dev/null +++ b/app/models/concerns/reportable.rb @@ -0,0 +1,23 @@ +module Reportable + extend ActiveSupport::Concern + + included do + has_one :report, as: :process, dependent: :destroy + accepts_nested_attributes_for :report + end + + def report + super || build_report + end + + Report::KINDS.each do |kind| + define_method "#{kind}_enabled?" do + report.send(kind) + end + alias_method "#{kind}_enabled", "#{kind}_enabled?" + + define_method "#{kind}_enabled=" do |enabled| + report.send("#{kind}=", enabled) + end + end +end 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/poll.rb b/app/models/poll.rb index 4484a4589..6d0e344b0 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -7,6 +7,7 @@ class Poll < ApplicationRecord include Notifiable include Sluggable include StatsVersionable + include Reportable translates :name, touch: true translates :summary, touch: true diff --git a/app/models/report.rb b/app/models/report.rb new file mode 100644 index 000000000..857cb462f --- /dev/null +++ b/app/models/report.rb @@ -0,0 +1,5 @@ +class Report < ApplicationRecord + KINDS = %i[results stats advanced_stats] + + belongs_to :process, polymorphic: true +end 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 %> +
| <%= t("stats.budgets.heading") %> | +<%= t("stats.budgets.investments_sent_html") %> | + + <% stats.all_phases.each do |phase| %> ++ <%= t("stats.budgets.participants_#{phase}_phase") %> + | + <% end %> +||
|---|---|---|---|---|
| <%= t("stats.budgets.total") %> | +<%= t("stats.budgets.percent_total_participants") %> | +<%= t("stats.budgets.percent_heading_census") %> | + <% end %> +||
| + <%= heading.name %> + | ++ <%= stats.headings[heading.id][:total_investments_count] %> + | + + <% stats.all_phases.each do |phase| %> ++ <%= stats.headings[heading.id]["total_participants_#{phase}_phase".to_sym] %> + | ++ <%= number_to_stats_percentage(stats.headings[heading.id]["percentage_participants_#{phase}_phase".to_sym]) %> + | ++ <%= number_to_stats_percentage(stats.headings[heading.id]["percentage_district_population_#{phase}_phase".to_sym]) %> + | + <% end %> +
<%= link_to t("stats.advanced"), "#advanced_statistics" %>
+ diff --git a/app/views/budgets/stats/show.html.erb b/app/views/budgets/stats/show.html.erb index 0ad241529..bbbfda4bb 100644 --- a/app/views/budgets/stats/show.html.erb +++ b/app/views/budgets/stats/show.html.erb @@ -41,107 +41,12 @@| <%= t("stats.budgets.heading") %> | -<%= t("stats.budgets.investments_sent_html") %> | - - <% @stats.all_phases.each do |phase| %> -- <%= t("stats.budgets.participants_#{phase}_phase") %> - | - <% end %> -||
|---|---|---|---|---|
| <%= t("stats.budgets.total") %> | -<%= t("stats.budgets.percent_total_participants") %> | -<%= t("stats.budgets.percent_heading_census") %> | - <% end %> -||
| - <%= heading.name %> - | -- <%= @stats.headings[heading.id][:total_investments_count] %> - | - - <% @stats.all_phases.each do |phase| %> -- <%= @stats.headings[heading.id]["total_participants_#{phase}_phase".to_sym] %> - | -- <%= number_to_stats_percentage(@stats.headings[heading.id]["percentage_participants_#{phase}_phase".to_sym]) %> - | -- <%= number_to_stats_percentage(@stats.headings[heading.id]["percentage_district_population_#{phase}_phase".to_sym]) %> - | - <% end %> -
| <%= t("polls.show.stats.votes") %> | + <% stats.channels.each do |channel| %> +<%= t("polls.show.stats.#{channel}") %> | + <% end %> +<%= t("polls.show.stats.total") %> | +
|---|---|---|
| <%= t("polls.show.stats.valid") %> | + + <% stats.channels.each do |channel| %> ++ <%= stats.send(:"total_#{channel}_valid") %> + (<%= stats.send(:"valid_percentage_#{channel}").round(2) %>%) + | + <% end %> + ++ <%= stats.total_valid_votes %> + (<%= stats.total_valid_percentage.round(2) %>%) + | + +
| <%= t("polls.show.stats.white") %> | + + <% stats.channels.each do |channel| %> ++ <%= stats.send(:"total_#{channel}_white") %> + (<%= stats.send(:"white_percentage_#{channel}").round(2) %>%) + | + <% end %> + +<%= stats.total_white_votes %> + (<%= stats.total_white_percentage.round(2) %>%) + | +
| <%= t("polls.show.stats.null_votes") %> | + + <% stats.channels.each do |channel| %> ++ <%= stats.send(:"total_#{channel}_null") %> + (<%= stats.send(:"null_percentage_#{channel}").round(2) %>%) + | + <% end %> + ++ <%= stats.total_null_votes %> + (<%= stats.total_null_percentage.round(2) %>%) + | +
| <%= t("polls.show.stats.total") %> | + + <% stats.channels.each do |channel| %> ++ <%= stats.send(:"total_participants_#{channel}") %> + (<%= stats.send(:"total_participants_#{channel}_percentage").round(2) %>%) + | + <% end %> + +<%= stats.total_participants %> | +
<%= link_to t("stats.advanced"), "#advanced_statistics" %>
+ diff --git a/app/views/polls/stats.html.erb b/app/views/polls/stats.html.erb index 1284b26fa..8e43d5b58 100644 --- a/app/views/polls/stats.html.erb +++ b/app/views/polls/stats.html.erb @@ -8,118 +8,17 @@| <%= t("polls.show.stats.votes") %> | - <% @stats.channels.each do |channel| %> -<%= t("polls.show.stats.#{channel}") %> | - <% end %> -<%= t("polls.show.stats.total") %> | -
|---|---|---|
| <%= t("polls.show.stats.valid") %> | - - <% @stats.channels.each do |channel| %> -- <%= @stats.send(:"total_#{channel}_valid") %> - (<%= @stats.send(:"valid_percentage_#{channel}").round(2) %>%) - | - <% end %> - -- <%= @stats.total_valid_votes %> - (<%= @stats.total_valid_percentage.round(2) %>%) - | - -
| <%= t("polls.show.stats.white") %> | - - <% @stats.channels.each do |channel| %> -- <%= @stats.send(:"total_#{channel}_white") %> - (<%= @stats.send(:"white_percentage_#{channel}").round(2) %>%) - | - <% end %> - -<%= @stats.total_white_votes %> - (<%= @stats.total_white_percentage.round(2) %>%) - | -
| <%= t("polls.show.stats.null_votes") %> | - - <% @stats.channels.each do |channel| %> -- <%= @stats.send(:"total_#{channel}_null") %> - (<%= @stats.send(:"null_percentage_#{channel}").round(2) %>%) - | - <% end %> - -- <%= @stats.total_null_votes %> - (<%= @stats.total_null_percentage.round(2) %>%) - | -
| <%= t("polls.show.stats.total") %> | - - <% @stats.channels.each do |channel| %> -- <%= @stats.send(:"total_participants_#{channel}") %> - (<%= @stats.send(:"total_participants_#{channel}_percentage").round(2) %>%) - | - <% end %> - -<%= @stats.total_participants %> | -
- <%= t("stats.no_demographic_data", count: @stats.total_no_demographic_data) %> -
-+ <%= t("stats.no_demographic_data", count: @stats.total_no_demographic_data) %> +