Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 38bbc10c83
Fetching contributors…

Cannot retrieve contributors at this time

74 lines (59 sloc) 2.457 kB
class Event < ActiveRecord::Base
sanitize :title, :description
notify on: :create,
to: :students,
text: 'A new %{type}, <b>%{title}</b>, was created for %{section} on <b>%{starts_on}</b> from <b>%{time}</b>.',
modal: true
notify on: :update,
to: :students,
text: 'The %{type} for %{section}, <b>%{title}</b>, was just updated.',
modal: true
scope :visible, where(visible: true)
scope :assignments, where(assignment: true).order(:ends_at)
scope :for, lambda {|date_range| where(starts_at: date_range).order(:starts_at)}
belongs_to :section
has_many :grades, dependent: :destroy
has_many :documents, as: :resource
validates_presence_of :title, :starts_at, :ends_at
validate :check_starts_at,
on: :create
validate :check_ends_at
# Assignment-specific validations
validates_numericality_of :points_possible,
if: Proc.new {|event| event.assignment}
validates_numericality_of :weight,
less_than_or_equal_to: Proc.new {|event| 100 - event.section.weight_total + (event.weight_was || 0)},
if: Proc.new {|event| event.assignment}
attr_accessible :visible, :assignment, :title, :description, :starts_at, :ends_at, :points_possible, :weight, :grades_attributes
accepts_nested_attributes_for :grades,
allow_destroy: true,
reject_if: Proc.new {|attributes| !attributes['id'] && attributes['score'].blank?}
def full_title
"#{assignment ? 'DUE: ' : nil}#{title}"
end
def time
time = starts_at.strftime("%-l:%M")
time += starts_at.strftime("%P") unless starts_at.strftime("%P") == ends_at.strftime("%P")
time += ends_at.strftime("-%-l:%M%P")
end
def starts_on
starts_at.to_date
end
def notify_icon
assignment ? 'check' : 'calendar'
end
def notify_text_arguments
{
type: assignment ? 'assignment' : 'event',
starts_on: starts_on.strftime("%a., %b #{starts_on.day.ordinalize}"),
time: time
}
end
protected
def check_starts_at
errors.add(:starts_at, 'must be close to the current time') if starts_at && starts_at < 1.hour.ago
end
def check_ends_at
errors.add(:ends_at, 'must be after start time') if ends_at && starts_at && ends_at < starts_at
end
end
Jump to Line
Something went wrong with that request. Please try again.