Sha256: 01f1bd7a91e228cfe9c3c638b51a18fc8e6b27d15aabc958fc7d2c8ce88e0555

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

module Gaku
  class ClassGroups::StudentsController < GakuController

    load_and_authorize_resource :class_group, class: ClassGroup
    load_and_authorize_resource :student,
                                through: :class_group,
                                class: Student

    inherit_resources
    respond_to :js, only: %i(new destroy enroll_student)

    def new
      @class_group = ClassGroup.find(params[:class_group_id])
      @students = Student.includes([:addresses, :class_groups, :class_group_enrollments]).all
    end

    def enroll_student
      unless params[:student_ids].blank?
        @class_group = ClassGroup.find(params[:class_group_enrollment][:class_group_id])
        student_ids = params[:student_ids].split(',')
        enroll_student_ids(student_ids)
      end
    end

    def destroy
      @class_group_enrollment = ClassGroupEnrollment.find(params[:class_group_enrollment])
      @class_group = ClassGroup.find(@class_group_enrollment.class_group_id)
      @class_group_enrollment.destroy
    end

    private

    def enroll_student_ids(student_ids)
      @students = []
      student_ids.each  do |student_id|
        student = Student.find(student_id)
        enrollment = ClassGroupEnrollment.create!(class_group_id: params[:class_group_enrollment][:class_group_id], student_id: student.id)
        @students << enrollment.student
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
gaku_core-0.0.3 app/controllers/gaku/class_groups/students_controller.rb
gaku-0.0.3 core/app/controllers/gaku/class_groups/students_controller.rb
gaku-0.0.2 core/app/controllers/gaku/class_groups/students_controller.rb
gaku_core-0.0.2 app/controllers/gaku/class_groups/students_controller.rb
gaku-0.0.1 core/app/controllers/gaku/class_groups/students_controller.rb
gaku_core-0.0.1 app/controllers/gaku/class_groups/students_controller.rb