We were very inconsistent regarding these rules. Personally I prefer no empty lines around blocks, clases, etc... as recommended by the Ruby style guide [1], and they're the default values in rubocop, so those are the settings I'm applying. The exception is the `private` access modifier, since we were leaving empty lines around it most of the time. That's the default rubocop rule as well. Personally I don't have a strong preference about this one. [1] https://rubystyle.guide/#empty-lines-around-bodies
33 lines
1.3 KiB
Ruby
33 lines
1.3 KiB
Ruby
class Poll::PairAnswer < ApplicationRecord
|
|
belongs_to :question, -> { with_hidden }
|
|
belongs_to :author, -> { with_hidden }, class_name: "User", foreign_key: "author_id"
|
|
belongs_to :answer_right, class_name: "Poll::Question::Answer", foreign_key: "answer_rigth_id"
|
|
belongs_to :answer_left, class_name: "Poll::Question::Answer", foreign_key: "answer_left_id"
|
|
|
|
delegate :poll, :poll_id, to: :question
|
|
|
|
validates :question, presence: true
|
|
validates :author, presence: true
|
|
validates :answer_left, presence: true
|
|
validates :answer_right, presence: true
|
|
|
|
validates :answer_left, inclusion: { in: ->(a) { a.question.question_answers.visibles }},
|
|
unless: ->(a) { a.question.blank? }
|
|
|
|
validates :answer_right, inclusion: { in: ->(a) { a.question.question_answers.visibles }},
|
|
unless: ->(a) { a.question.blank? }
|
|
|
|
scope :by_author, ->(author_id) { where(author_id: author_id) }
|
|
scope :by_question, ->(question_id) { where(question_id: question_id) }
|
|
|
|
def self.generate_pair(question, user)
|
|
answers = question.question_answers.visibles.sample(2)
|
|
question.pair_answers.by_author(user).map(&:destroy)
|
|
question.pair_answers.create(author: user, answer_left: answers[0], answer_right: answers[1])
|
|
end
|
|
|
|
def answers
|
|
[answer_left, answer_right].compact
|
|
end
|
|
end
|