Module: ActivityNotification::SubscriptionApi

Extended by:
ActiveSupport::Concern
Included in:
ORM::ActiveRecord::Subscription, ORM::Dynamoid::Subscription, ORM::Mongoid::Subscription
Defined in:
lib/activity_notification/apis/subscription_api.rb

Overview

Defines API for subscription included in Subscription model.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert_time_as_hash(time) ⇒ Time, Object

Convert Time value to store in database as Hash value.

Parameters:

  • time (Time)

    Time value to store in database as Hash value

Returns:

  • (Time, Object)

    Converted Time value



73
74
75
# File 'lib/activity_notification/apis/subscription_api.rb', line 73

def self.convert_time_as_hash(time)
  time
end

.filtered_by_keyActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>

Selects filtered subscriptions by key.

Examples:

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

@subscriptions = @user.subscriptions.filtered_by_key('comment.reply')

Parameters:

  • key (String)

    Key of the subscription for filter

Returns:

  • (ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>)

    Database query of filtered subscriptions



15
# File 'lib/activity_notification/apis/subscription_api.rb', line 15

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

.filtered_by_optionsActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>

Selects filtered subscriptions by key with filter options.

Examples:

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

@subscriptions = @user.subscriptions.filtered_by_key('comment.reply')

Get custom filtered subscriptions of the @user

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

Parameters:

  • options (Hash)

    Options for filter

Returns:

  • (ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>)

    Database query of filtered subscriptions



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/activity_notification/apis/subscription_api.rb', line 27

scope :filtered_by_options, ->(options = {}) {
  options = ActivityNotification.cast_to_indifferent_hash(options)
  filtered_subscriptions = all
  if options.has_key?(:filtered_by_key)
    filtered_subscriptions = filtered_subscriptions.filtered_by_key(options[:filtered_by_key])
  end
  if options.has_key?(:custom_filter)
    filtered_subscriptions = filtered_subscriptions.where(options[:custom_filter])
  end
  filtered_subscriptions
}

.to_optional_target_key(optional_target_name) ⇒ Symbol

Returns key of optional_targets hash from symbol class name of the optional target implementation.

Parameters:

  • optional_target_name (String, Symbol)

    Class name of the optional target implementation (e.g. :amazon_sns, :slack)

Returns:

  • (Symbol)

    Key of optional_targets hash



84
85
86
# File 'lib/activity_notification/apis/subscription_api.rb', line 84

def to_optional_target_key(optional_target_name)
  ("subscribing_to_" + optional_target_name.to_s).to_sym
end

.to_optional_target_subscribed_at_key(optional_target_name) ⇒ Symbol

Returns subscribed_at parameter key of optional_targets hash from symbol class name of the optional target implementation.

Parameters:

  • optional_target_name (String, Symbol)

    Class name of the optional target implementation (e.g. :amazon_sns, :slack)

Returns:

  • (Symbol)

    Subscribed_at parameter key of optional_targets hash



91
92
93
# File 'lib/activity_notification/apis/subscription_api.rb', line 91

def to_optional_target_subscribed_at_key(optional_target_name)
  ("subscribed_to_" + optional_target_name.to_s + "_at").to_sym
end

.to_optional_target_unsubscribed_at_key(optional_target_name) ⇒ Symbol

Returns unsubscribed_at parameter key of optional_targets hash from symbol class name of the optional target implementation.

Parameters:

  • optional_target_name (String, Symbol)

    Class name of the optional target implementation (e.g. :amazon_sns, :slack)

Returns:

  • (Symbol)

    Unsubscribed_at parameter key of optional_targets hash



98
99
100
# File 'lib/activity_notification/apis/subscription_api.rb', line 98

def to_optional_target_unsubscribed_at_key(optional_target_name)
  ("unsubscribed_to_" + optional_target_name.to_s + "_at").to_sym
end

Instance Method Details

#as_json(options = {}) ⇒ Hash

Override as_json method for optional_targets representation

Parameters:

  • options (Hash) (defaults to: {})

    Options for as_json method

Returns:

  • (Hash)

    Hash representing the subscription model



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/activity_notification/apis/subscription_api.rb', line 107

def as_json(options = {})
  json = super(options).with_indifferent_access
  optional_targets_json = {}
  optional_target_names.each do |optional_target_name|
    optional_targets_json[optional_target_name] = {
      subscribing:   json[:optional_targets][Subscription.to_optional_target_key(optional_target_name)],
      subscribed_at: json[:optional_targets][Subscription.to_optional_target_subscribed_at_key(optional_target_name)],
      unsubscribed_at: json[:optional_targets][Subscription.to_optional_target_unsubscribed_at_key(optional_target_name)]
    }
  end
  json[:optional_targets] = optional_targets_json
  json
end

#earliest_orderActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>

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

Returns:

  • (ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>)

    Database query of subscriptions ordered by earliest first



45
# File 'lib/activity_notification/apis/subscription_api.rb', line 45

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

#earliest_order!ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>

Orders by earliest (older) first as created_at: :asc. This method is to be overridden in implementation for each ORM.

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>)

    Database query of subscriptions ordered by earliest first



56
# File 'lib/activity_notification/apis/subscription_api.rb', line 56

scope :earliest_order!,           -> { earliest_order }

#earliest_subscribed_orderActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>

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

Returns:

  • (ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>)

    Database query of subscriptions ordered by earliest subscribed_at first



64
# File 'lib/activity_notification/apis/subscription_api.rb', line 64

scope :earliest_subscribed_order, -> { order(subscribed_at: :asc) }

#key_orderActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>

Orders by key name as key: :asc.

Returns:

  • (ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>)

    Database query of subscriptions ordered by key name



68
# File 'lib/activity_notification/apis/subscription_api.rb', line 68

scope :key_order,                 -> { order(key: :asc) }

#latest_orderActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>

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

Returns:

  • (ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>)

    Database query of subscriptions ordered by latest first



41
# File 'lib/activity_notification/apis/subscription_api.rb', line 41

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

#latest_order!ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>

Orders by latest (newest) first as created_at: :desc. This method is to be overridden in implementation for each ORM.

Parameters:

  • reverse (Boolean)

    If subscriptions will be ordered as earliest first

Returns:

  • (ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>)

    Database query of ordered subscriptions



51
# File 'lib/activity_notification/apis/subscription_api.rb', line 51

scope :latest_order!,             ->(reverse = false) { reverse ? earliest_order : latest_order }

#latest_subscribed_orderActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>

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

Returns:

  • (ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>)

    Database query of subscriptions ordered by latest subscribed_at first



60
# File 'lib/activity_notification/apis/subscription_api.rb', line 60

scope :latest_subscribed_order,   -> { order(subscribed_at: :desc) }

#optional_target_namesArray<Symbol>

Returns optional_target names of the subscription from optional_targets field.

Returns:

  • (Array<Symbol>)

    Array of optional target names



224
225
226
# File 'lib/activity_notification/apis/subscription_api.rb', line 224

def optional_target_names
  optional_targets.keys.select { |key| key.to_s.start_with?("subscribing_to_") }.map { |key| key.slice(15..-1) }
end

#subscribe(options = {}) ⇒ Boolean

Subscribes to the notification and notification email.

Parameters:

  • options (Hash) (defaults to: {})

    Options for subscribing to the notification

Options Hash (options):

  • :subscribed_at (DateTime) — default: Time.current

    Time to set to subscribed_at and subscribed_to_email_at of the subscription record

  • :with_email_subscription (Boolean) — default: true

    If the subscriber also subscribes notification email

  • :with_optional_targets (Boolean) — default: true

    If the subscriber also subscribes optional_targets

Returns:

  • (Boolean)

    If successfully updated subscription instance



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/activity_notification/apis/subscription_api.rb', line 128

def subscribe(options = {})
  subscribed_at = options[:subscribed_at] || Time.current
  with_email_subscription = options.has_key?(:with_email_subscription) ? options[:with_email_subscription] : ActivityNotification.config.subscribe_to_email_as_default
  with_optional_targets   = options.has_key?(:with_optional_targets) ? options[:with_optional_targets] : ActivityNotification.config.subscribe_to_optional_targets_as_default
  new_attributes = { subscribing: true, subscribed_at: subscribed_at, optional_targets: optional_targets }
  new_attributes = new_attributes.merge(subscribing_to_email: true, subscribed_to_email_at: subscribed_at) if with_email_subscription
  if with_optional_targets
    optional_target_names.each do |optional_target_name|
      new_attributes[:optional_targets] = new_attributes[:optional_targets].merge(
        Subscription.to_optional_target_key(optional_target_name) => true,
        Subscription.to_optional_target_subscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(subscribed_at))
    end
  end
  update(new_attributes)
end

#subscribe_to_email(options = {}) ⇒ Boolean

Subscribes to the notification email.

Parameters:

  • options (Hash) (defaults to: {})

    Options for subscribing to the notification email

Options Hash (options):

  • :subscribed_to_email_at (DateTime) — default: Time.current

    Time to set to subscribed_to_email_at of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



167
168
169
170
# File 'lib/activity_notification/apis/subscription_api.rb', line 167

def subscribe_to_email(options = {})
  subscribed_to_email_at = options[:subscribed_to_email_at] || Time.current
  update(subscribing_to_email: true, subscribed_to_email_at: subscribed_to_email_at)
end

#subscribe_to_optional_target(optional_target_name, options = {}) ⇒ Boolean

Subscribes to the specified optional target.

Parameters:

  • optional_target_name (String, Symbol)

    Symbol class name of the optional target implementation (e.g. :amazon_sns, :slack)

  • options (Hash) (defaults to: {})

    Options for unsubscribing to the specified optional target

Options Hash (options):

  • :subscribed_at (DateTime) — default: Time.current

    Time to set to subscribed__at in optional_targets hash of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



200
201
202
203
204
205
206
# File 'lib/activity_notification/apis/subscription_api.rb', line 200

def subscribe_to_optional_target(optional_target_name, options = {})
  subscribed_at = options[:subscribed_at] || Time.current
  update(optional_targets: optional_targets.merge(
    Subscription.to_optional_target_key(optional_target_name) => true,
    Subscription.to_optional_target_subscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(subscribed_at))
  )
end

#subscribing_to_optional_target?(optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_to_optional_targets_as_default) ⇒ Boolean

Returns if the target subscribes to the specified optional target.

Parameters:

  • optional_target_name (Symbol)

    Symbol class name of the optional target implementation (e.g. :amazon_sns, :slack)

  • subscribe_as_default (Boolean) (defaults to: ActivityNotification.config.subscribe_to_optional_targets_as_default)

    Default subscription value to use when the subscription record does not configured

Returns:

  • (Boolean)

    If the target subscribes to the specified optional target



187
188
189
190
191
192
# File 'lib/activity_notification/apis/subscription_api.rb', line 187

def subscribing_to_optional_target?(optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_to_optional_targets_as_default)
  optional_target_key = Subscription.to_optional_target_key(optional_target_name)
  subscribe_as_default ?
    !optional_targets.has_key?(optional_target_key) || optional_targets[optional_target_key] :
     optional_targets.has_key?(optional_target_key) && optional_targets[optional_target_key]
end

#unsubscribe(options = {}) ⇒ Boolean

Unsubscribes to the notification and notification email.

Parameters:

  • options (Hash) (defaults to: {})

    Options for unsubscribing to the notification

Options Hash (options):

  • :unsubscribed_at (DateTime) — default: Time.current

    Time to set to unsubscribed_at and unsubscribed_to_email_at of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/activity_notification/apis/subscription_api.rb', line 149

def unsubscribe(options = {})
  unsubscribed_at = options[:unsubscribed_at] || Time.current
  new_attributes = { subscribing:          false, unsubscribed_at:          unsubscribed_at,
                     subscribing_to_email: false, unsubscribed_to_email_at: unsubscribed_at,
                     optional_targets: optional_targets }
  optional_target_names.each do |optional_target_name|
    new_attributes[:optional_targets] = new_attributes[:optional_targets].merge(
      Subscription.to_optional_target_key(optional_target_name) => false,
      Subscription.to_optional_target_unsubscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(subscribed_at))
  end
  update(new_attributes)
end

#unsubscribe_to_email(options = {}) ⇒ Boolean

Unsubscribes to the notification email.

Parameters:

  • options (Hash) (defaults to: {})

    Options for unsubscribing the notification email

Options Hash (options):

  • :subscribed_to_email_at (DateTime) — default: Time.current

    Time to set to subscribed_to_email_at of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



177
178
179
180
# File 'lib/activity_notification/apis/subscription_api.rb', line 177

def unsubscribe_to_email(options = {})
  unsubscribed_to_email_at = options[:unsubscribed_to_email_at] || Time.current
  update(subscribing_to_email: false, unsubscribed_to_email_at: unsubscribed_to_email_at)
end

#unsubscribe_to_optional_target(optional_target_name, options = {}) ⇒ Boolean

Unsubscribes to the specified optional target.

Parameters:

  • optional_target_name (String, Symbol)

    Class name of the optional target implementation (e.g. :amazon_sns, :slack)

  • options (Hash) (defaults to: {})

    Options for unsubscribing to the specified optional target

Options Hash (options):

  • :unsubscribed_at (DateTime) — default: Time.current

    Time to set to unsubscribed__at in optional_targets hash of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



214
215
216
217
218
219
220
# File 'lib/activity_notification/apis/subscription_api.rb', line 214

def unsubscribe_to_optional_target(optional_target_name, options = {})
  unsubscribed_at = options[:unsubscribed_at] || Time.current
  update(optional_targets: optional_targets.merge(
    Subscription.to_optional_target_key(optional_target_name) => false,
    Subscription.to_optional_target_unsubscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(unsubscribed_at))
  )
end