Files
nairobi/app/models/poll/question/option.rb
taitus a29eeaf2e2 Add option_id to partial results and unique index
Similar to what we did in PR "Avoid duplicate records in poll answers" 5539,
specifically in commit 503369166, we want to stop relying on the plain text
"answer" and start using "option_id" to avoid issues with counts across
translations and to add consistency to the poll_partial_results table.

Note that we also moved the `possible_answers` method from Poll::Question to
Poll::Question::Option, since the list of valid answers really comes from the
options of a question and not from the question itself. Tests were updated
to validate answers against the translations of the assigned option.

Additionally, we renamed lambda parameters in validations to improve clarity.
2025-09-26 15:05:34 +02:00

59 lines
1.8 KiB
Ruby

class Poll::Question::Option < ApplicationRecord
self.table_name = "poll_question_answers"
include Galleryable
include Documentable
translates :title, touch: true
translates :description, touch: true
include Globalizable
accepts_nested_attributes_for :documents, allow_destroy: true
belongs_to :question, class_name: "Poll::Question"
has_many :answers, class_name: "Poll::Answer", dependent: :nullify
has_many :partial_results, class_name: "Poll::PartialResult", dependent: :nullify
has_many :videos, class_name: "Poll::Question::Option::Video",
dependent: :destroy,
foreign_key: "answer_id",
inverse_of: :option
validates_translation :title, presence: true
validates :given_order, presence: true, uniqueness: { scope: :question_id }
scope :with_content, -> { excluding(without_content) }
scope :without_content, -> do
where(description: "")
.where.missing(:images)
.where.missing(:documents)
.where.missing(:videos)
end
def self.order_options(ordered_array)
ordered_array.each_with_index do |option_id, order|
find(option_id).update_column(:given_order, order + 1)
end
end
def self.last_position(question_id)
where(question_id: question_id).maximum("given_order") || 0
end
def total_votes
Poll::Answer.where(question_id: question, answer: title).count +
::Poll::PartialResult.where(question: question).where(answer: title).sum(:amount)
end
def total_votes_percentage
question.options_total_votes.zero? ? 0 : (total_votes * 100.0) / question.options_total_votes
end
def with_read_more?
description.present? || images.any? || documents.any? || videos.any?
end
def possible_answers
translations.pluck(:title)
end
end