From c9fb47aa3d12b0c2cc265f2cddd4b94c79655380 Mon Sep 17 00:00:00 2001 From: taitus Date: Thu, 11 Sep 2025 09:31:24 +0200 Subject: [PATCH] Add PollPartialResultOptionFinder to extend PollOptionFinder Introduce a dedicated finder for partial results, reusing the logic of PollOptionFinder. This will be used in rake tasks to avoid code duplication and make the intent clearer. --- app/lib/poll_partial_result_option_finder.rb | 7 +++++++ lib/tasks/polls.rake | 13 +++---------- 2 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 app/lib/poll_partial_result_option_finder.rb diff --git a/app/lib/poll_partial_result_option_finder.rb b/app/lib/poll_partial_result_option_finder.rb new file mode 100644 index 000000000..1bf64da83 --- /dev/null +++ b/app/lib/poll_partial_result_option_finder.rb @@ -0,0 +1,7 @@ +class PollPartialResultOptionFinder < PollOptionFinder + private + + def existing_choices + question.partial_results.where(option_id: nil).distinct.pluck(:answer) + end +end diff --git a/lib/tasks/polls.rake b/lib/tasks/polls.rake index 8645243f7..2d7d2d064 100644 --- a/lib/tasks/polls.rake +++ b/lib/tasks/polls.rake @@ -155,20 +155,13 @@ namespace :polls do Tenant.run_on_each do Poll::Question.find_each do |question| - options = question.question_options.joins(:translations).reorder(:id) - existing_choices = question.partial_results.where(option_id: nil).distinct.pluck(:answer) + option_finder = PollPartialResultOptionFinder.new(question) - choices_map = existing_choices.to_h do |choice| - [choice, options.where("lower(title) = lower(?)", choice).distinct.ids] - end - - manageable_choices, unmanageable_choices = choices_map.partition { |choice, ids| ids.count == 1 } - - manageable_choices.each do |choice, ids| + option_finder.manageable_choices.each do |choice, ids| question.partial_results.where(option_id: nil, answer: choice).update_all(option_id: ids.first) end - unmanageable_choices.each do |choice, ids| + option_finder.unmanageable_choices.each do |choice, ids| tenant_info = " on tenant #{Tenant.current_schema}" unless Tenant.default? if ids.count == 0