Module: ActiveRecordSurveyApi::Concerns::Controllers::Questions

Extended by:
ActiveSupport::Concern
Included in:
QuestionsController, Concerns::Controllers::Questions
Defined in:
lib/active_record_survey_api/concerns/controllers/questions.rb

Instance Method Summary collapse

Instance Method Details

#createObject



54
55
56
57
58
59
60
61
62
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 54

def create
	question_params.delete(:type) unless question_params[:type].nil?  # cannot set type through create

	@question = new_question(question_params)
	@question.survey = @survey
	@question.save

	render json: serialize_model(@question, serializer: ActiveRecordSurveyApi::QuestionSerializer)
end

#destroyObject



23
24
25
26
27
28
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 23

def destroy
	@question = question_by_id(params[:id])
	@question.destroy

	head :no_content
end

#get_next_questionObject



64
65
66
67
68
69
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 64

def get_next_question
	@question = question_by_id(params[:question_id])
	@questions = @question.next_questions

	render json: serialize_models(@questions, serializer: ActiveRecordSurveyApi::QuestionSerializer, meta: { total: @questions.length })
end

#indexObject



11
12
13
14
15
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 11

def index
	@questions = all_questions

	render json: serialize_models(@questions, serializer: ActiveRecordSurveyApi::QuestionSerializer, meta: { total: @questions.length })
end


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 71

def link_next_question
	@question_from = question_by_id(params[:question_id])
	@question_to = question_by_id(json_params[:question_id])

	if @question_from.next_questions.length > 0
		@question_from.remove_link
	end

	begin
		@question_from.build_link(@question_to)
		@question_from.survey.save
	rescue Exception => $e
		render json: {
			errors: [
				{
					status: "508",
					code: "LOOP_DETECTED"
				}
			]
		}, status: :loop_detected
	else
		head :no_content
	end
end

#showObject



17
18
19
20
21
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 17

def show
	@question = question_by_id(params[:id])

	render json: serialize_model(@question, serializer: ActiveRecordSurveyApi::QuestionSerializer)
end


96
97
98
99
100
101
102
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 96

def unlink_next_question
	@question = question_by_id(params[:question_id])
	@question.remove_link
	@question.survey.save

	head :no_content
end

#updateObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 30

def update
	@question = question_by_id(params[:id])
	if !question_params[:type].nil?
		begin
			@question.update_question_type(question_params[:type].to_s.constantize)
			@question.survey.save
		rescue Exception => $e
			render json: {
				errors: [
					{
						status: "422",
						code: "422"
					}
				]
			}, status: :loop_detected
			return
		end
	end

	@question.update_attributes(question_params.except(:type))

	render json: serialize_model(@question, serializer: ActiveRecordSurveyApi::QuestionSerializer)
end