Sha256: 58e21eba03d1bc7d24d748814f24d48371c330e03964dce3187c379c9386ad95

Contents?: true

Size: 1.37 KB

Versions: 10

Compression:

Stored size: 1.37 KB

Contents

module Gaku
  class Course < ActiveRecord::Base

    include Notes

    has_many :enrollments,
             class_name: 'Gaku::CourseEnrollment',
             dependent: :destroy

    has_many :students, through: :enrollments

    has_many :course_group_enrollments
    has_many :course_groups, through: :course_group_enrollments

    has_many :class_groups, through: :class_group_course_enrollments
    has_many :class_group_course_enrollments, dependent: :destroy

    has_many :semester_courses, dependent: :destroy
    has_many :semesters, through: :semester_courses

    has_many :exam_schedules

    belongs_to :syllabus
    belongs_to :class_group

    delegate :name, :code, to: :syllabus, prefix: true, allow_nil: true

    accepts_nested_attributes_for :enrollments

    validates :code, presence: true

    scope :without_semester, -> { includes(:semester_courses).where(gaku_semester_courses: { course_id: nil }) }

    def to_s
      if syllabus_name
        "#{syllabus_name}-#{code}"
      else
        code
      end
    end

    def to_selectbox
      [to_s, id]
    end

    def enroll_class_group(class_group)
      unless class_group.blank?
        ActiveRecord::Base.transaction do
          class_group.student_ids.each do |student_id|
            CourseEnrollment.find_or_create_by(student_id: student_id, course_id: id)
         end
        end
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
gaku-0.1.1 core/app/models/gaku/course.rb
gaku_core-0.1.1 app/models/gaku/course.rb
gaku-0.1.0 core/app/models/gaku/course.rb
gaku_core-0.1.0 app/models/gaku/course.rb
gaku_core-0.0.3 app/models/gaku/course.rb
gaku-0.0.3 core/app/models/gaku/course.rb
gaku-0.0.2 core/app/models/gaku/course.rb
gaku_core-0.0.2 app/models/gaku/course.rb
gaku-0.0.1 core/app/models/gaku/course.rb
gaku_core-0.0.1 app/models/gaku/course.rb