Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Questionaire
- =======
- def add_question(text, number, type)
- number ||= @next_question_position
- new_question = questions.build(:text => text, :position_in_survey => number, :answer_type => type)
- next_question_position! and reset_choice_position!
- new_question
- end
- def next_question_position!
- @next_question_position += 1
- end
- def reset_choice_position!
- @next_choice_position = 0
- end
- =======
- def add_choice(text, next_question = @next_question_position)
- next_question ||= @next_question_position #??
- new_choice = current_question.choices.build(
- :text => text,
- :position_in_question => @next_choice_position,
- :next_question_position_in_survey => next_question
- )
- next_choice_position!
- new_choice
- end
- def next_choice_position!
- @next_choice_position += 1
- end
- =========
- def to_s
- lines = []
- questions.each do |question|
- lines << ((question.is_numeric?) ? "#{question.to_s} #{question.tag}" : "#{question.to_s}")
- question.choices.each do |choice|
- lines << _______? ? " #{choice.to_s} #{choice.tag}" : " #{choice.to_s}"
- end
- lines << ""
- end
- lines.join("\n").chop
- end
- def _______?(choice, question)
- choice.next_question_position_in_survey != question.position_in_survey + 1
- end
- ==========
- def question_numbers
- questions.collect { |q| q.number }.sort
- end
- def duplicated_question_numbers
- duplicated_numbers = questions_numbers.uniq - questions_numbers
- if duplicated_numbers.present?
- errors.add_to_base("Existe(m) mais de uma pergunta com o(s) número(s): #{duplicated_numbers.join(", ")}.")
- end
- end
- =========
- # Verifica se não existem números de pergunta faltantes entre a primeira e última pergunta
- def missing_question_numbers
- missing_questions = []
- question_numbers.max.times do |i|
- expected_number = i + 1
- missing_questions << expected_number unless question_numbers.include?(expected_number)
- end
- if missing_questions.present?
- errors.add_to_base("Não existe(m) pergunta(s) com o(s) número(s): #{missing_questions.join(", ")} .")
- end
- end
- =======
- def next_question_numbers
- numbers = []
- questions.each do |question|
- numbers << question.choices.collect { |c| c.next_question_position_in_survey + 1 }
- end
- end
- def unaccessible_questions
- numbers = next_question_numbers
- unreachable = question_numbers.uniq.collect do |n|
- n unless (numbers.include?(n) || n == 1)
- end
- if unreachable.present?
- errors.add_to_base("Não existe caminho possível para a(s) pergunta(s): #{unreachable_questions.join(", ")}.")
- end
- end
- =======
- validate :duplicated_question_numbers,
- :missing_question_numbers,
- :unaccessible_questions
Add Comment
Please, Sign In to add comment