Module: ActivityNotification::SubscriptionApi
- Extended by:
- ActiveSupport::Concern
- Included in:
- Subscription
- Defined in:
- lib/activity_notification/apis/subscription_api.rb
Overview
Defines API for subscription included in Subscription model.
Class Method Summary collapse
-
.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
-
#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_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
.to_optional_target_key(optional_target_name) ⇒ Symbol
Returns key of optional_targets hash from symbol class name of the optional target implementation.
10 11 12 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 10 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.
17 18 19 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 17 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.
24 25 26 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 24 def to_optional_target_unsubscribed_at_key(optional_target_name) ("unsubscribed_to_" + optional_target_name.to_s + "_at").to_sym end |
Instance Method Details
#optional_target_names ⇒ Array<Symbol>
Returns optional_target names of the subscription from optional_targets field.
132 133 134 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 132 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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 36 def subscribe( = {}) subscribed_at = [:subscribed_at] || Time.current with_email_subscription = .has_key?(:with_email_subscription) ? [:with_email_subscription] : true with_optional_targets = .has_key?(:with_optional_targets) ? [:with_optional_targets] : true 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) => subscribed_at) end end update(new_attributes) end |
#subscribe_to_email(options = {}) ⇒ Boolean
Subscribes to the notification email.
75 76 77 78 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 75 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.
108 109 110 111 112 113 114 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 108 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) => subscribed_at) ) end |
#subscribing_to_optional_target?(optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_as_default) ⇒ Boolean
Returns if the target subscribes to the specified optional target.
95 96 97 98 99 100 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 95 def subscribing_to_optional_target?(optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_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.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 57 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) => subscribed_at) end update(new_attributes) end |
#unsubscribe_to_email(options = {}) ⇒ Boolean
Unsubscribes to the notification email.
85 86 87 88 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 85 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.
122 123 124 125 126 127 128 |
# File 'lib/activity_notification/apis/subscription_api.rb', line 122 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) => unsubscribed_at) ) end |