Class: Dynamoid::Criteria::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/activity_notification/orm/dynamoid/extension.rb,
lib/activity_notification/orm/dynamoid.rb

Overview

Direct Known Subclasses

None

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_index!(reverse = false, with_group_members = false) ⇒ Dynamoid::Criteria::Chain

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


140
141
142
143
# File 'lib/activity_notification/orm/dynamoid.rb', line 140

def 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
end

.earlier_than(created_time) ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>

Selects filtered notifications earlier than specified time.

Examples:

Get filtered unopened notificatons of the @user earlier than @notification

@notifications = @user.notifications.unopened_only.earlier_than(@notification.created_at)


285
286
287
# File 'lib/activity_notification/orm/dynamoid.rb', line 285

def earlier_than(created_time)
  where('created_at.lt': created_time)
end

.exists?Boolean

Return if records exist



51
52
53
# File 'lib/activity_notification/orm/dynamoid/extension.rb', line 51

def exists?
  record_limit(1).count > 0
end

.filtered_by_association(name, instance) ⇒ Dynamoid::Criteria::Chain

Selects filtered notifications or subscriptions by associated instance.



183
184
185
# File 'lib/activity_notification/orm/dynamoid.rb', line 183

def filtered_by_association(name, instance)
  instance.present? ? where("#{name}_key" => "#{instance.class.name}#{ActivityNotification.config.composite_key_delimiter}#{instance.id}") : where("#{name}_key.null" => true)
end

.filtered_by_association_type(name, type) ⇒ Dynamoid::Criteria::Chain

Selects filtered notifications or subscriptions by association type.



192
193
194
195
196
# File 'lib/activity_notification/orm/dynamoid.rb', line 192

def filtered_by_association_type(name, type)
  # Handle both string class names and object instances
  type_name = type.is_a?(String) ? type : type.class.name
  where("#{name}_key.begins_with" => "#{type_name}#{ActivityNotification.config.composite_key_delimiter}")
end

.filtered_by_association_type_and_id(name, type, id) ⇒ Dynamoid::Criteria::Chain

Selects filtered notifications or subscriptions by association type and id.



204
205
206
# File 'lib/activity_notification/orm/dynamoid.rb', line 204

def filtered_by_association_type_and_id(name, type, id)
  type.present? && id.present? ? where("#{name}_key" => "#{type}#{ActivityNotification.config.composite_key_delimiter}#{id}") : none
end

.filtered_by_group(group) ⇒ Dynamoid::Criteria::Chain

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)


235
236
237
# File 'lib/activity_notification/orm/dynamoid.rb', line 235

def filtered_by_group(group)
  filtered_by_association("group", group)
end

.filtered_by_instance(notifiable) ⇒ Dynamoid::Criteria::Chain

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)


225
226
227
# File 'lib/activity_notification/orm/dynamoid.rb', line 225

def filtered_by_instance(notifiable)
  filtered_by_association("notifiable", notifiable)
end

.filtered_by_key(key) ⇒ Dynamoid::Criteria::Chain

Selects filtered notifications or subscriptions by key.

Examples:

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

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


265
266
267
# File 'lib/activity_notification/orm/dynamoid.rb', line 265

def filtered_by_key(key)
  where(key: key)
end

.filtered_by_options(options = {}) ⇒ Dynamoid::Criteria::Chain

Selects filtered notifications or subscriptions 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] })

Options Hash (options):

  • :filtered_by_type (String) — default: nil

    Notifiable type for filter

  • :filtered_by_group (Object) — default: nil

    Group instance for filter

  • :filtered_by_group_type (String) — default: nil

    Group type for filter, valid with :filtered_by_group_id

  • :filtered_by_group_id (String) — default: nil

    Group instance id for filter, valid with :filtered_by_group_type

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :later_than (String) — default: nil

    ISO 8601 format time to filter notification index later than specified time

  • :earlier_than (String) — default: nil

    ISO 8601 format time to filter notification index earlier than specified time

  • :custom_filter (Array|Hash) — default: nil

    Custom notification filter (e.g. [‘created_at.gt’: time.hour.ago])



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/activity_notification/orm/dynamoid.rb', line 313

def filtered_by_options(options = {})
  options = ActivityNotification.cast_to_indifferent_hash(options)
  filtered_notifications = self
  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.filtered_by_association_type_and_id("group", options[:filtered_by_group_type], 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?(:later_than)
    filtered_notifications = filtered_notifications.later_than(Time.iso8601(options[:later_than]))
  end
  if options.has_key?(:earlier_than)
    filtered_notifications = filtered_notifications.earlier_than(Time.iso8601(options[:earlier_than]))
  end
  if options.has_key?(:custom_filter)
    filtered_notifications = filtered_notifications.where(options[:custom_filter])
  end
  filtered_notifications
end

.filtered_by_target(target) ⇒ Dynamoid::Criteria::Chain

Selects filtered notifications or subscriptions by target instance.

ActivityNotification::Notification.filtered_by_target(@user)

is the same as

@user.notifications


215
216
217
# File 'lib/activity_notification/orm/dynamoid.rb', line 215

def filtered_by_target(target)
  filtered_by_association("target", target)
end

.filtered_by_target_type(target_type) ⇒ Dynamoid::Criteria::Chain

Selects filtered notifications or subscriptions by target_type.

Examples:

Get filtered unopened notificatons of User as target type

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


245
246
247
# File 'lib/activity_notification/orm/dynamoid.rb', line 245

def filtered_by_target_type(target_type)
  filtered_by_association_type("target", target_type)
end

.filtered_by_type(notifiable_type) ⇒ Dynamoid::Criteria::Chain

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')


255
256
257
# File 'lib/activity_notification/orm/dynamoid.rb', line 255

def filtered_by_type(notifiable_type)
  filtered_by_association_type("notifiable", notifiable_type)
end

.group_members_of_owner_ids_only(owner_ids) ⇒ Dynamoid::Criteria::Chain

Selects group member notifications with specified group owner ids.



485
486
487
# File 'lib/activity_notification/orm/dynamoid.rb', line 485

def group_members_of_owner_ids_only(owner_ids)
  owner_ids.present? ? where('group_owner_id.in': owner_ids) : none
end

.group_members_onlyDynamoid::Criteria::Chain

Selects group member notifications only.



403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/activity_notification/orm/dynamoid.rb', line 403

def group_members_only
  # Create a new chain to avoid state issues
  new_chain = @source.where('group_owner_id.not_null': true)
  # Apply existing conditions from current chain
  if instance_variable_defined?(:@where_conditions) && @where_conditions
    @where_conditions.instance_variable_get(:@hash_conditions).each do |condition|
      # Skip conflicting group_owner_id conditions
      next if condition.key?(:"group_owner_id.null") || condition.key?(:"group_owner_id.not_null")
      new_chain = new_chain.where(condition)
    end
  end
  new_chain
end

.group_owners_onlyDynamoid::Criteria::Chain

Selects group owner notifications only.



396
397
398
# File 'lib/activity_notification/orm/dynamoid.rb', line 396

def group_owners_only
  where('group_owner_id.null': true)
end

.later_than(created_time) ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>

Selects filtered notifications later than specified time.

Examples:

Get filtered unopened notificatons of the @user later than @notification

@notifications = @user.notifications.unopened_only.later_than(@notification.created_at)


275
276
277
# File 'lib/activity_notification/orm/dynamoid.rb', line 275

def later_than(created_time)
  where('created_at.gt': created_time)
end

.limit(limit) ⇒ Dynamoid::Criteria::Chain

Set query result limit as record_limit of Dynamoid



44
45
46
# File 'lib/activity_notification/orm/dynamoid/extension.rb', line 44

def limit(limit)
  record_limit(limit)
end

.opened_index(limit, reverse = false, with_group_members = false) ⇒ Dynamoid::Criteria::Chain

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


173
174
175
176
# File 'lib/activity_notification/orm/dynamoid.rb', line 173

def 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
end

.opened_index_group_members_only(limit) ⇒ Dynamoid::Criteria::Chain

Selects group member notifications in opened_index.



468
469
470
471
# File 'lib/activity_notification/orm/dynamoid.rb', line 468

def opened_index_group_members_only(limit)
  group_owner_ids = opened_index(limit).map(&:id)
  group_owner_ids.empty? ? none : where('group_owner_id.in': group_owner_ids)
end

.opened_only(limit) ⇒ Dynamoid::Criteria::Chain

Selects opened notifications only with limit.



452
453
454
# File 'lib/activity_notification/orm/dynamoid.rb', line 452

def opened_only(limit)
  limit == 0 ? none : opened_only!.limit(limit)
end

.opened_only!Dynamoid::Criteria::Chain

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



436
437
438
439
440
441
442
443
444
445
446
# File 'lib/activity_notification/orm/dynamoid.rb', line 436

def opened_only!
  # Create a new chain to avoid state issues
  new_chain = @source.where('opened_at.not_null': true)
  # Apply existing conditions from current chain
  if instance_variable_defined?(:@where_conditions) && @where_conditions
    @where_conditions.instance_variable_get(:@hash_conditions).each do |condition|
      new_chain = new_chain.where(condition)
    end
  end
  new_chain
end

.sizeInteger

Return size of records as count



58
59
60
# File 'lib/activity_notification/orm/dynamoid/extension.rb', line 58

def size
  count
end

.unopened_index(reverse = false, with_group_members = false) ⇒ Dynamoid::Criteria::Chain

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


156
157
158
159
# File 'lib/activity_notification/orm/dynamoid.rb', line 156

def 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
end

.unopened_index_group_members_onlyDynamoid::Criteria::Chain

Selects group member notifications in unopened_index.



459
460
461
462
# File 'lib/activity_notification/orm/dynamoid.rb', line 459

def unopened_index_group_members_only
  group_owner_ids = unopened_index.map(&:id)
  group_owner_ids.empty? ? none : where('group_owner_id.in': group_owner_ids)
end

.unopened_onlyDynamoid::Criteria::Chain

Selects unopened notifications only.



420
421
422
423
424
425
426
427
428
429
430
# File 'lib/activity_notification/orm/dynamoid.rb', line 420

def unopened_only
  # Create a new chain to avoid state issues
  new_chain = @source.where('opened_at.null': true)
  # Apply existing conditions from current chain
  if instance_variable_defined?(:@where_conditions) && @where_conditions
    @where_conditions.instance_variable_get(:@hash_conditions).each do |condition|
      new_chain = new_chain.where(condition)
    end
  end
  new_chain
end

.within_expiration_only(expiry_delay) ⇒ Dynamoid::Criteria::Chain

Selects notifications within expiration.



477
478
479
# File 'lib/activity_notification/orm/dynamoid.rb', line 477

def within_expiration_only(expiry_delay)
  where('created_at.gt': expiry_delay.ago)
end

Instance Method Details

#earliestNotification

Returns earliest notification instance.



538
539
540
# File 'lib/activity_notification/orm/dynamoid.rb', line 538

def earliest
  earliest_order.first
end

#earliest!Notification

Returns earliest notification instance. It does not use sort key in DynamoDB tables.



552
553
554
# File 'lib/activity_notification/orm/dynamoid.rb', line 552

def earliest!
  earliest_order!.first
end

#earliest_orderDynamoid::Criteria::Chain

Orders by earliest (older) first as created_at: :asc. It uses sort key of Global Secondary Index in DynamoDB tables.



351
352
353
354
# File 'lib/activity_notification/orm/dynamoid.rb', line 351

def earliest_order
  # order(created_at: :asc)
  scan_index_forward(true)
end

#earliest_order!Array

Orders by earliest (older) first as created_at: :asc and returns as array. It does not use sort key in DynamoDB tables.



367
368
369
370
# File 'lib/activity_notification/orm/dynamoid.rb', line 367

def earliest_order!
  # order(created_at: :asc)
  all.to_a.sort_by {|n| n.created_at }
end

#earliest_subscribed_orderArray

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



381
382
383
384
# File 'lib/activity_notification/orm/dynamoid.rb', line 381

def earliest_subscribed_order
  # order(subscribed_at: :asc)
  all.to_a.sort_by {|n| n.subscribed_at }
end

#key_orderArray

Orders by key name as key: :asc.



388
389
390
391
# File 'lib/activity_notification/orm/dynamoid.rb', line 388

def key_order
  # order(key: :asc)
  all.to_a.sort_by {|n| n.key }
end

#latestNotification

Returns latest notification instance.



532
533
534
# File 'lib/activity_notification/orm/dynamoid.rb', line 532

def latest
  latest_order.first
end

#latest!Notification

Returns latest notification instance. It does not use sort key in DynamoDB tables.



545
546
547
# File 'lib/activity_notification/orm/dynamoid.rb', line 545

def latest!
  latest_order!.first
end

#latest_orderDynamoid::Criteria::Chain

Orders by latest (newest) first as created_at: :desc. It uses sort key of Global Secondary Index in DynamoDB tables.



343
344
345
346
# File 'lib/activity_notification/orm/dynamoid.rb', line 343

def latest_order
  # order(created_at: :desc)
  scan_index_forward(false)
end

#latest_order!(reverse = false) ⇒ Array

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



359
360
361
362
# File 'lib/activity_notification/orm/dynamoid.rb', line 359

def latest_order!(reverse = false)
  # order(created_at: :desc)
  reverse ? earliest_order! : earliest_order!.reverse
end

#latest_subscribed_orderArray

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



374
375
376
377
# File 'lib/activity_notification/orm/dynamoid.rb', line 374

def latest_subscribed_order
  # order(subscribed_at: :desc)
  earliest_subscribed_order.reverse
end

#noneObject

Return new none object



36
37
38
# File 'lib/activity_notification/orm/dynamoid/extension.rb', line 36

def none
  None.new(self.source)
end

#reloadObject

Dummy reload method for test of notifications or subscriptions.



526
527
528
# File 'lib/activity_notification/orm/dynamoid.rb', line 526

def reload
  self
end

#serializable_hash(options = {}) ⇒ Object

Return serializable_hash as array



70
71
72
# File 'lib/activity_notification/orm/dynamoid/extension.rb', line 70

def serializable_hash(options = {})
  all.to_a.map { |r| r.serializable_hash(options) }
end

#uniq_keysArray<String>

Selects unique keys from query for notifications or subscriptions.



558
559
560
# File 'lib/activity_notification/orm/dynamoid.rb', line 558

def uniq_keys
  all.to_a.collect {|n| n.key }.uniq
end

#update_all(conditions = {}) ⇒ Object

TODO Make this batch



63
64
65
66
67
# File 'lib/activity_notification/orm/dynamoid/extension.rb', line 63

def update_all(conditions = {})
  each do |document|
    document.update_attributes(conditions)
  end
end

#with_groupDynamoid::Criteria::Chain

Includes group instance with query for notifications.



503
504
505
# File 'lib/activity_notification/orm/dynamoid.rb', line 503

def with_group
  self
end

#with_group_membersDynamoid::Criteria::Chain

Includes group member instances with query for notifications.



515
516
517
# File 'lib/activity_notification/orm/dynamoid.rb', line 515

def with_group_members
  self
end

#with_group_ownerDynamoid::Criteria::Chain

Includes group owner instances with query for notifications.



509
510
511
# File 'lib/activity_notification/orm/dynamoid.rb', line 509

def with_group_owner
  self
end

#with_notifiableDynamoid::Criteria::Chain

Includes notifiable instance with query for notifications.



497
498
499
# File 'lib/activity_notification/orm/dynamoid.rb', line 497

def with_notifiable
  self
end

#with_notifierDynamoid::Criteria::Chain

Includes notifier instance with query for notifications.



521
522
523
# File 'lib/activity_notification/orm/dynamoid.rb', line 521

def with_notifier
  self
end

#with_targetDynamoid::Criteria::Chain

Includes target instance with query for notifications or subscriptions.



491
492
493
# File 'lib/activity_notification/orm/dynamoid.rb', line 491

def with_target
  self
end