diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 78b11d321..2e4e12045 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -13,7 +13,7 @@ class PollsController < ApplicationController def index @polls = Kaminari.paginate_array( - @polls.public_polls.not_budget.send(@current_filter).includes(:geozones).sort_for_list + @polls.created_by_admin.not_budget.send(@current_filter).includes(:geozones).sort_for_list ).page(params[:page]) end diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index f4f57ec5c..a3de04ccf 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -49,19 +49,15 @@ module PollsHelper end def link_to_poll(text, poll) - if poll.results_enabled? + if can?(:results, poll) link_to text, results_poll_path(id: poll.slug || poll.id) - elsif poll.stats_enabled? + elsif can?(:stats, poll) link_to text, stats_poll_path(id: poll.slug || poll.id) else link_to text, poll_path(id: poll.slug || poll.id) end end - def show_stats_or_results? - @poll.expired? && (@poll.results_enabled? || @poll.stats_enabled?) - end - def results_menu? controller_name == "polls" && action_name == "results" end diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index b997e56a4..25dcb60c7 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -75,7 +75,7 @@ module Abilities can [:index, :create, :edit, :update, :destroy], Geozone - can [:read, :create, :update, :destroy, :add_question, :search_booths, :search_officers, :booth_assignments, :results, :stats], Poll + can [:read, :create, :update, :destroy, :add_question, :search_booths, :search_officers, :booth_assignments], Poll can [:read, :create, :update, :destroy, :available], Poll::Booth can [:search, :create, :index, :destroy], ::Poll::Officer can [:create, :destroy, :manage], ::Poll::BoothAssignment diff --git a/app/models/abilities/everyone.rb b/app/models/abilities/everyone.rb index 6cdc8b6c9..89a29f4b9 100644 --- a/app/models/abilities/everyone.rb +++ b/app/models/abilities/everyone.rb @@ -7,20 +7,16 @@ module Abilities can [:read, :map, :summary, :share], Proposal can :read, Comment can :read, Poll - can :results, Poll do |poll| - poll.expired? && poll.results_enabled? - end - can :stats, Poll do |poll| - poll.expired? && poll.stats_enabled? - end + can :results, Poll, id: Poll.expired.results_enabled.not_budget.ids + can :stats, Poll, id: Poll.expired.stats_enabled.not_budget.ids can :read, Poll::Question can :read, User can [:read, :welcome], Budget can [:read], Budget can [:read], Budget::Group can [:read, :print, :json_data], Budget::Investment - can(:read_results, Budget) { |budget| budget.results_enabled? && budget.finished? } - can(:read_stats, Budget) { |budget| budget.stats_enabled? && budget.valuating_or_later? } + can :read_results, Budget, id: Budget.finished.results_enabled.ids + can :read_stats, Budget, id: Budget.valuating_or_later.stats_enabled.ids can :read_executions, Budget, phase: "finished" can :new, DirectMessage can [:read, :debate, :draft_publication, :allegations, :result_publication, diff --git a/app/models/budget.rb b/app/models/budget.rb index 57be318ca..26489419c 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -47,6 +47,7 @@ class Budget < ApplicationRecord scope :reviewing, -> { where(phase: "reviewing") } scope :selecting, -> { where(phase: "selecting") } scope :valuating, -> { where(phase: "valuating") } + scope :valuating_or_later, -> { where(phase: Budget::Phase.kind_or_later("valuating")) } scope :publishing_prices, -> { where(phase: "publishing_prices") } scope :balloting, -> { where(phase: "balloting") } scope :reviewing_ballots, -> { where(phase: "reviewing_ballots") } diff --git a/app/models/budget/phase.rb b/app/models/budget/phase.rb index de90075a7..de3b8c0f1 100644 --- a/app/models/budget/phase.rb +++ b/app/models/budget/phase.rb @@ -32,6 +32,10 @@ class Budget define_singleton_method(phase) { find_by(kind: phase) } end + def self.kind_or_later(phase) + PHASE_KINDS[PHASE_KINDS.index(phase)..-1] + end + def next_enabled_phase next_phase&.enabled? ? next_phase : next_phase&.next_enabled_phase end @@ -92,7 +96,7 @@ class Budget end def in_phase_or_later?(phase) - PHASE_KINDS.index(kind) >= PHASE_KINDS.index(phase) + self.class.kind_or_later(phase).include?(kind) end end end diff --git a/app/models/concerns/reportable.rb b/app/models/concerns/reportable.rb index f584faa86..5a57551f8 100644 --- a/app/models/concerns/reportable.rb +++ b/app/models/concerns/reportable.rb @@ -4,6 +4,10 @@ module Reportable included do has_one :report, as: :process, inverse_of: :process, dependent: :destroy accepts_nested_attributes_for :report + + Report::KINDS.each do |kind| + scope "#{kind}_enabled", -> { joins(:report).where("reports.#{kind}": true) } + end end def report diff --git a/app/models/poll.rb b/app/models/poll.rb index 8b07b8aeb..bd577670b 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -40,7 +40,6 @@ class Poll < ApplicationRecord accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true scope :for, ->(element) { where(related: element) } - scope :public_polls, -> { where(related: nil) } scope :current, -> { where("starts_at <= ? and ? <= ends_at", Date.current.beginning_of_day, Date.current.beginning_of_day) } scope :expired, -> { where("ends_at < ?", Date.current.beginning_of_day) } scope :recounting, -> { where(ends_at: (Date.current.beginning_of_day - RECOUNT_DURATION)..Date.current.beginning_of_day) } diff --git a/app/views/polls/_poll_subnav.html.erb b/app/views/polls/_poll_subnav.html.erb index e50d64e0a..1fdc2d71d 100644 --- a/app/views/polls/_poll_subnav.html.erb +++ b/app/views/polls/_poll_subnav.html.erb @@ -1,8 +1,8 @@ -<% if show_stats_or_results? %> +<% if can?(:stats, @poll) || can?(:results, @poll) %>