Remove Questionable concern
Since we were only using it in one place, it made the code harder to follow. We'll extract it again if we ever find a way to reuse it.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
class Poll::Question < ApplicationRecord
|
||||
include Measurable
|
||||
include Searchable
|
||||
include Questionable
|
||||
|
||||
acts_as_paranoid column: :hidden_at
|
||||
include ActsAsParanoidAliases
|
||||
@@ -19,6 +18,7 @@ class Poll::Question < ApplicationRecord
|
||||
inverse_of: :question,
|
||||
dependent: :destroy
|
||||
has_many :partial_results
|
||||
has_one :votation_type, as: :questionable, dependent: :destroy
|
||||
belongs_to :proposal
|
||||
|
||||
validates_translation :title, presence: true, length: { minimum: 4 }
|
||||
@@ -26,6 +26,9 @@ class Poll::Question < ApplicationRecord
|
||||
validates :poll_id, presence: true, if: proc { |question| question.poll.nil? }
|
||||
|
||||
accepts_nested_attributes_for :question_options, reject_if: :all_blank, allow_destroy: true
|
||||
accepts_nested_attributes_for :votation_type
|
||||
|
||||
delegate :max_votes, :multiple?, :vote_type, to: :votation_type, allow_nil: true
|
||||
|
||||
scope :by_poll_id, ->(poll_id) { where(poll_id: poll_id) }
|
||||
|
||||
@@ -82,4 +85,25 @@ class Poll::Question < ApplicationRecord
|
||||
def options_with_read_more
|
||||
question_options.select(&:with_read_more?)
|
||||
end
|
||||
|
||||
def unique?
|
||||
votation_type.nil? || votation_type.unique?
|
||||
end
|
||||
|
||||
def find_or_initialize_user_answer(user, title)
|
||||
answer = answers.find_or_initialize_by(find_by_attributes(user, title))
|
||||
answer.answer = title
|
||||
answer
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_by_attributes(user, title)
|
||||
case vote_type
|
||||
when "unique", nil
|
||||
{ author: user }
|
||||
when "multiple"
|
||||
{ author: user, answer: title }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user