Guest User

Untitled

a guest
Jul 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. Questionaire
  2.  
  3. =======
  4.  
  5. def add_question(text, number, type)
  6. number ||= @next_question_position
  7. new_question = questions.build(:text => text, :position_in_survey => number, :answer_type => type)
  8. next_question_position! and reset_choice_position!
  9.  
  10. new_question
  11. end
  12.  
  13. def next_question_position!
  14. @next_question_position += 1
  15. end
  16.  
  17. def reset_choice_position!
  18. @next_choice_position = 0
  19. end
  20.  
  21. =======
  22.  
  23. def add_choice(text, next_question = @next_question_position)
  24. next_question ||= @next_question_position #??
  25.  
  26. new_choice = current_question.choices.build(
  27. :text => text,
  28. :position_in_question => @next_choice_position,
  29. :next_question_position_in_survey => next_question
  30. )
  31.  
  32. next_choice_position!
  33. new_choice
  34. end
  35.  
  36. def next_choice_position!
  37. @next_choice_position += 1
  38. end
  39.  
  40. =========
  41.  
  42. def to_s
  43. lines = []
  44.  
  45. questions.each do |question|
  46. lines << ((question.is_numeric?) ? "#{question.to_s} #{question.tag}" : "#{question.to_s}")
  47.  
  48. question.choices.each do |choice|
  49. lines << _______? ? " #{choice.to_s} #{choice.tag}" : " #{choice.to_s}"
  50. end
  51.  
  52. lines << ""
  53. end
  54.  
  55. lines.join("\n").chop
  56. end
  57.  
  58. def _______?(choice, question)
  59. choice.next_question_position_in_survey != question.position_in_survey + 1
  60. end
  61.  
  62. ==========
  63.  
  64. def question_numbers
  65. questions.collect { |q| q.number }.sort
  66. end
  67.  
  68. def duplicated_question_numbers
  69. duplicated_numbers = questions_numbers.uniq - questions_numbers
  70.  
  71. if duplicated_numbers.present?
  72. errors.add_to_base("Existe(m) mais de uma pergunta com o(s) número(s): #{duplicated_numbers.join(", ")}.")
  73. end
  74. end
  75.  
  76. =========
  77.  
  78. # Verifica se não existem números de pergunta faltantes entre a primeira e última pergunta
  79. def missing_question_numbers
  80. missing_questions = []
  81.  
  82. question_numbers.max.times do |i|
  83. expected_number = i + 1
  84. missing_questions << expected_number unless question_numbers.include?(expected_number)
  85. end
  86.  
  87. if missing_questions.present?
  88. errors.add_to_base("Não existe(m) pergunta(s) com o(s) número(s): #{missing_questions.join(", ")} .")
  89. end
  90. end
  91.  
  92. =======
  93. def next_question_numbers
  94. numbers = []
  95. questions.each do |question|
  96. numbers << question.choices.collect { |c| c.next_question_position_in_survey + 1 }
  97. end
  98. end
  99.  
  100. def unaccessible_questions
  101. numbers = next_question_numbers
  102.  
  103. unreachable = question_numbers.uniq.collect do |n|
  104. n unless (numbers.include?(n) || n == 1)
  105. end
  106.  
  107. if unreachable.present?
  108. errors.add_to_base("Não existe caminho possível para a(s) pergunta(s): #{unreachable_questions.join(", ")}.")
  109. end
  110. end
  111.  
  112. =======
  113.  
  114. validate :duplicated_question_numbers,
  115. :missing_question_numbers,
  116. :unaccessible_questions
Add Comment
Please, Sign In to add comment