# frozen_string_literal: true module Decidim module ParticipatoryProcesses # A controller that holds the logic to show ParticipatoryProcesses in a # public layout. class ParticipatoryProcessesController < Decidim::ParticipatoryProcesses::ApplicationController include ParticipatorySpaceContext participatory_space_layout only: [:show, :all_metrics] include FilterResource helper_method :collection, :promoted_participatory_processes, :participatory_processes, :stats, :metrics, :default_date_filter, :related_processes, :linked_assemblies def index raise ActionController::RoutingError, "Not Found" if published_processes.none? enforce_permission_to :list, :process enforce_permission_to :list, :process_group end def show enforce_permission_to :read, :process, process: current_participatory_space end def all_metrics if current_participatory_space.show_statistics enforce_permission_to :read, :process, process: current_participatory_space else render status: :not_found end end private def search_klass ParticipatoryProcessSearch end def default_filter_params { scope_id: nil, area_id: nil, date: default_date_filter } end def organization_participatory_processes @organization_participatory_processes ||= OrganizationParticipatoryProcesses.new(current_organization).query end def current_participatory_space return unless params["slug"] @current_participatory_space ||= organization_participatory_processes.where(slug: params["slug"]).or( organization_participatory_processes.where(id: params["slug"]) ).first! end def published_processes @published_processes ||= OrganizationPublishedParticipatoryProcesses.new(current_organization, current_user) end def promoted_participatory_processes @promoted_participatory_processes ||= published_processes | PromotedParticipatoryProcesses.new end def collection @collection ||= participatory_processes + participatory_process_groups end def filtered_processes search.results end def participatory_processes @participatory_processes ||= filtered_processes.groupless end def participatory_process_groups @participatory_process_groups ||= Decidim::ParticipatoryProcessGroup .where(id: filtered_processes.grouped.group_ids) end def stats @stats ||= ParticipatoryProcessStatsPresenter.new(participatory_process: current_participatory_space) end def metrics @metrics ||= ParticipatoryProcessMetricChartsPresenter.new(participatory_process: current_participatory_space) end def default_date_filter return "active" if published_processes.any?(&:active?) return "upcoming" if published_processes.any?(&:upcoming?) return "past" if published_processes.any?(&:past?) "all" end def related_processes @related_processes ||= current_participatory_space .linked_participatory_space_resources(:participatory_processes, "related_processes") .published .all end def linked_assemblies @linked_assemblies ||= current_participatory_space.linked_participatory_space_resources(:assembly, "included_participatory_processes").public_spaces end end end end