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:
7
app/lib/poll_partial_result_option_finder.rb
Normal file
7
app/lib/poll_partial_result_option_finder.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class PollPartialResultOptionFinder < PollOptionFinder
|
||||
private
|
||||
|
||||
def existing_choices
|
||||
question.partial_results.where(option_id: nil).distinct.pluck(:answer)
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user