Module: Qwester::QuestionnairesHelper

Defined in:
app/helpers/qwester/questionnaires_helper.rb

Instance Method Summary collapse

Instance Method Details

#answer_checked(answer) ⇒ Object



25
26
27
# File 'app/helpers/qwester/questionnaires_helper.rb', line 25

def answer_checked(answer)
  answer_store_answers.include?(answer) || params_includes_answer(answer)
end

#answer_store_answersObject



29
30
31
32
# File 'app/helpers/qwester/questionnaires_helper.rb', line 29

def answer_store_answers
  answer_store = get_qwester_answer_store
  answer_store ? answer_store.answers : []
end

#params_includes_answer(answer) ⇒ Object

params should be of form: “question_id”=>href=""1"">answer_ids“=>}



35
36
37
38
39
40
41
# File 'app/helpers/qwester/questionnaires_helper.rb', line 35

def params_includes_answer(answer)
  question_ids = params[:question_id]
  return nil unless question_ids.kind_of? Hash
  answers = question_ids[answer.question_id.to_s]
  return nil unless answers.kind_of? Hash
  answers.values.flatten.include?(answer.id.to_s)
end

#qwester_answers_selection_list(question, list_class = 'qwester_options') ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/qwester/questionnaires_helper.rb', line 3

def qwester_answers_selection_list(question, list_class = 'qwester_options')
  button_name = "question_id[#{question.id}][answer_ids][]"
  answers = question.answers
  buttons = answers.collect do |answer|
    if question.multi_answer?
      check_box_id = "question_id[#{question.id}][answer_ids][#{answer.id}]"
      button = check_box_tag(
                 check_box_id, 
                 answer.id, 
                 answer_checked(answer), 
                 name: button_name
               )
    else
      button = radio_button_tag(button_name, answer.id, answer_checked(answer))
    end
    id = button.match(/id=\"(\w+)\"/)[1]
    text = label_tag(id, answer.value)
    ('li', "#{button}#{text}".html_safe)
  end
  ('ul', buttons.join.html_safe, :class => list_class)
end