Class: Surveyform

Inherits:
Survey
  • Object
show all
Defined in:
app/models/surveyform.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SurveyorGui::Models::SurveyMethods

included, #no_responses

Class Method Details

.search(search) ⇒ Object



2
3
4
# File 'app/models/surveyform.rb', line 2

def self.search(search)
  search ? where('title LIKE ?', "%#{search}%") : all
end

Instance Method Details

#_iterate_questions(questions, survey_section_id, question_sorter) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'app/models/surveyform.rb', line 38

def _iterate_questions(questions, survey_section_id, question_sorter)
  questions.each do |question_id|
    question = Question.find(question_id.to_i)
    if question.part_of_group?
      _process_question_group(question, survey_section_id, question_sorter)
    else
      _process_individual_question(question, survey_section_id, question_sorter)
    end
  end
end

#_iterate_survey_sections(serialized_sort_data, question_sorter) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/surveyform.rb', line 15

def _iterate_survey_sections(serialized_sort_data, question_sorter)
  # from data in the form of
  # {"sortable_question<survey_section_id>": {
  #   <question_id>,
  #   <question_id>,
  #   etc.
  # }
  #
  # build a hash in the form of 
  #  {<question_id>: {
  #     "survey_section_id":  <survey_section_id>,
  #     "display_order":      <display_order>
  #     }
  #   }   
  serialized_sort_data.each do |(survey_section_identifier, questions)|
    if survey_section_identifier.include? "sortable_question"
      survey_section_id = /\d+/.match(survey_section_identifier).to_s
      questions.delete("id") #for some reason, question groups introduce values of "id" into array.
      _iterate_questions(questions, survey_section_id, question_sorter)
    end
  end
end

#_process_individual_question(question, survey_section_id, question_sorter) ⇒ Object



56
57
58
# File 'app/models/surveyform.rb', line 56

def _process_individual_question(question, survey_section_id, question_sorter)
  question_sorter.push(question.id.to_s, survey_section_id)
end

#_process_question_group(question, survey_section_id, question_sorter) ⇒ Object



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

def _process_question_group(question, survey_section_id, question_sorter)
  question_group = question.question_group
  question_group.questions.each do |question|
    _process_individual_question(question, survey_section_id, question_sorter)
  end
end

#sort_as_per_array(serialized_sort_data) ⇒ Object



6
7
8
9
10
11
12
# File 'app/models/surveyform.rb', line 6

def sort_as_per_array(serialized_sort_data)
  question_sorter = QuestionSorter.new
  _iterate_survey_sections(serialized_sort_data, question_sorter)
  survey_section_questions = survey_sections.collect(&:questions)
  question_sorter.rearrange(survey_section_questions)

end