require "cat_engine/bank" require "cat_engine/engine" module StudyEngine class Result < ActiveRecord::Base belongs_to :assessment, touch: true has_many :answers delegate :current_question, to: :bank def bank CatEngine::Bank.new(bank_id, history, assessment.full_history) end def name bank_id.sub(".flat","") end def history answers.pluck(:response_id).join(" ") end def save_answer answer wrapper do # race condition could bypass validity check if bank.valid_answer?(answer.response_id) answers << answer update_attributes bank.to_h save! end end end private def wrapper # UGH HACK HACK HACK why can't FlatEngine handle being wrapped in a lock? if bank.send(:engine).is_a? CatEngine::Engine with_lock { yield } else yield end end end end