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
-
.convert_time_as_hash(time) ⇒ Time, Object
Convert Time value to store in database as Hash value.
-
.filtered_by_key ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Selects filtered subscriptions by key.
-
.filtered_by_options ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Selects filtered subscriptions by key with filter options.
-
.to_optional_target_key(optional_target_name) ⇒ Symbol
Returns key of optional_targets hash from symbol class name of the optional target implementation.
-
.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.
-
.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.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hash
Override as_json method for optional_targets representation.
-
#earliest_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as created_at: :asc.
-
#earliest_order! ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as created_at: :asc.
-
#earliest_subscribed_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as subscribed_at: :asc.
-
#key_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by key name as key: :asc.
-
#latest_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as created_at: :desc.
-
#latest_order! ⇒ ActiveRecord_AssociationRelation<Notificaion>, Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as created_at: :desc.
-
#latest_subscribed_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as subscribed_at: :desc.
-
#optional_target_names ⇒ Array<Symbol>
Returns optional_target names of the subscription from optional_targets field.
-
#subscribe(options = {}) ⇒ Boolean
Subscribes to the notification and notification email.
-
#subscribe_to_email(options = {}) ⇒ Boolean
Subscribes to the notification email.
-
#subscribe_to_optional_target(optional_target_name, options = {}) ⇒ Boolean
Subscribes to the specified optional target.
-
#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.
-
#unsubscribe(options = {}) ⇒ Boolean
Unsubscribes to the notification and notification email.
-
#unsubscribe_to_email(options = {}) ⇒ Boolean
Unsubscribes to the notification email.
-
#unsubscribe_to_optional_target(optional_target_name, options = {}) ⇒ Boolean
Unsubscribes to the specified optional target.
Class Method Details
.convert_time_as_hash(time) ⇒ Time, Object
Convert Time value to store in database as Hash 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_key ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Selects filtered subscriptions by key.
15 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 15 scope :filtered_by_key, ->(key) { where(key: key) } |
.filtered_by_options ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Selects filtered subscriptions by key with filter options.
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, ->( = {}) { = ActivityNotification.cast_to_indifferent_hash() filtered_subscriptions = all if .has_key?(:filtered_by_key) filtered_subscriptions = filtered_subscriptions.filtered_by_key([:filtered_by_key]) end if .has_key?(:custom_filter) filtered_subscriptions = filtered_subscriptions.where([: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.
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.
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.
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
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( = {}) json = super().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_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as created_at: :asc.
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.
56 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 56 scope :earliest_order!, -> { earliest_order } |
#earliest_subscribed_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by earliest (older) first as subscribed_at: :asc.
64 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 64 scope :earliest_subscribed_order, -> { order(subscribed_at: :asc) } |
#key_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by key name as key: :asc.
68 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 68 scope :key_order, -> { order(key: :asc) } |
#latest_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as created_at: :desc.
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.
51 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 51 scope :latest_order!, ->(reverse = false) { reverse ? earliest_order : latest_order } |
#latest_subscribed_order ⇒ ActiveRecord_AssociationRelation<Subscription>, Mongoid::Criteria<Notificaion>
Orders by latest (newest) first as subscribed_at: :desc.
60 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 60 scope :latest_subscribed_order, -> { order(subscribed_at: :desc) } |
#optional_target_names ⇒ Array<Symbol>
Returns optional_target names of the subscription from optional_targets field.
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.
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( = {}) subscribed_at = [:subscribed_at] || Time.current with_email_subscription = .has_key?(:with_email_subscription) ? [:with_email_subscription] : ActivityNotification.config.subscribe_to_email_as_default with_optional_targets = .has_key?(:with_optional_targets) ? [: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.
167 168 169 170 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 167 def subscribe_to_email( = {}) subscribed_to_email_at = [: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.
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, = {}) subscribed_at = [: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.
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.
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 149 def unsubscribe( = {}) unsubscribed_at = [: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.
177 178 179 180 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 177 def unsubscribe_to_email( = {}) unsubscribed_to_email_at = [: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.
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, = {}) unsubscribed_at = [: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 |