Rename Poll::Question::Answer to Poll::Question::Option

Having a class named `Poll::Question::Answer` and another class named
`Poll::Answer` was so confusing that no developer working on the project
has ever been capable of remembering which is which for more than a few
seconds.

Furthermore, we're planning to add open answers to polls, and we might
add a reference from the `poll_answers` table to the
`poll_question_answers` table to property differentiate between open
answers and closed answers. Having yet another thing named answer would
be more than what our brains can handle (we know it because we did this
once in a prototype).

So we're renaming `Poll::Question::Answer` to `Poll::Question::Option`.
Hopefully that'll make it easier to remember. The name is also (more or
less) consistent with the `Legislation::QuestionOption` class, which is
similar.

We aren't changing the table or columns names for now in order to avoid
possible issues when upgrading (old code running with the new database
tables/columns after running the migrations but before deployment has
finished, for instance). We might do it in the future.

I've tried not to change the internationalization keys either so
existing translations would still be valid. However, since we have to
change the keys in `activerecord.yml` so methods like
`human_attribute_name` keep working, I'm also changing them in places
where similar keys were used (like `poll_question_answer` or
`poll/question/answer`).

Note that it isn't clear whether we should use `option` or
`question_option` in some cases. In order to keep things simple, we're
using `option` where we were using `answer` and `question_option` where
we were using `question_answer`.

Also note we're adding tests for the admin menu component, since at
first I forgot to change the `answers` reference there and all tests
passed.
This commit is contained in:
Javi Martín
2024-05-13 17:38:43 +02:00
parent 6188281d33
commit 38b38d1fcc
100 changed files with 676 additions and 655 deletions

View File

@@ -14,8 +14,8 @@ class Poll::Question < ApplicationRecord
has_many :comments, as: :commentable, inverse_of: :commentable
has_many :answers, class_name: "Poll::Answer"
has_many :question_answers, -> { order "given_order asc" },
class_name: "Poll::Question::Answer",
has_many :question_options, -> { order "given_order asc" },
class_name: "Poll::Question::Option",
inverse_of: :question,
dependent: :destroy
has_many :partial_results
@@ -25,7 +25,7 @@ class Poll::Question < ApplicationRecord
validates :author, presence: true
validates :poll_id, presence: true, if: proc { |question| question.poll.nil? }
accepts_nested_attributes_for :question_answers, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :question_options, reject_if: :all_blank, allow_destroy: true
scope :by_poll_id, ->(poll_id) { where(poll_id: poll_id) }
@@ -63,23 +63,23 @@ class Poll::Question < ApplicationRecord
where(poll_id: Poll.answerable_by(user).pluck(:id))
end
def answers_total_votes
question_answers.reduce(0) { |total, question_answer| total + question_answer.total_votes }
def options_total_votes
question_options.reduce(0) { |total, question_option| total + question_option.total_votes }
end
def most_voted_answer_id
question_answers.max_by(&:total_votes)&.id
def most_voted_option_id
question_options.max_by(&:total_votes)&.id
end
def possible_answers
question_answers.joins(:translations).pluck(:title)
question_options.joins(:translations).pluck(:title)
end
def answers_with_read_more?
answers_with_read_more.any?
def options_with_read_more?
options_with_read_more.any?
end
def answers_with_read_more
question_answers.select(&:with_read_more?)
def options_with_read_more
question_options.select(&:with_read_more?)
end
end