Module: ActiveRecordSurveyApi::Concerns::Controllers::Answers

Extended by:
ActiveSupport::Concern
Included in:
AnswersController, Concerns::Controllers::Answers
Defined in:
lib/active_record_survey_api/concerns/controllers/answers.rb

Instance Method Summary collapse

Instance Method Details

#createObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/active_record_survey_api/concerns/controllers/answers.rb', line 41

def create
	@answer = new_answer(answer_params)

	begin
		@question.build_answer(@answer)
		@question.survey.save
	rescue Exception => $e
		render json: {
			errors: [
				{
					status: "400",
					code: "CANNOT_MIX_ANSWER_TYPE"
				}
			]
		}, status: :bad_request
	else
		render json: serialize_model(@answer, serializer: ActiveRecordSurveyApi::AnswerSerializer)
	end
end

#destroyObject



34
35
36
37
38
39
# File 'lib/active_record_survey_api/concerns/controllers/answers.rb', line 34

def destroy
	@answer = answer_by_id(params[:id])
	@answer.destroy

	head :no_content
end

#get_next_questionObject



61
62
63
64
65
66
# File 'lib/active_record_survey_api/concerns/controllers/answers.rb', line 61

def get_next_question
	@answer = answer_by_id(params[:answer_id])
	@question = @answer.next_question

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

#indexObject



22
23
24
25
26
# File 'lib/active_record_survey_api/concerns/controllers/answers.rb', line 22

def index
	@answers = all_answers

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


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

def link_next_question
	@answer = answer_by_id(params[:answer_id])
	@question = ActiveRecordSurvey::Node::Question.find(json_params[:question_id])

	if !@answer.next_question.nil?
		@answer.remove_link
	end

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

#showObject



28
29
30
31
32
# File 'lib/active_record_survey_api/concerns/controllers/answers.rb', line 28

def show
	@answer = answer_by_id(params[:id])

	render json: serialize_model(@answer, serializer: ActiveRecordSurveyApi::AnswerSerializer)
end


93
94
95
96
97
98
99
# File 'lib/active_record_survey_api/concerns/controllers/answers.rb', line 93

def unlink_next_question
	@answer = answer_by_id(params[:answer_id])
	@answer.remove_link
	@answer.save

	head :no_content
end

#updateObject



13
14
15
16
17
18
19
20
# File 'lib/active_record_survey_api/concerns/controllers/answers.rb', line 13

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

	@answer = answer_by_id(params[:id])
	@answer.update_attributes(answer_params)

	render json: serialize_model(@answer, serializer: ActiveRecordSurveyApi::AnswerSerializer)
end