Table name: schedules
id :integer not null, primary key name :text description :text start_time :datetime end_time :datetime university_id :integer created_at :datetime updated_at :datetime status :string(255) default("Pending") notified :boolean default(FALSE) deleted_at :datetime comments_count :integer default(0) frequency :string(255) type :string(255) reminder :integer customized_by_id :integer parent_id :integer owner_id :integer grouped :boolean default(FALSE)
Attribute accessor
Attribute accessor
Attribute accessor
Attribute accessor
Attribute accessor
Returns daily repeated schedules.
# File app/models/schedule.rb, line 89 def self.repeated_daily_schedules(start_date, end_date) schedules = [] daily_schedules.each do |schedule| start_date.upto(end_date) do |date| new_schedule = schedule.dup new_schedule.id = schedule.id new_schedule.owner_id = schedule.owner_id new_date_hash = {year: date.year, month: date.month, day: date.day} new_schedule.start_time = schedule.start_time.change(new_date_hash) if new_schedule.end_time new_schedule.end_time = schedule.end_time.change(new_date_hash) end schedules << new_schedule end end schedules end
Returns monthly repeated schedules.
# File app/models/schedule.rb, line 128 def self.repeated_monthly_schedules(start_date, end_date) schedules = [] monthly_schedules.each do |schedule| new_schedule = schedule.dup new_schedule.id = schedule.id new_schedule.owner_id = schedule.owner_id r = Recurrence.new(:every => :month, :on => schedule.start_time.day,:starts => start_date, :interval => :monthly) r.events!.each do |date| if(start_date <= date && end_date >= date) new_date_hash = {year: date.year, month: date.month, day: date.day} new_schedule.start_time = new_schedule.start_time.change(new_date_hash) if new_schedule.end_time new_schedule.end_time = new_schedule.end_time.change(new_date_hash) end schedules << new_schedule end end end schedules end
Returns weekly repeated schedules.
# File app/models/schedule.rb, line 108 def self.repeated_weekly_schedules(start_date, end_date) schedules = [] weekly_schedules.each do |schedule| new_schedule = schedule.dup new_schedule.id = schedule.id new_schedule.owner_id = schedule.owner_id r = Recurrence.new(:every => :week, :starts => start_date, :until => end_date.to_date-1, :on => [schedule.start_time.strftime("%A").downcase.to_sym]) r.events!.each do |date| new_date_hash = {year: date.year, month: date.month, day: date.day} new_schedule.start_time = new_schedule.start_time.change(new_date_hash) if new_schedule.end_time new_schedule.end_time = new_schedule.end_time.change(new_date_hash) end schedules << new_schedule end end schedules end
Returns yearly repeated schedules.
# File app/models/schedule.rb, line 150 def self.repeated_yearly_schedules(start_date, end_date) schedules = [] yearly_schedules.each do |schedule| new_schedule = schedule.dup new_schedule.id = schedule.id new_schedule.owner_id = schedule.owner_id r = Recurrence.new(:every => :year, :starts => start_date, :on => [schedule.start_time.month, schedule.start_time.day]) r.events!.each do |date| if(start_date <= date && end_date >= date) new_date_hash = {year: date.year, month: date.month, day: date.day} new_schedule.start_time = new_schedule.start_time.change(new_date_hash) if new_schedule.end_time new_schedule.end_time = new_schedule.end_time.change(new_date_hash) end schedules << new_schedule end end end schedules end
It finds the upcoming schedule in 1 hour.
# File app/models/schedule.rb, line 172 def self.upcoming_schedules schedules = Schedule.group_schedules.not_notified.where("start_time between ? and ?", Time.now.in_time_zone, Time.now.in_time_zone + 1.hours) schedules.find_each do |schedule| users = schedule.owner comment = users.comments.build(subject: "Upcoming schedule - #{schedule.name}", university_id: schedule.university_id, body: schedule.description, user_id: users.id, commentable_id: schedule.shares.last.shared_to_id, body: schedule.description, commentable_type: "Group") comment.save validate: false schedule.set_notified schedule.save validate: false end end
Returns true when status is accepted
# File app/models/schedule.rb, line 243 def accepted? status == 'Accepted' end
Checks whether the schedule is accepted by user or not.
# File app/models/schedule.rb, line 258 def accepted_by?(user) confirmations.accepted.where(user_id: user.id).exists? end
Checks whether user can delete the schedule or not.
# File app/models/schedule.rb, line 281 def can_delete?(user) if grouped? customized_by?(user) ? true : owner_id == user.id else owner_id == user.id end end
Checks whether schedule completed by specified user or not.
# File app/models/schedule.rb, line 350 def completed_by?(user, date=nil) date = date.to_date if date if frequency? schedule_completions.exists?(user: user, date: date) else schedule_completions.exists?(user: user) end end
To send the schedule notifications to the users in group.
# File app/models/schedule.rb, line 215 def create_group_notification GroupScheduleNotificationWorker.perform_async(id) end
Checks whether the specifird user has customized the schedule or not.
# File app/models/schedule.rb, line 290 def customized_by?(user) customized_by_id == user.id end
Returns array of users who customized the schedule.
# File app/models/schedule.rb, line 336 def customized_by_users if parent_id? User.none else User.where(id: Schedule.where(parent_id: id).select(:customized_by_id)) end end
Returns true when type is event
# File app/models/schedule.rb, line 253 def event? type == 'Event' end
Returns group of schedule user(i.e. parent user).
# File app/models/schedule.rb, line 382 def group grouped end
To mark schedule as completed by specified user.
# File app/models/schedule.rb, line 360 def mark_completed!(user, date=nil) date = date.to_date if date schedule_completion = self.schedule_completions.find_or_create_by(user: user, date: date) end
To mark schedule as uncompleted by specified user.
# File app/models/schedule.rb, line 366 def mark_uncompleted!(user, date=nil) date = date.to_date if date sc = if frequency? self.schedule_completions.find_by(user: user, date: date) else self.schedule_completions.find_by(user: user) end sc.destroy if sc end
It returns the next date and time for schedule reminder.
# File app/models/schedule.rb, line 295 def next_reminder_at case frequency when nil if start_time > Time.now if start_time - reminder.hours < Time.now return Time.now else return start_time - reminder.hours end end when 'Daily' date = Date.today_in_time_zone time = start_time.change(year: date.year, month: date.month, day: date.day) - reminder.hour if time < Time.now date = date + 1.day return start_time.change(year: date.year, month: date.month, day: date.day) - reminder.hour else return start_time.change(year: date.year, month: date.month, day: date.day) - reminder.hour end when 'Weekly' date = Recurrence.new(every: :week, starts: Time.now, on: start_time.wday).first return start_time.change(year: date.year, month: date.month, day: date.day) - reminder.hour when 'Monthly' date = Recurrence.new(every: :month, starts: Time.now, on: start_time.day).first return start_time.change(year: date.year, month: date.month, day: date.day) - reminder.hour when 'Yearly' date = Recurrence.new(every: :year, starts: Time.now, on: [start_time.month, start_time.day]).first return start_time.change(year: date.year, month: date.month, day: date.day) - reminder.hour end end
Returns true when start time is less than current time
# File app/models/schedule.rb, line 268 def passed? start_time <= Time.now end
Returns true when status is pending
# File app/models/schedule.rb, line 238 def pending? status == 'Pending' end
Returns true if schedule is not grouped
# File app/models/schedule.rb, line 210 def personal? !grouped? end
Returns true when status is rejected
# File app/models/schedule.rb, line 248 def rejected? status == 'Rejected' end
Checks whether the schedule is rejected by user.
# File app/models/schedule.rb, line 263 def rejected_by?(user) confirmations.rejected.where(user_id: user.id).exists? end
Calculate the reminder time.
# File app/models/schedule.rb, line 84 def reminder_time start_time - reminder.hours if reminder? end
Delete reminder for schedule.
# File app/models/schedule.rb, line 345 def remove_reminder ScheduleReminderWorker.destroy_reminder(id) end
To reschedule the schedule reminder.
# File app/models/schedule.rb, line 327 def reschedule_reminder if reminder ScheduleReminderWorker.reschedule(id, next_reminder_at) else remove_reminder end end
It checks whether it contains reminder and if present it will send reminder email for schedule
# File app/models/schedule.rb, line 273 def schedule_reminder if reminder time = next_reminder_at ScheduleReminderWorker.perform_at(time, id) if time end end
It set status true if resource type is user.
# File app/models/schedule.rb, line 203 def set_approved if self.personal? self.status = 'Accepted' end end
It set notified true if resource type is group.
# File app/models/schedule.rb, line 184 def set_notified if self.grouped? self.notified = true end end
It checks for date validation if user enter date it converts it to datetime before saving.
# File app/models/schedule.rb, line 220 def switch_date_and_time if self.date self.start_time = Time.zone.parse("#{Date.strptime(self.date, '%m/%d/%Y')} #{self.stime}") self.end_time = Time.zone.parse("#{Date.strptime(self.date, '%m/%d/%Y')} #{self.etime}") end end
Create group schedule exception if parent id is present.
# File app/models/schedule.rb, line 377 def update_confirmation_state self.confirmations.map(&:remove!) end
It returns the group users of schedule.
# File app/models/schedule.rb, line 387 def users shared_with_users.except_user(User.current) end