Class: SurveySection

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Surveyor::Models::SurveySectionMethods
Defined in:
lib/surveyor/unparser.rb,
app/models/survey_section.rb

Instance Method Summary collapse

Methods included from Surveyor::Models::SurveySectionMethods

#default_args, #initialize, #questions_and_groups, #translation

Instance Method Details

#unparse(dsl) ⇒ Object

block



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/surveyor/unparser.rb', line 33

def unparse(dsl)
  with_defaults = SurveySection.new(:title => title)
  attrs = self.attributes.delete_if{|k,v| with_defaults[k] == v or %w(created_at updated_at id survey_id).include? k}.symbolize_keys!
  group_questions = []
  dsl << "  section \"#{title}\""
  dsl << (attrs.blank? ? " do\n" : ", #{attrs.inspect.gsub(/\{|\}/, "")} do\n")
  questions.each_with_index do |question, index|
    if question.solo?
      question.unparse(dsl)
    else # gather up the group questions
      group_questions << question
      if (index + 1 >= questions.size) or (question.question_group != questions[index + 1].question_group)
        # this is the last question of the section, or the group
        question.question_group.unparse(dsl)
      end
      group_questions = []
    end
  end
  dsl << "  end\n"
end