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,30 +0,0 @@
|
|||||||
module Questionable
|
|
||||||
extend ActiveSupport::Concern
|
|
||||||
|
|
||||||
included do
|
|
||||||
has_one :votation_type, as: :questionable, dependent: :destroy
|
|
||||||
accepts_nested_attributes_for :votation_type
|
|
||||||
delegate :max_votes, :multiple?, :vote_type, to: :votation_type, allow_nil: true
|
|
||||||
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
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
class Poll::Question < ApplicationRecord
|
class Poll::Question < ApplicationRecord
|
||||||
include Measurable
|
include Measurable
|
||||||
include Searchable
|
include Searchable
|
||||||
include Questionable
|
|
||||||
|
|
||||||
acts_as_paranoid column: :hidden_at
|
acts_as_paranoid column: :hidden_at
|
||||||
include ActsAsParanoidAliases
|
include ActsAsParanoidAliases
|
||||||
@@ -19,6 +18,7 @@ class Poll::Question < ApplicationRecord
|
|||||||
inverse_of: :question,
|
inverse_of: :question,
|
||||||
dependent: :destroy
|
dependent: :destroy
|
||||||
has_many :partial_results
|
has_many :partial_results
|
||||||
|
has_one :votation_type, as: :questionable, dependent: :destroy
|
||||||
belongs_to :proposal
|
belongs_to :proposal
|
||||||
|
|
||||||
validates_translation :title, presence: true, length: { minimum: 4 }
|
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? }
|
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 :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) }
|
scope :by_poll_id, ->(poll_id) { where(poll_id: poll_id) }
|
||||||
|
|
||||||
@@ -82,4 +85,25 @@ class Poll::Question < ApplicationRecord
|
|||||||
def options_with_read_more
|
def options_with_read_more
|
||||||
question_options.select(&:with_read_more?)
|
question_options.select(&:with_read_more?)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user