From d3fc6e59562ea898f91c77f8ca976b4d939276c6 Mon Sep 17 00:00:00 2001 From: Bernie Chiu Date: Wed, 23 Oct 2013 17:06:52 +0800 Subject: [PATCH] Add import on radiogroups on file change --- .../survey/back_end/surveys_controller.rb | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb b/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb index 23729bd03..e3da9257a 100644 --- a/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb +++ b/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb @@ -109,6 +109,64 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController question.update_attributes(title: titles[index]) end + # Modify multiline options if changed + @survey_questions.each_with_index do |question, index| + + case question.type + when ::SurveyQuestion::Radiogroup + radio_groups = [] + spreadsheet_radiogroups_lines = + question.survey_question_options.count * question.survey_question_radiogroups.count + 2 + + # Grab each radiogroups line for update + (2...spreadsheet_radiogroups_lines).each do |line| + radio_groups << @spreadsheet.row(line)[index * 2] + end + + # Parse the needed info in the array + radio_titles = [] + radio_options = [] + radio_groups.each do |group| + option_with_radio = group.split(' - ') + radio_titles << option_with_radio[0] + radio_options << option_with_radio[1] + end + + # Update the spreadsheet info to the DB + groups_of_options = [] + groups_of_radios = [] + radio_titles.each_slice(question.survey_question_radiogroups.count) do |options| + groups_of_options << options + end + + radio_options.each_slice(question.survey_question_radiogroups.count) do |options| + groups_of_radios << options + end + + # Update option names + question.survey_question_options.each_with_index do |option, index| + groups_of_options[index].each do |modified_option| + if option.name_translations["zh_tw"] != modified_option + option.update_attributes(name: modified_option) + break + end + end + end + + # Update radio names + question.survey_question_radiogroups.each_with_index do |option, index| + groups_of_radios.each do |radios| + if radios[index] != option.name_translations["zh_tw"] + binding.pry + option.update_attributes(name: radios[index]) + break + end + end + end + + end + end + # @start_row = 2 # @survey.survey_answers.destroy # @survey_questions.each_with_index do |question,index|