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.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hash
Override as_json method for optional_targets representation.
-
#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.
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 |
#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 |