Class: ActivityNotification::Notification

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Common, NotificationApi, Renderable
Defined in:
lib/activity_notification/models/notification.rb

Overview

Notification model implementation generated by ActivityNotification.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NotificationApi

available_options, #group_member?, #group_member_count, group_member_exists?, #group_member_exists?, #group_member_notifier_count, #group_member_notifier_exists?, #group_notification_count, #group_notifier_count, #group_owner?, #latest_group_member, #notifiable_path, notify, notify_all, notify_to, #open!, open_all_of, #opened?, send_batch_notification_email, #send_notification_email, #unopened?

Methods included from Common

#printable_name, #printable_type, #resolve_value, #to_class_name, #to_resource_name, #to_resources_name

Methods included from Renderable

#layout_path, #partial_path, #prepare_locals, #prepare_parameters, #render, #text

Class Method Details

.all_index!ActiveRecord_AssociationRelation<Notificaion>

Selects all notification index.

ActivityNotification::Notification.all_index!

is defined same as

ActivityNotification::Notification.group_owners_only.latest_order

Examples:

Get all notification index of the @user

@notifications = @user.notifications.all_index!
@notifications = @user.notifications.group_owners_only.latest_order

Parameters:

  • reverse (Boolean)

    If notification index will be ordered as earliest first

  • with_group_members (Boolean)

    If notification index will include group members

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



71
72
73
74
# File 'lib/activity_notification/models/notification.rb', line 71

scope :all_index!,                        ->(reverse = false, with_group_members = false) {
  target_index = with_group_members ? self : group_owners_only
  reverse ? target_index.earliest_order : target_index.latest_order
}

.filtered_by_groupActiveRecord_AssociationRelation<Notificaion>

Selects filtered notifications by group instance.

Examples:

Get filtered unopened notificatons of the @user for @article as group

@notifications = @user.notifications.unopened_only.filtered_by_group(@article)

Parameters:

  • group (Object)

    Group instance for filter

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



176
# File 'lib/activity_notification/models/notification.rb', line 176

scope :filtered_by_group,                 ->(group) { where(group: group) }

.filtered_by_instanceActiveRecord_AssociationRelation<Notificaion>

Selects filtered notifications by notifiable instance.

Examples:

Get filtered unopened notificatons of the @user for @comment as notifiable

@notifications = @user.notifications.unopened_only.filtered_by_instance(@comment)

Parameters:

  • notifiable (Object)

    Notifiable instance for filter

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



160
# File 'lib/activity_notification/models/notification.rb', line 160

scope :filtered_by_instance,              ->(notifiable) { where(notifiable: notifiable) }

.filtered_by_keyActiveRecord_AssociationRelation<Notificaion>

Selects filtered notifications by key.

Examples:

Get filtered unopened notificatons of the @user with key ‘comment.reply’

@notifications = @user.notifications.unopened_only.filtered_by_key('comment.reply')

Parameters:

  • key (String)

    Key of the notification for filter

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



184
# File 'lib/activity_notification/models/notification.rb', line 184

scope :filtered_by_key,                   ->(key) { where(key: key) }

.filtered_by_optionsActiveRecord_AssociationRelation<Notificaion>

Selects filtered notifications by notifiable_type, group or key with filter options.

Examples:

Get filtered unopened notificatons of the @user for Comment notifiable class

@notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_type: 'Comment' })

Get filtered unopened notificatons of the @user for @article as group

@notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_group: @article })

Get filtered unopened notificatons of the @user for Article instance id=1 as group

@notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_group_type: 'Article', filtered_by_group_id: '1' })

Get filtered unopened notificatons of the @user with key ‘comment.reply’

@notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_key: 'comment.reply' })

Get filtered unopened notificatons of the @user for Comment notifiable class with key ‘comment.reply’

@notifications = @user.notifications.unopened_only.filtered_by_options({ filtered_by_type: 'Comment', filtered_by_key: 'comment.reply' })

Get custom filtered notificatons of the @user

@notifications = @user.notifications.unopened_only.filtered_by_options({ custom_filter: ["created_at >= ?", time.hour.ago] })

Parameters:

  • options (Hash)

    Options for filter

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/activity_notification/models/notification.rb', line 208

scope :filtered_by_options,               ->(options = {}) {
  options = ActivityNotification.cast_to_indifferent_hash(options)
  filtered_notifications = all
  if options.has_key?(:filtered_by_type)
    filtered_notifications = filtered_notifications.filtered_by_type(options[:filtered_by_type])
  end
  if options.has_key?(:filtered_by_group)
    filtered_notifications = filtered_notifications.filtered_by_group(options[:filtered_by_group])
  end
  if options.has_key?(:filtered_by_group_type) and options.has_key?(:filtered_by_group_id)
    filtered_notifications = filtered_notifications
                             .where(group_type: options[:filtered_by_group_type], group_id: options[:filtered_by_group_id])
  end
  if options.has_key?(:filtered_by_key)
    filtered_notifications = filtered_notifications.filtered_by_key(options[:filtered_by_key])
  end
  if options.has_key?(:custom_filter)
    filtered_notifications = filtered_notifications.where(options[:custom_filter])
  end
  filtered_notifications
}

.filtered_by_targetActiveRecord_AssociationRelation<Notificaion>

Selects filtered notifications by target instance.

ActivityNotification::Notification.filtered_by_target(@user)

is the same as

@user.notifications

Parameters:

  • target (Object)

    Target instance for filter

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



144
# File 'lib/activity_notification/models/notification.rb', line 144

scope :filtered_by_target,                ->(target) { where(target: target) }

.filtered_by_target_typeActiveRecord_AssociationRelation<Notificaion>

Selects filtered notifications by target_type.

Examples:

Get filtered unopened notificatons of User as target type

@notifications = ActivityNotification.Notification.unopened_only.filtered_by_target_type('User')

Parameters:

  • target_type (String)

    Target type for filter

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



152
# File 'lib/activity_notification/models/notification.rb', line 152

scope :filtered_by_target_type,           ->(target_type) { where(target_type: target_type) }

.filtered_by_typeActiveRecord_AssociationRelation<Notificaion>

Selects filtered notifications by notifiable_type.

Examples:

Get filtered unopened notificatons of the @user for Comment notifiable class

@notifications = @user.notifications.unopened_only.filtered_by_type('Comment')

Parameters:

  • notifiable_type (String)

    Notifiable type for filter

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



168
# File 'lib/activity_notification/models/notification.rb', line 168

scope :filtered_by_type,                  ->(notifiable_type) { where(notifiable_type: notifiable_type) }

.group_members_onlyActiveRecord_AssociationRelation<Notificaion>

Selects group member notifications only.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



58
# File 'lib/activity_notification/models/notification.rb', line 58

scope :group_members_only,                -> { where.not(group_owner_id: nil) }

.group_owners_onlyActiveRecord_AssociationRelation<Notificaion>

Selects group owner notifications only.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



53
# File 'lib/activity_notification/models/notification.rb', line 53

scope :group_owners_only,                 -> { where(group_owner_id: nil) }

.opened_indexActiveRecord_AssociationRelation<Notificaion>

Selects unopened notification index.

ActivityNotification::Notification.opened_index(limit)

is defined same as

ActivityNotification::Notification.opened_only(limit).group_owners_only.latest_order

Examples:

Get unopened notificaton index of the @user with limit 10

@notifications = @user.notifications.opened_index(10)
@notifications = @user.notifications.opened_only(10).group_owners_only.latest_order

Parameters:

  • limit (Integer)

    Limit to query for opened notifications

  • reverse (Boolean)

    If notification index will be ordered as earliest first

  • with_group_members (Boolean)

    If notification index will include group members

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



121
122
123
124
# File 'lib/activity_notification/models/notification.rb', line 121

scope :opened_index,                      ->(limit, reverse = false, with_group_members = false) {
  target_index = with_group_members ? opened_only(limit) : opened_only(limit).group_owners_only
  reverse ? target_index.earliest_order : target_index.latest_order
}

.opened_index_group_members_onlyActiveRecord_AssociationRelation<Notificaion>

Selects group member notifications in opened_index.

Parameters:

  • limit (Integer)

    Limit to query for opened notifications

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



135
# File 'lib/activity_notification/models/notification.rb', line 135

scope :opened_index_group_members_only,   ->(limit) { where(group_owner_id: opened_index(limit).map(&:id)) }

.opened_onlyActiveRecord_AssociationRelation<Notificaion>

Selects opened notifications only with limit.

Parameters:

  • limit (Integer)

    Limit to query for opened notifications

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



107
# File 'lib/activity_notification/models/notification.rb', line 107

scope :opened_only,                       ->(limit) { opened_only!.limit(limit) }

.opened_only!ActiveRecord_AssociationRelation<Notificaion>

Selects opened notifications only without limit. Be careful to get too many records with this method.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



101
# File 'lib/activity_notification/models/notification.rb', line 101

scope :opened_only!,                      -> { where.not(opened_at: nil) }

.unopened_indexActiveRecord_AssociationRelation<Notificaion>

Selects unopened notification index.

ActivityNotification::Notification.unopened_index

is defined same as

ActivityNotification::Notification.unopened_only.group_owners_only.latest_order

Examples:

Get unopened notificaton index of the @user

@notifications = @user.notifications.unopened_index
@notifications = @user.notifications.unopened_only.group_owners_only.latest_order

Parameters:

  • reverse (Boolean)

    If notification index will be ordered as earliest first

  • with_group_members (Boolean)

    If notification index will include group members

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



92
93
94
95
# File 'lib/activity_notification/models/notification.rb', line 92

scope :unopened_index,                    ->(reverse = false, with_group_members = false) {
  target_index = with_group_members ? unopened_only : unopened_only.group_owners_only
  reverse ? target_index.earliest_order : target_index.latest_order
}

.unopened_index_group_members_onlyActiveRecord_AssociationRelation<Notificaion>

Selects group member notifications in unopened_index.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



129
# File 'lib/activity_notification/models/notification.rb', line 129

scope :unopened_index_group_members_only, -> { where(group_owner_id: unopened_index.map(&:id)) }

.unopened_onlyActiveRecord_AssociationRelation<Notificaion>

Selects unopened notifications only.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of filtered notifications



79
# File 'lib/activity_notification/models/notification.rb', line 79

scope :unopened_only,                     -> { where(opened_at: nil) }

Instance Method Details

#earliestNotification

Returns earliest notification instance.

Returns:



268
# File 'lib/activity_notification/models/notification.rb', line 268

scope :earliest,                          -> { earliest_order.first }

#earliest_orderActiveRecord_AssociationRelation<Notificaion>

Orders by earliest (older) first as created_at: :asc.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Database query of notifications ordered by earliest first



260
# File 'lib/activity_notification/models/notification.rb', line 260

scope :earliest_order,                    -> { order(created_at: :asc) }

#groupObject

Belongs to group instance of this notification as polymorphic association.

Returns:

  • (Object)

    Group instance of this notification



22
# File 'lib/activity_notification/models/notification.rb', line 22

belongs_to :group,         polymorphic: true

#group_membersActiveRecord_AssociationRelation<Notificaion>

Has many group member notification instances of this notification. Only group owner instance has :group_members value. Group member instance has nil as :group_members association.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Array or database query of the group member notification instances of this notification



36
# File 'lib/activity_notification/models/notification.rb', line 36

has_many   :group_members, class_name: :Notification, foreign_key: :group_owner_id

#group_ownerNotification

Belongs to group owner notification instance of this notification. Only group member instance has :group_owner value. Group owner instance has nil as :group_owner association.

Returns:

  • (Notification)

    Group owner notification instance of this notification



29
# File 'lib/activity_notification/models/notification.rb', line 29

belongs_to :group_owner,   class_name: :Notification

#latestNotification

Returns latest notification instance.

Returns:



264
# File 'lib/activity_notification/models/notification.rb', line 264

scope :latest,                            -> { latest_order.first }

#latest_orderActiveRecord_AssociationRelation<Notificaion>

Orders by latest (newest) first as created_at: :desc.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Database query of notifications ordered by latest first



256
# File 'lib/activity_notification/models/notification.rb', line 256

scope :latest_order,                      -> { order(created_at: :desc) }

#notifiableObject

Belongs to notifiable instance of this notification as polymorphic association.

Returns:

  • (Object)

    Notifiable instance of this notification



17
# File 'lib/activity_notification/models/notification.rb', line 17

belongs_to :notifiable,    polymorphic: true

#notifierObject

Belongs to :otifier instance of this notification.

Returns:

  • (Object)

    Notifier instance of this notification



41
# File 'lib/activity_notification/models/notification.rb', line 41

belongs_to :notifier,      polymorphic: true

#targetObject

Belongs to target instance of this notification as polymorphic association.

Returns:

  • (Object)

    Target instance of this notification



12
# File 'lib/activity_notification/models/notification.rb', line 12

belongs_to :target,        polymorphic: true

#with_groupActiveRecord_AssociationRelation<Notificaion>

Includes group instance with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Database query of notifications with group



240
# File 'lib/activity_notification/models/notification.rb', line 240

scope :with_group,                        -> { includes(:group) }

#with_group_membersActiveRecord_AssociationRelation<Notificaion>

Includes group member instances with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Database query of notifications with group members



248
# File 'lib/activity_notification/models/notification.rb', line 248

scope :with_group_members,                -> { includes(:group_members) }

#with_group_ownerActiveRecord_AssociationRelation<Notificaion>

Includes group owner instances with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Database query of notifications with group owner



244
# File 'lib/activity_notification/models/notification.rb', line 244

scope :with_group_owner,                 -> { includes(:group_owner) }

#with_notifiableActiveRecord_AssociationRelation<Notificaion>

Includes notifiable instance with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Database query of notifications with notifiable



236
# File 'lib/activity_notification/models/notification.rb', line 236

scope :with_notifiable,                   -> { includes(:notifiable) }

#with_notifierActiveRecord_AssociationRelation<Notificaion>

Includes notifier instance with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Database query of notifications with notifier



252
# File 'lib/activity_notification/models/notification.rb', line 252

scope :with_notifier,                     -> { includes(:notifier) }

#with_targetActiveRecord_AssociationRelation<Notificaion>

Includes target instance with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>)

    Database query of notifications with target



232
# File 'lib/activity_notification/models/notification.rb', line 232

scope :with_target,                       -> { includes(:target) }