app/models/section.rb
def objects_fields_in language, fields
    #store the sections_field in language, but also the default language, for fail safe purposes
    fields[:sections_field] ||= {}
    #stores object if language is not the default language. nil otherwise || If language is the default one, the object will be in sections_field
    fields[:sections_field_default] ||= {}
    #store the questions_field in language, but also the default language, for fail safe purposes
    fields[:questions_field] ||= {}
    #stores object if language is not the default language. nil otherwise || If language is the default one, the object will be in questions_field
    fields[:questions_field_default] ||= {}
    fields[:answer_types_field] ||= {}
    fields[:multi_answer_option_field] ||= {}
    fields[:other_text_fields] ||= {}
    fields[:range_answer_option_field] ||= {}
    fields[:rank_answer_option_field] ||= {}
    fields[:matrix_answer_query_field] ||={}
    fields[:matrix_answer_option_field] ||={}
    fields[:matrix_answer_drop_option_field] ||= {}
    self.self_and_descendants.each do |s|
      fields[:sections_field][s.id.to_s] = s.section_fields.find_by_language(language)
      fields[:sections_field_default][s.id.to_s] = fields[:sections_field][s.id.to_s] && fields[:sections_field][s.id.to_s].is_default_language? ? nil : s.section_fields.find_by_is_default_language(true)
      s.questions.find(:all, :include => [:answer_type]).each do |q|
        fields[:questions_field][q.id.to_s] = q.question_fields.find_by_language(language)
        fields[:questions_field_default][q.id.to_s] = fields[:questions_field][q.id.to_s] && fields[:questions_field][q.id.to_s].is_default_language? ? nil : q.question_fields.find_by_is_default_language(true)
        answer_type_identifier = q.answer_type_id.to_s + "_" + q.answer_type_type
        fields[:answer_types_field][answer_type_identifier] =  (q.answer_type.present? && q.answer_type.help_text.present?) ? q.answer_type.value_in(:help_text, language) : fields[:answer_types_field][answer_type_identifier].present? ? fields[:answer_types_field][answer_type_identifier] : nil
        if  q.answer_type.is_a?(MultiAnswer) || q.answer_type.is_a?(RankAnswer) || q.answer_type.is_a?(RangeAnswer)
          atype = q.answer_type_type
          q.answer_type.send(atype.underscore.downcase+"_options").each do |option|
            field = option.send(option.class.to_s.underscore.downcase+"_fields").find_by_language(language)
            if field && field.option_text.present?
              fields[(option.class.to_s.underscore.downcase+"_field").to_sym][option.id.to_s] = field.option_text
            else
              fields[(option.class.to_s.underscore.downcase+"_field").to_sym][option.id.to_s] = option.send(option.class.to_s.underscore.downcase+"_fields").find_by_is_default_language(true).option_text
            end
          end
          #get the "other field" label in the questionnaire language necessary
          if q.answer_type.is_a?(MultiAnswer) && q.answer_type.other_required?
            other_field = q.answer_type.other_fields.present? ? (q.answer_type.other_fields.find_by_language(language) || q.answer_type.other_fields.find_by_is_default_language(true)) : "Other"
            fields[:other_text_fields][q.answer_type.id.to_s] = other_field.is_a?(OtherField) ?  other_field.other_text : other_field
          end
        elsif q.answer_type.is_a?(MatrixAnswer)
          q.answer_type.matrix_answer_queries.each do |maq|
            field = maq.matrix_answer_query_fields.find_by_language(language)
            fields[:matrix_answer_query_field][maq.id.to_s] = field.title.present? ? field.title : maq.matrix_answer_query_fields.find_by_is_default_language(true).title
          end
          q.answer_type.matrix_answer_options.each do |mao|
            field = mao.matrix_answer_option_fields.find_by_language(language)
            fields[:matrix_answer_option_field][mao.id.to_s] = field.title.present? ? field.title : mao.matrix_answer_option_fields.find_by_is_default_language(true).title
          end
          q.answer_type.matrix_answer_drop_options.each do |mado|
            field = mado.matrix_answer_drop_option_fields.find_by_language(language)
            fields[:matrix_answer_drop_option_field][mado.id.to_s] = field.option_text.present? ? field.option_text : mado.matrix_answer_drop_option_fields.find_by_is_default_language(true).option_text
          end
        end
      end
    end
  end
#The purpose of this function is to run the maximum number of queries at the same time, so that rendering of the views isn't
#interrupted  by db access
#Get a section descendants' section_fields. And their questions' question_fields, and the answer type existing options
# And the answer_types help text and the options text in "language".
# The information is stored in the fields hash.
# For sections and questions returns also the fields for the default language.
# Because the section_field object has more than one field (title, description, etc) and so one of those can be empty
# and it will be necessary to store and then use the section_field of the default language.
The purpose of this function is to run the maximum number of queries at the same time, so that rendering of the views isn't
interrupted  by db access
Get a section descendants' section_fields. And their questions' question_fields, and the answer type existing options
And the answer_types help text and the options text in "language".
The information is stored in the fields hash.
For sections and questions returns also the fields for the default language.
Because the section_field object has more than one field (title, description, etc) and so one of those can be empty
and it will be necessary to store and then use the section_field of the default language.
The purpose of this function is to run the maximum number of queries at the same time so that rendering of the views isn ' t interrupted by db access Get a section descendants ' section_fields . And their questions ' question_fields and the answer type existing options And the answer_types help text and the options text in " language " . The information is stored in the fields hash . For sections and questions returns also the fields for the default language . Because the section_field object has more than one field ( title description etc ) and so one of those can be empty and it will be necessary to store and then use the section_field of the default language .
def objects_fields_in language, fields
    fields[:sections_field] ||= {}
    fields[:sections_field_default] ||= {}
    fields[:questions_field] ||= {}
    fields[:questions_field_default] ||= {}
    fields[:answer_types_field] ||= {}
    fields[:multi_answer_option_field] ||= {}
    fields[:other_text_fields] ||= {}
    fields[:range_answer_option_field] ||= {}
    fields[:rank_answer_option_field] ||= {}
    fields[:matrix_answer_query_field] ||={}
    fields[:matrix_answer_option_field] ||={}
    fields[:matrix_answer_drop_option_field] ||= {}
    self.self_and_descendants.each do |s|
      fields[:sections_field][s.id.to_s] = s.section_fields.find_by_language(language)
      fields[:sections_field_default][s.id.to_s] = fields[:sections_field][s.id.to_s] && fields[:sections_field][s.id.to_s].is_default_language? ? nil : s.section_fields.find_by_is_default_language(true)
      s.questions.find(:all, :include => [:answer_type]).each do |q|
        fields[:questions_field][q.id.to_s] = q.question_fields.find_by_language(language)
        fields[:questions_field_default][q.id.to_s] = fields[:questions_field][q.id.to_s] && fields[:questions_field][q.id.to_s].is_default_language? ? nil : q.question_fields.find_by_is_default_language(true)
        answer_type_identifier = q.answer_type_id.to_s + "_" + q.answer_type_type
        fields[:answer_types_field][answer_type_identifier] =  (q.answer_type.present? && q.answer_type.help_text.present?) ? q.answer_type.value_in(:help_text, language) : fields[:answer_types_field][answer_type_identifier].present? ? fields[:answer_types_field][answer_type_identifier] : nil
        if  q.answer_type.is_a?(MultiAnswer) || q.answer_type.is_a?(RankAnswer) || q.answer_type.is_a?(RangeAnswer)
          atype = q.answer_type_type
          q.answer_type.send(atype.underscore.downcase+"_options").each do |option|
            field = option.send(option.class.to_s.underscore.downcase+"_fields").find_by_language(language)
            if field && field.option_text.present?
              fields[(option.class.to_s.underscore.downcase+"_field").to_sym][option.id.to_s] = field.option_text
            else
              fields[(option.class.to_s.underscore.downcase+"_field").to_sym][option.id.to_s] = option.send(option.class.to_s.underscore.downcase+"_fields").find_by_is_default_language(true).option_text
            end
          end
          if q.answer_type.is_a?(MultiAnswer) && q.answer_type.other_required?
            other_field = q.answer_type.other_fields.present? ? (q.answer_type.other_fields.find_by_language(language) || q.answer_type.other_fields.find_by_is_default_language(true)) : "Other"
            fields[:other_text_fields][q.answer_type.id.to_s] = other_field.is_a?(OtherField) ?  other_field.other_text : other_field
          end
        elsif q.answer_type.is_a?(MatrixAnswer)
          q.answer_type.matrix_answer_queries.each do |maq|
            field = maq.matrix_answer_query_fields.find_by_language(language)
            fields[:matrix_answer_query_field][maq.id.to_s] = field.title.present? ? field.title : maq.matrix_answer_query_fields.find_by_is_default_language(true).title
          end
          q.answer_type.matrix_answer_options.each do |mao|
            field = mao.matrix_answer_option_fields.find_by_language(language)
            fields[:matrix_answer_option_field][mao.id.to_s] = field.title.present? ? field.title : mao.matrix_answer_option_fields.find_by_is_default_language(true).title
          end
          q.answer_type.matrix_answer_drop_options.each do |mado|
            field = mado.matrix_answer_drop_option_fields.find_by_language(language)
            fields[:matrix_answer_drop_option_field][mado.id.to_s] = field.option_text.present? ? field.option_text : mado.matrix_answer_drop_option_fields.find_by_is_default_language(true).option_text
          end
        end
      end
    end
  end
def objects_fields_in language , fields fields [ :sections_field ] ||= { } fields [ :sections_field_default ] ||= { } fields [ :questions_field ] ||= { } fields [ :questions_field_default ] ||= { } fields [ :answer_types_field ] ||= { } fields [ :multi_answer_option_field ] ||= { } fields [ :other_text_fields ] ||= { } fields [ :range_answer_option_field ] ||= { } fields [ :rank_answer_option_field ] ||= { } fields [ :matrix_answer_query_field ] ||= { } fields [ :matrix_answer_option_field ] ||= { } fields [ :matrix_answer_drop_option_field ] ||= { } self . self_and_descendants . each do | s | fields [ :sections_field ] [ s . id . to_s ] = s . section_fields . find_by_language ( language ) fields [ :sections_field_default ] [ s . id . to_s ] = fields [ :sections_field ] [ s . id . to_s ] && fields [ :sections_field ] [ s . id . to_s ] . is_default_language? ? nil : s . section_fields . find_by_is_default_language ( true ) s . questions . find ( :all , :include => [ :answer_type ] ) . each do | q | fields [ :questions_field ] [ q . id . to_s ] = q . question_fields . find_by_language ( language ) fields [ :questions_field_default ] [ q . id . to_s ] = fields [ :questions_field ] [ q . id . to_s ] && fields [ :questions_field ] [ q . id . to_s ] . is_default_language? ? nil : q . question_fields . find_by_is_default_language ( true ) answer_type_identifier = q . answer_type_id . to_s + "_" + q . answer_type_type fields [ :answer_types_field ] [ answer_type_identifier ] = ( q . answer_type . present? && q . answer_type . help_text . present? ) ? q . answer_type . value_in ( :help_text , language ) : fields [ :answer_types_field ] [ answer_type_identifier ] . present? ? fields [ :answer_types_field ] [ answer_type_identifier ] : nil if q . answer_type . is_a? ( MultiAnswer ) || q . answer_type . is_a? ( RankAnswer ) || q . answer_type . is_a? ( RangeAnswer ) atype = q . answer_type_type q . answer_type . send ( atype . underscore . downcase + "_options" ) . each do | option | field = option . send ( option . class . to_s . underscore . downcase + "_fields" ) . find_by_language ( language ) if field && field . option_text . present? fields [ ( option . class . to_s . underscore . downcase + "_field" ) . to_sym ] [ option . id . to_s ] = field . option_text else fields [ ( option . class . to_s . underscore . downcase + "_field" ) . to_sym ] [ option . id . to_s ] = option . send ( option . class . to_s . underscore . downcase + "_fields" ) . find_by_is_default_language ( true ) . option_text end end if q . answer_type . is_a? ( MultiAnswer ) && q . answer_type . other_required? other_field = q . answer_type . other_fields . present? ? ( q . answer_type . other_fields . find_by_language ( language ) || q . answer_type . other_fields . find_by_is_default_language ( true ) ) : "Other" fields [ :other_text_fields ] [ q . answer_type . id . to_s ] = other_field . is_a? ( OtherField ) ? other_field . other_text : other_field end elsif q . answer_type . is_a? ( MatrixAnswer ) q . answer_type . matrix_answer_queries . each do | maq | field = maq . matrix_answer_query_fields . find_by_language ( language ) fields [ :matrix_answer_query_field ] [ maq . id . to_s ] = field . title . present? ? field . title : maq . matrix_answer_query_fields . find_by_is_default_language ( true ) . title end q . answer_type . matrix_answer_options . each do | mao | field = mao . matrix_answer_option_fields . find_by_language ( language ) fields [ :matrix_answer_option_field ] [ mao . id . to_s ] = field . title . present? ? field . title : mao . matrix_answer_option_fields . find_by_is_default_language ( true ) . title end q . answer_type . matrix_answer_drop_options . each do | mado | field = mado . matrix_answer_drop_option_fields . find_by_language ( language ) fields [ :matrix_answer_drop_option_field ] [ mado . id . to_s ] = field . option_text . present? ? field . option_text : mado . matrix_answer_drop_option_fields . find_by_is_default_language ( true ) . option_text end end end end end
The purpose of this function is to run the maximum number of queries at the same time, so that rendering of the views isn't
interrupted  by db access
Get a section descendants' section_fields.
The purpose of this function is to run the maximum number of queries at the same time so that rendering of the views isn ' t interrupted by db access Get a section descendants ' section_fields .
#store the sections_field in language, but also the default language, for fail safe purposes #stores object if language is not the default language. nil otherwise || If language is the default one, the object will be in sections_field #store the questions_field in language, but also the default language, for fail safe purposes #stores object if language is not the default language. nil otherwise || If language is the default one, the object will be in questions_field #get the "other field" label in the questionnaire language necessary
language
fields