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, #email_subscribed?, generate_notification, #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?, #optional_target_names, #optional_target_subscribed?, #optional_targets, #publish_to_optional_targets, #remove_from_group, send_batch_notification_email, #send_notification_email, #subscribed?, #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



73
74
75
76
# File 'lib/activity_notification/models/notification.rb', line 73

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_group ⇒ ActiveRecord_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



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

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

.filtered_by_instance ⇒ ActiveRecord_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



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

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

.filtered_by_key ⇒ ActiveRecord_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



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

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

.filtered_by_options ⇒ ActiveRecord_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



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

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) && 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_target ⇒ ActiveRecord_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



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

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

.filtered_by_target_type ⇒ ActiveRecord_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



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

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

.filtered_by_type ⇒ ActiveRecord_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



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

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

.group_members_only ⇒ ActiveRecord_AssociationRelation<Notificaion>

Selects group member notifications only.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Array or database query of filtered notifications



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

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

.group_owners_only ⇒ ActiveRecord_AssociationRelation<Notificaion>

Selects group owner notifications only.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Array or database query of filtered notifications



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

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

.opened_index ⇒ ActiveRecord_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



123
124
125
126
# File 'lib/activity_notification/models/notification.rb', line 123

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_only ⇒ ActiveRecord_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



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

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

.opened_only ⇒ ActiveRecord_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



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

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



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

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

.unopened_index ⇒ ActiveRecord_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



94
95
96
97
# File 'lib/activity_notification/models/notification.rb', line 94

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_only ⇒ ActiveRecord_AssociationRelation<Notificaion>

Selects group member notifications in unopened_index.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Array or database query of filtered notifications



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

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

.unopened_only ⇒ ActiveRecord_AssociationRelation<Notificaion>

Selects unopened notifications only.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Array or database query of filtered notifications



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

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

Instance Method Details

#earliest ⇒ Notification

Returns earliest notification instance.

Returns:



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

scope :earliest,                          -> { earliest_order.first }

#earliest_order ⇒ ActiveRecord_AssociationRelation<Notificaion>

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

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Database query of notifications ordered by earliest first



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

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

#group ⇒ Object

Belongs to group instance of this notification as polymorphic association.

Returns:

  • (Object) —

    Group instance of this notification



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

belongs_to :group,         polymorphic: true

#group_members ⇒ ActiveRecord_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



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

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

#group_owner ⇒ Notification

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



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

belongs_to :group_owner,   class_name: :Notification

#latest ⇒ Notification

Returns latest notification instance.

Returns:



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

scope :latest,                            -> { latest_order.first }

#latest_order ⇒ ActiveRecord_AssociationRelation<Notificaion>

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

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Database query of notifications ordered by latest first



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

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

#notifiable ⇒ Object

Belongs to notifiable instance of this notification as polymorphic association.

Returns:

  • (Object) —

    Notifiable instance of this notification



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

belongs_to :notifiable,    polymorphic: true

#notifier ⇒ Object

Belongs to :otifier instance of this notification.

Returns:

  • (Object) —

    Notifier instance of this notification



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

belongs_to :notifier,      polymorphic: true

#target ⇒ Object

Belongs to target instance of this notification as polymorphic association.

Returns:

  • (Object) —

    Target instance of this notification



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

belongs_to :target,        polymorphic: true

#with_group ⇒ ActiveRecord_AssociationRelation<Notificaion>

Includes group instance with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Database query of notifications with group



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

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

#with_group_members ⇒ ActiveRecord_AssociationRelation<Notificaion>

Includes group member instances with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Database query of notifications with group members



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

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

#with_group_owner ⇒ ActiveRecord_AssociationRelation<Notificaion>

Includes group owner instances with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Database query of notifications with group owner



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

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

#with_notifiable ⇒ ActiveRecord_AssociationRelation<Notificaion>

Includes notifiable instance with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Database query of notifications with notifiable



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

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

#with_notifier ⇒ ActiveRecord_AssociationRelation<Notificaion>

Includes notifier instance with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Database query of notifications with notifier



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

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

#with_target ⇒ ActiveRecord_AssociationRelation<Notificaion>

Includes target instance with query for notifications.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>) —

    Database query of notifications with target



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

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