class Schedule

Schema Information

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)

Attributes

date[RW]

Attribute accessor

etime[RW]

Attribute accessor

schedule_type[RW]

Attribute accessor

stime[RW]

Attribute accessor

user_emails[RW]

Attribute accessor

Public Class Methods

find_by_shared_to(object, *opts) click to toggle source

find the schedule with repsect to the shared objects

# File app/models/schedule.rb, line 191
def self.find_by_shared_to(object, *opts)
  if object.is_a? Group
    relation = joins("LEFT OUTER JOIN shares s ON s.shareable_id = #{self.table_name}.id")
    .where("s.shareable_type =? and s.shared_to_type=? and s.shared_to_id = ?", self, object.class.base_class, object.id)
  else
    relation = joins("LEFT OUTER JOIN shares s ON s.shareable_id = #{self.table_name}.id")
    .where("schedules.owner_id = ? OR (s.shareable_type =? AND ((s.shared_to_type=? and s.shared_to_id = ?) OR (s.shared_to_type= 'Group' and s.shared_to_id IN (#{object.groups.select(:id).to_sql}))))", object.id, 'Schedule', object.class.base_class, object.id)
   end
  relation
 end
repeated_daily_schedules(start_date, end_date) click to toggle source

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
repeated_monthly_schedules(start_date, end_date) click to toggle source

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
repeated_weekly_schedules(start_date, end_date) click to toggle source

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
repeated_yearly_schedules(start_date, end_date) click to toggle source

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
upcoming_schedules() click to toggle source

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

Public Instance Methods

accepted?() click to toggle source

Returns true when status is accepted

# File app/models/schedule.rb, line 243
def accepted?
  status == 'Accepted'
end
accepted_by?(user) click to toggle source

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
can_delete?(user) click to toggle source

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
completed_by?(user, date=nil) click to toggle source

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
create_group_notification() click to toggle source

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
customized_by?(user) click to toggle source

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
customized_by_users() click to toggle source

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
event?() click to toggle source

Returns true when type is event

# File app/models/schedule.rb, line 253
def event?
  type == 'Event'
end
group() click to toggle source

Returns group of schedule user(i.e. parent user).

# File app/models/schedule.rb, line 382
def group
  grouped
end
mark_completed!(user, date=nil) click to toggle source

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
mark_uncompleted!(user, date=nil) click to toggle source

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
next_reminder_at() click to toggle source

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
passed?() click to toggle source

Returns true when start time is less than current time

# File app/models/schedule.rb, line 268
def passed?
  start_time <= Time.now
end
pending?() click to toggle source

Returns true when status is pending

# File app/models/schedule.rb, line 238
def pending?
  status == 'Pending'
end
personal?() click to toggle source

Returns true if schedule is not grouped

# File app/models/schedule.rb, line 210
def personal?
  !grouped?
end
rejected?() click to toggle source

Returns true when status is rejected

# File app/models/schedule.rb, line 248
def rejected?
  status == 'Rejected'
end
rejected_by?(user) click to toggle source

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
reminder_time() click to toggle source

Calculate the reminder time.

# File app/models/schedule.rb, line 84
def reminder_time
  start_time - reminder.hours if reminder?
end
remove_reminder() click to toggle source

Delete reminder for schedule.

# File app/models/schedule.rb, line 345
def remove_reminder
  ScheduleReminderWorker.destroy_reminder(id)
end
reschedule_reminder() click to toggle source

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
schedule_reminder() click to toggle source

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
set_approved() click to toggle source

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
set_notified() click to toggle source

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
share_with!(emails) click to toggle source

To get the email of the users to whom the schedule is shared

# File app/models/schedule.rb, line 228
def share_with!(emails)
  users = User.where(email: emails)
  @invitee_emails = emails - users.map(&:email)
  self.shares = users.map do |u|
    Share.new(shareable: self, user_id: User.current.id, shared_to: u)
  end
  Invite.invite_users(@invitee_emails, self) if @invitee_emails.present?
end
switch_date_and_time() click to toggle source

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
update_confirmation_state() click to toggle source

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
users() click to toggle source

It returns the group users of schedule.

# File app/models/schedule.rb, line 387
def users
  shared_with_users.except_user(User.current)
end