Class: Qe::QuestionSet

Inherits:
Object
  • Object
show all
Defined in:
app/models/qe/question_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements, answer_sheet) ⇒ QuestionSet

associate answers from database with a set of elements



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/qe/question_set.rb', line 9

def initialize(elements, answer_sheet)
  @elements = elements
  @answer_sheet = answer_sheet

  @questions = elements.select { |e| e.question? }

  # answers = @answer_sheet.answers_by_question

  @questions.each do |question|
    question.answers = question.responses(answer_sheet) #answers[question.id]
  end    
  @questions
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



6
7
8
# File 'app/models/qe/question_set.rb', line 6

def elements
  @elements
end

Instance Method Details

#any_questions?Boolean

def valid?

valid = true
@questions.each do |question|
  valid = false unless question.valid_response?  # run through ALL questions
end
valid

end

Returns:

  • (Boolean)


44
45
46
# File 'app/models/qe/question_set.rb', line 44

def any_questions?
  @questions.length > 0
end

#post(params, answer_sheet) ⇒ Object

update with responses from form



24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/qe/question_set.rb', line 24

def post(params, answer_sheet)
  questions_indexed = @questions.index_by {|q| q.id}
  
  # loop over form values
  params ||= {}
  params.each do |question_id, response|
    next if questions_indexed[question_id.to_i].nil? # the rare case where a question was removed after the app was opened.
    # update each question with the posted response
    questions_indexed[question_id.to_i].set_response(posted_values(response), answer_sheet)
  end
end

#saveObject



48
49
50
51
52
53
54
# File 'app/models/qe/question_set.rb', line 48

def save
  AnswerSheet.transaction do
    @questions.each do |question|
      question.save_response(@answer_sheet)
    end
  end
end