class Notification

Schema Information

Table name: activities

id             :integer          not null, primary key
trackable_id   :integer
trackable_type :string(255)
owner_id       :integer
owner_type     :string(255)
key            :string(255)
parameters     :text
recipient_id   :integer
recipient_type :string(255)
created_at     :datetime
updated_at     :datetime
read           :boolean          default(FALSE)
type           :string(255)
university_id     :integer
group_id       :integer
comments_count :integer          default(0)

Public Class Methods

unapproved_event_notifications(user) click to toggle source

Returns all unresponded group events notifications.

# File app/models/notification.rb, line 56
def self.unapproved_event_notifications(user)
  group_notifications = []
  user.unresponded_group_events.unapproved.each do |event|
    group_notifications << Notification.new(trackable_type: 'Event', trackable_id: event.id, owner_id: event.owner_id, owner_type: 'User', key: 'group_event.create', created_at: event.created_at)
  end
  group_notifications
end

Public Instance Methods

accepted?() click to toggle source

It sets the notification response to Accepted.

# File app/models/notification.rb, line 86
def accepted?
  accepted_at.present?
end
accepted_at() click to toggle source

Returns the date when it is accepted.

# File app/models/notification.rb, line 96
def accepted_at
  case trackable_type
    when 'Schedule'
      trackable.confirmations.accepted.where(user: recipient).first.try(:created_at)
    when 'ActsAsFriendable::Friendship'
      trackable.approved? ? trackable.updated_at : nil
    else
      false
  end
end
create_activity() click to toggle source

To call super method to create activity.

Calls superclass method
# File app/models/notification.rb, line 51
def create_activity
  super
end
increase_user_notifications_count() click to toggle source

To increment the user notification count.

# File app/models/notification.rb, line 30
def increase_user_notifications_count
  if recipient.is_a? User
    recipient.update_columns(cached_notification_count: recipient.cached_notification_count + 1)
  end
end
owner() click to toggle source

It returns the owner of notification.

Calls superclass method
# File app/models/notification.rb, line 65
def owner
  unless super
    User.new(name: "anonymous")
  else
    super
  end
end
pending?() click to toggle source

It sets the notification response to nil if it is pending.

# File app/models/notification.rb, line 74
def pending?
  case trackable_type
    when 'Schedule'
      !trackable.confirmations.where(user: recipient).exists?
    when 'ActsAsFriendable::Friendship'
      trackable.try(:approved).nil?
    else
      true
  end
end
rejected?() click to toggle source

It sets the notification response to Rejected.

# File app/models/notification.rb, line 91
def rejected?
  rejected_at.present?
end
rejected_at() click to toggle source

Returns the date when it is rejected.

# File app/models/notification.rb, line 108
def rejected_at
  case trackable_type
    when 'Schedule'
      trackable.confirmations.rejected.where(user: recipient).first.try(:created_at)
    when 'ActsAsFriendable::Friendship'
      trackable.approved.nil? ? nil : (!trackable.approved? ? trackable.updated_at : nil)
    else
      false
  end
end
trigger_pusher_event() click to toggle source

Trigger pusher event to send notification.

# File app/models/notification.rb, line 37
def trigger_pusher_event
  if Configurations::Pusher.enabled?
    client = ActsAsTenant.current_tenant.pusher_configuration.client
    data = {type: self.key}
    if self.trackable_type == 'TutoringSession'
      client["private-user-#{owner_id}"].trigger_async('new_notification', data.merge!(trackable.for_appointment_sidebar(User.current, owner)))
      client["private-user-#{recipient_id}"].trigger_async('new_notification', data.merge(trackable.for_appointment_sidebar(User.current, recipient)).merge(count: recipient.cached_notification_count))
    else
      client["private-user-#{recipient_id}"].trigger_async('new_notification', data.merge(count: recipient.cached_notification_count))
    end
  end
end