course.rb 901 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. class Course < ApplicationRecord
  2. belongs_to :school
  3. validates :school_id, presence: true
  4. validates :name, presence: true, length: { maximum: 255 }
  5. validates :starts_on, presence: true
  6. validates :ends_on, presence: true
  7. validate :date_order
  8. has_many :course_participations, dependent: :destroy
  9. has_many :users, through: :course_participations
  10. has_many :lectures
  11. has_many :assignments
  12. has_many :data_files, as: :repository
  13. def can_download_files?(user)
  14. self.users.include?(user)
  15. end
  16. def can_upload_files?(user)
  17. self.users.merge(CourseParticipation.teachers).include?(user)
  18. end
  19. has_many :news_posts, as: :news_feed
  20. def can_post_news?(user)
  21. self.users.merge(CourseParticipation.teachers).include?(user)
  22. end
  23. private
  24. def date_order
  25. if ends_on < starts_on
  26. errors.add(:ends_on, "should come after starts_on")
  27. end
  28. end
  29. end