Skip to content

Commit

Permalink
fix: Prevent error on PP sort with end_date nil (#626)
Browse files Browse the repository at this point in the history
* fix: update to handle processes without start_date or end_date

* fix: update sort in controllers

* test: update test with process without end date

* refactor: optimize queries in controllers and update tests

* refactor: update sort processes in controllers

---------

Co-authored-by: Lucie Grau <87868063+luciegrau@users.noreply.github.com>
  • Loading branch information
2 people authored and Quentinchampenois committed Nov 22, 2024
1 parent d62d8a7 commit aec62da
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
11 changes: 8 additions & 3 deletions app/controllers/decidim/assemblies/assemblies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
5 changes: 3 additions & 2 deletions spec/controllers/assemblies_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/participatory_processes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aec62da

Please sign in to comment.