diff --git a/app/controllers/decidim/assemblies/assemblies_controller.rb b/app/controllers/decidim/assemblies/assemblies_controller.rb index 6fa700f10d..c409bde9a2 100644 --- a/app/controllers/decidim/assemblies/assemblies_controller.rb +++ b/app/controllers/decidim/assemblies/assemblies_controller.rb @@ -89,10 +89,11 @@ def assembly_participatory_processes @assembly_participatory_processes ||= @current_participatory_space.linked_participatory_space_resources(:participatory_processes, "included_participatory_processes") else @assembly_participatory_processes = @current_participatory_space.linked_participatory_space_resources(:participatory_processes, "included_participatory_processes") + @active_processes ||= @assembly_participatory_processes.select(&:active?) sorted_by_date = { - active: @assembly_participatory_processes.active_spaces.sort_by(&:end_date), - future: @assembly_participatory_processes.future_spaces.sort_by(&:start_date), - past: @assembly_participatory_processes.past_spaces.sort_by(&:end_date).reverse + active: @active_processes.reject { |process| process.end_date.nil? }.sort_by(&:end_date) + processes_without_end_date(@active_processes), + future: @assembly_participatory_processes.upcoming.sort_by(&:start_date), + past: @assembly_participatory_processes.past.sort_by(&:end_date).reverse } @assembly_participatory_processes = sorted_by_date end @@ -101,6 +102,10 @@ def assembly_participatory_processes def current_assemblies_settings @current_assemblies_settings ||= Decidim::AssembliesSetting.find_or_create_by(decidim_organization_id: current_organization.id) end + + def processes_without_end_date(processes) + processes.select { |process| process.end_date.nil? } + end end end end diff --git a/app/controllers/decidim/participatory_processes/participatory_processes_controller.rb b/app/controllers/decidim/participatory_processes/participatory_processes_controller.rb index e1d755b7a3..72b7cf0f25 100644 --- a/app/controllers/decidim/participatory_processes/participatory_processes_controller.rb +++ b/app/controllers/decidim/participatory_processes/participatory_processes_controller.rb @@ -136,7 +136,7 @@ def linked_assemblies def custom_sort(date) case date when "active" - @participatory_processes.sort_by(&:end_date) + @participatory_processes.reject { |process| process.end_date.nil? }.sort_by(&:end_date) + processes_without_end_date(@participatory_processes) when "past" @participatory_processes.sort_by(&:end_date).reverse when "upcoming" @@ -149,11 +149,16 @@ def custom_sort(date) end def sort_all_processes - actives = @participatory_processes.select(&:active?).sort_by(&:end_date) + @actives_processes ||= @participatory_processes.select(&:active?) + actives = @actives_processes.reject { |process| process.end_date.nil? }.sort_by(&:end_date) + processes_without_end_date(@actives_processes) pasts = @participatory_processes.select(&:past?).sort_by(&:end_date).reverse upcomings = @participatory_processes.select(&:upcoming?).sort_by(&:start_date) (actives + upcomings + pasts) end + + def processes_without_end_date(processes) + processes.select { |process| process.end_date.nil? } + end end end end diff --git a/spec/controllers/assemblies_controller_spec.rb b/spec/controllers/assemblies_controller_spec.rb index f28fd0e2c6..5d90dee6a5 100644 --- a/spec/controllers/assemblies_controller_spec.rb +++ b/spec/controllers/assemblies_controller_spec.rb @@ -146,11 +146,12 @@ module Assemblies context "when sort_by_date variable is true" do before do allow(Rails.application.secrets).to receive(:dig).with(:decidim, :participatory_processes, :sort_by_date).and_return(true) + active_processes.first.update(end_date: nil) end - it "includes only participatory processes related to the assembly, actives one by end_date then upcoming ones by start_date then past ones by end_date reversed" do + it "includes only participatory processes related to the assembly, actives one by end_date with end_date nil last, then upcoming ones by start_date then past ones by end_date reversed" do sorted_participatory_processes = { - active: participatory_processes.select(&:active?).sort_by(&:end_date), + active: participatory_processes.select { |process| process.active? && !process.end_date.nil? }.sort_by(&:end_date) + participatory_processes.select { |process| process.active? && process.end_date.nil? }, future: participatory_processes.select(&:upcoming?).sort_by(&:start_date), past: participatory_processes.select(&:past?).sort_by(&:end_date).reverse } diff --git a/spec/controllers/participatory_processes_controller_spec.rb b/spec/controllers/participatory_processes_controller_spec.rb index 52d34d206b..977ccc12b6 100644 --- a/spec/controllers/participatory_processes_controller_spec.rb +++ b/spec/controllers/participatory_processes_controller_spec.rb @@ -137,11 +137,12 @@ module ParticipatoryProcesses context "and sort_by_date is true" do before do allow(Rails.application.secrets).to receive(:dig).with(:decidim, :participatory_processes, :sort_by_date).and_return(true) + active_processes.first.update(end_date: nil) end # search.with_date will default to "active" it "orders active processes by end date" do - expect(controller.helpers.participatory_processes).to eq(active_processes.sort_by(&:end_date)) + expect(controller.helpers.participatory_processes).to eq(active_processes.reject { |process| process.end_date.nil? }.sort_by(&:end_date) + active_processes.select { |process| process.end_date.nil? }) end end end