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



37
38
39
40
41
42
43
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 37

def 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



45
46
47
48
49
50
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 45

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


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 52

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


77
78
79
80
81
82
83
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 77

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
# File 'lib/active_record_survey_api/concerns/controllers/questions.rb', line 30

def update
  @question = question_by_id(params[:id])
  @question.update_attributes(question_params)

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