personal-course/app/models/course_assignment.rb

29 lines
1.2 KiB
Ruby

class CourseAssignment
include Mongoid::Document
include Mongoid::Timestamps
include Slug
field :name, as: :slug_title, type: String, localize: true, default: ""
field :deadline, type: DateTime, default: DateTime.now
field :assign_date, type: DateTime, default: DateTime.now
field :detail, type: String, localize: true, default: ""
has_many :course_attachments, :dependent => :destroy, :autosave => true
accepts_nested_attributes_for :course_attachments, :allow_destroy => true
belongs_to :course
scope :enabled_for_student, ->{where(:assign_date.lte=>DateTime.now)}
def display_attachments
self.course_attachments.map{|f|
next if f.file.file.nil?
title = (f.title.blank? ? f.file.file.original_filename : f.title)
"<a href=\"#{f.file.url}\" title=\"#{title}\">#{title}</a>"
}.join("<br>").html_safe
end
def display_deadline
self.deadline.strftime("%Y-%m-%d %H:%M")
end
def display_assign_date
self.assign_date.strftime("%Y-%m-%d %H:%M")
end
def deliver_count
StudentAssignment.where(:course_assignment_id => self.id,:member_profile_id.ne=>nil).count rescue 0
end
end