Sha256: c9d2e5cd8f23b165aaa31ea640d4182435b8845af050e3433b77959ea5777fed

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

module Tasuku
  module Taskables
    class Question::Answer < ActiveRecord::Base
      include Taskable::Submission

      belongs_to :author, polymorphic: true
      has_many :votes, dependent: :destroy

      validates :author_id, :author_type, presence: true
      validates :votes, length: { minimum: 1 }
      validate :can_only_answer_each_question_once
      validate :can_only_vote_once_for_single_choice_questions

      def question
        votes.first.option.question if votes.any?
      end

      request is: :question

      def correct?
        votes.all? { |vote| vote.option.correct? }
      end

      private

      def can_only_answer_each_question_once
        errors.add :base, I18n.t('tasuku.taskables.questions.answers.already_answered') if question && question.answers.find_by(author: author)
      end

      def can_only_vote_once_for_single_choice_questions
        errors.add :base, I18n.t('tasuku.taskables.questions.answers.can_only_vote_once') if question && question.single? && votes.many?
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tasuku-1.1.0 app/models/tasuku/taskables/question/answer.rb
tasuku-1.0.1 app/models/tasuku/taskables/question/answer.rb
tasuku-0.1.1 app/models/tasuku/taskables/question/answer.rb
tasuku-0.1.0 app/models/tasuku/taskables/question/answer.rb