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.
This commit is contained in:
taitus
2025-09-11 09:31:24 +02:00
parent ed2a25663b
commit c9fb47aa3d
2 changed files with 10 additions and 10 deletions

View File

@@ -0,0 +1,7 @@
class PollPartialResultOptionFinder < PollOptionFinder
private
def existing_choices
question.partial_results.where(option_id: nil).distinct.pluck(:answer)
end
end

View File

@@ -155,20 +155,13 @@ namespace :polls do
Tenant.run_on_each do Tenant.run_on_each do
Poll::Question.find_each do |question| Poll::Question.find_each do |question|
options = question.question_options.joins(:translations).reorder(:id) option_finder = PollPartialResultOptionFinder.new(question)
existing_choices = question.partial_results.where(option_id: nil).distinct.pluck(:answer)
choices_map = existing_choices.to_h do |choice| option_finder.manageable_choices.each do |choice, ids|
[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|
question.partial_results.where(option_id: nil, answer: choice).update_all(option_id: ids.first) question.partial_results.where(option_id: nil, answer: choice).update_all(option_id: ids.first)
end 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? tenant_info = " on tenant #{Tenant.current_schema}" unless Tenant.default?
if ids.count == 0 if ids.count == 0