Files
grecia/app/models/poll/question/answer.rb
Javi Martín 6d26ce57cb Show answers with attachments in additional info
We weren't showing the details of answers without a description, even if
they had images, videos or documents. Some users found that behavior
unexpected since the description isn't a mandatory field and so they
left it blank, but they added images to that answer and they didn't
appear on the poll page.

Note we had a condition not to show the title of an answer when it had
no description. I think that condition was redundant because answers
without a description weren't loaded in the first place. Anyway, that
condition doesn't make sense anymore because we're displaying answers
with images but no description.
2021-10-06 16:52:23 +02:00

44 lines
1.4 KiB
Ruby

class Poll::Question::Answer < ApplicationRecord
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 :videos, class_name: "Poll::Question::Answer::Video", dependent: :destroy
validates_translation :title, presence: true
validates :given_order, presence: true, uniqueness: { scope: :question_id }
scope :with_content, -> { where.not(id: without_content) }
scope :without_content, -> do
where(description: "")
.left_joins(:images).where(images: { id: nil })
.left_joins(:documents).where(documents: { id: nil })
.left_joins(:videos).where(poll_question_answer_videos: { id: nil })
end
def self.order_answers(ordered_array)
ordered_array.each_with_index do |answer_id, order|
find(answer_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.answers_total_votes.zero? ? 0 : (total_votes * 100.0) / question.answers_total_votes
end
end