lib/survey_gizmo/api/question.rb in survey-gizmo-ruby-4.1.0 vs lib/survey_gizmo/api/question.rb in survey-gizmo-ruby-5.0.2
- old
+ new
@@ -1,5 +1,7 @@
+require 'survey_gizmo/api/option'
+
module SurveyGizmo; module API
# @see SurveyGizmo::Resource::ClassMethods
class Question
include SurveyGizmo::Resource
include SurveyGizmo::MultilingualTitle
@@ -8,39 +10,49 @@
attribute :type, String
attribute :description, String
attribute :shortname, String
attribute :properties, Hash
attribute :after, Integer
+ attribute :options, Array[Option]
attribute :survey_id, Integer
attribute :page_id, Integer, default: 1
attribute :sub_question_skus, Array
attribute :parent_question_id, Integer
alias_attribute :_subtype, :type
- route '/survey/:survey_id/surveyquestion/:id', via: :get
- route '/survey/:survey_id/surveypage/:page_id/surveyquestion', via: :create
- route '/survey/:survey_id/surveypage/:page_id/surveyquestion/:id', via: [:update, :delete]
+ @route = {
+ get: '/survey/:survey_id/surveyquestion/:id',
+ create: '/survey/:survey_id/surveypage/:page_id/surveyquestion',
+ update: '/survey/:survey_id/surveypage/:page_id/surveyquestion/:id'
+ }
+ @route[:delete] = @route[:update]
def survey
@survey ||= Survey.first(id: survey_id)
end
def options
- @options ||= Option.all(survey_id: survey_id, page_id: page_id, question_id: id, all_pages: true)
+ return parent_question.options.dup.each { |o| o.question_id = id } if parent_question
+
+ @options ||= Option.all(children_params.merge(all_pages: true)).to_a
+ @options.each { |o| o.attributes = children_params }
end
def parent_question
- @parent_question ||= parent_question_id ? Question.first(survey_id: survey_id, id: parent_question_id) : nil
+ return nil unless parent_question_id
+ @parent_question ||= Question.first(survey_id: survey_id, id: parent_question_id)
end
def sub_questions
- @sub_questions ||= sub_question_skus.map { |subquestion_id| Question.first(survey_id: survey_id, id: subquestion_id) }
- .each { |subquestion| subquestion.parent_question_id = id }
- end
-
- # @see SurveyGizmo::Resource#to_param_options
- def to_param_options
- { id: self.id, survey_id: self.survey_id, page_id: self.page_id }
+ @sub_questions ||= sub_question_skus.map do |sku|
+ # As of 2015-12-23, the sub_question_skus attribute can either contain an array of integers if no shortname (alias)
+ # was set for any question, or an array of [String, Integer] with the String corresponding to the subquestion
+ # shortname and the integer corresponding to the subquestion id if at least one shortname was set.
+ sku = sku[1] if sku.is_a?(Array)
+ subquestion = Question.first(survey_id: survey_id, id: sku)
+ subquestion.parent_question_id = id
+ subquestion
+ end
end
end
end; end