Files
nairobi/app/models/concerns/questionable.rb
decabeza 36e452437e Add questions with mutiple answers to polls public interface
The `reload` method added to max_votes validation is needed because the
author gets here with some changes because of the around_action
`switch_locale`, which adds some changes to the current user record and
therefore, the lock method raises an exception when trying to lock it
requiring us to save or discard those record changes.
2022-10-18 11:04:40 +02:00

31 lines
720 B
Ruby

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