Class: Course::Survey::ResponsesController

Inherits:
Controller show all
Defined in:
app/controllers/course/survey/responses_controller.rb

Instance Method Summary collapse

Methods included from ApplicationSignInCallbacksConcern

#sign_in

Methods included from ApplicationPaginationConcern

#page_param

Methods included from ApplicationAnnouncementsConcern

#global_announcements, #unread_global_announcements

Methods included from ApplicationInstanceUserConcern

#current_instance_user

Methods included from ApplicationUserTimeZoneConcern

#set_time_zone

Methods included from ApplicationUserConcern

#handle_access_denied, #publicly_accessible?

Methods included from ApplicationComponentsConcern

#handle_component_not_found

Instance Method Details

#createObject


15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/course/survey/responses_controller.rb', line 15

def create
  if current_course_user
    build_response
    @response.save!
    render_response_json
  else
    render json: { error: t('course.survey.responses.no_course_user') }, status: :bad_request
  end
rescue ActiveRecord::RecordInvalid => error
  handle_create_error(error)
end

#editObject

Raises:

  • (CanCan::AccessDenied)

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/course/survey/responses_controller.rb', line 35

def edit
  raise CanCan::AccessDenied if cannot?(:submit, @response) && cannot?(:modify, @response)

  respond_to do |format|
    format.html { render 'course/survey/surveys/index' }
    format.json do
      @response.build_missing_answers
      if @response.save
        render_response_json
      else
        head :internal_server_error
      end
    end
  end
end

#indexObject


5
6
7
8
9
10
11
12
13
# File 'app/controllers/course/survey/responses_controller.rb', line 5

def index
  authorize!(:manage, @survey)
  respond_to do |format|
    format.html { render 'course/survey/surveys/index' }
    format.json do
      @course_students = current_course.course_users.students.order_alphabetically
    end
  end
end

#showObject


27
28
29
30
31
32
33
# File 'app/controllers/course/survey/responses_controller.rb', line 27

def show
  authorize!(:read_answers, @response)
  respond_to do |format|
    format.html { render 'course/survey/surveys/index' }
    format.json { render_response_json }
  end
end

#unsubmitObject


66
67
68
69
70
71
72
73
# File 'app/controllers/course/survey/responses_controller.rb', line 66

def unsubmit
  @response.unsubmit
  if @response.save
    render_response_json
  else
    head :bad_request
  end
end

#updateObject


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/course/survey/responses_controller.rb', line 51

def update
  if params[:response][:submit]
    authorize!(:submit, @response)
    @response.submit
  else
    authorize!(:modify, @response)
  end

  if @response.update(response_update_params)
    render_response_json
  else
    render json: { errors: @response.errors }, status: :bad_request
  end
end