Module: SurveyorParserQuestionMethods

Defined in:
lib/surveyor/parser.rb

Overview

Question model

Instance Method Summary collapse

Instance Method Details

#parse_and_build(context, args, original_method, reference_identifier) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/surveyor/parser.rb', line 263

def parse_and_build(context, args, original_method, reference_identifier)
  # clear context
  [ :question,
    :dependency,
    :dependency_condition,
    :answer,
    :validation,
    :validation_condition ].each{|k| context.delete k}

  # build and set context
  text = args[0] || "Question"
  hash_args = args[1] || {}
  correct = hash_args.delete :correct
  self.attributes = ({
    :question_group => context[:question_group],
    :reference_identifier => reference_identifier,
    :is_mandatory => context[:default_mandatory],
    :text => text,
    :display_type => (original_method =~ /label|image/ ? original_method : "default"),
    :display_order => context[:survey_section].questions.size }.merge(hash_args))
  context[:survey_section].questions << context[:question] = self

  # keep reference for correct answers
  context[:questions_with_correct_answers][self.reference_identifier] = correct unless self.reference_identifier.blank? or correct.blank?

  # keep reference for dependencies
  unless self.reference_identifier.blank?
    context[:duplicate_references].push "q_#{self.reference_identifier}" if context[:question_references].has_key?(self.reference_identifier)
    context[:question_references][self.reference_identifier] = context[:question]
  end

  # add grid answers
  if context[:question_group] && context[:question_group].display_type == "grid"
    (context[:grid_answers] || []).each do |grid_answer|
      a = context[:question].answers.build(grid_answer.attributes.reject{|k,v| %w(id api_id created_at updated_at).include?(k)})
      context[:answer_references][self.reference_identifier] ||= {} unless self.reference_identifier.blank?
      context[:answer_references][self.reference_identifier][grid_answer.reference_identifier] = a unless self.reference_identifier.blank? or grid_answer.reference_identifier.blank?
    end
  end
end