Module: ActivityNotification::Subscriber
- Extended by:
- ActiveSupport::Concern
- Includes:
- Association
- Defined in:
- lib/activity_notification/models/concerns/subscriber.rb
Overview
Subscriber implementation included in target model to manage subscriptions, like users or administrators.
Class Method Summary collapse
-
.available_as_subscriber? ⇒ Boolean
Checks if the model includes subscriber and subscriber methods are available.
Instance Method Summary collapse
-
#build_subscription(subscription_params = {}) ⇒ Subscription
Builds new subscription of the target.
-
#create_subscription(subscription_params = {}) ⇒ Subscription
Creates new subscription of the target.
-
#find_or_create_subscription(key, subscription_params = {}) ⇒ Subscription
Gets subscription of the target and notification key.
-
#find_subscription(key) ⇒ Subscription
Gets subscription of the target and notification key.
-
#notification_keys(options = {}) ⇒ Array<Notificaion>
Gets received notification keys of the target.
-
#subscription_index(options = {}) ⇒ Array<Notificaion>
Gets configured subscription index of the target.
-
#subscriptions ⇒ Array<Subscription>, Mongoid::Criteria<Subscription>
Has many subscription instances of this target.
Methods included from Association
belongs_to_composite_xdb_record, belongs_to_polymorphic_xdb_record, filtered_by_association, has_many_composite_xdb_records, has_many_polymorphic_xdb_records, has_many_records, #update
Class Method Details
.available_as_subscriber? ⇒ Boolean
Checks if the model includes subscriber and subscriber methods are available. Also checks if the model includes target and target methods are available, then return true.
22 23 24 |
# File 'lib/activity_notification/models/concerns/subscriber.rb', line 22 def available_as_subscriber? available_as_target? end |
Instance Method Details
#build_subscription(subscription_params = {}) ⇒ Subscription
Builds new subscription of the target.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/activity_notification/models/concerns/subscriber.rb', line 61 def build_subscription(subscription_params = {}) created_at = Time.current if subscription_params[:subscribing] == false && subscription_params[:subscribing_to_email].nil? subscription_params[:subscribing_to_email] = subscription_params[:subscribing] elsif subscription_params[:subscribing_to_email].nil? subscription_params[:subscribing_to_email] = ActivityNotification.config.subscribe_to_email_as_default end subscription = Subscription.new(subscription_params) subscription.assign_attributes(target: self) subscription.subscribing? ? subscription.assign_attributes(subscribing: true, subscribed_at: created_at) : subscription.assign_attributes(subscribing: false, unsubscribed_at: created_at) subscription.subscribing_to_email? ? subscription.assign_attributes(subscribing_to_email: true, subscribed_to_email_at: created_at) : subscription.assign_attributes(subscribing_to_email: false, unsubscribed_to_email_at: created_at) subscription.optional_targets = (subscription.optional_targets || {}).with_indifferent_access optional_targets = {}.with_indifferent_access subscription.optional_target_names.each do |optional_target_name| optional_targets = subscription.subscribing_to_optional_target?(optional_target_name) ? 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(created_at) ) : 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(created_at) ) end subscription.assign_attributes(optional_targets: optional_targets) subscription end |
#create_subscription(subscription_params = {}) ⇒ Subscription
Creates new subscription of the target.
51 52 53 54 55 |
# File 'lib/activity_notification/models/concerns/subscriber.rb', line 51 def create_subscription(subscription_params = {}) subscription = build_subscription(subscription_params) raise RecordInvalidError, subscription.errors..first unless subscription.save subscription end |
#find_or_create_subscription(key, subscription_params = {}) ⇒ Subscription
Gets subscription of the target and notification key.
41 42 43 44 |
# File 'lib/activity_notification/models/concerns/subscriber.rb', line 41 def find_or_create_subscription(key, subscription_params = {}) subscription = find_subscription(key) subscription || create_subscription(subscription_params.merge(key: key)) end |
#find_subscription(key) ⇒ Subscription
Gets subscription of the target and notification key.
32 33 34 |
# File 'lib/activity_notification/models/concerns/subscriber.rb', line 32 def find_subscription(key) subscriptions.where(key: key).first end |
#notification_keys(options = {}) ⇒ Array<Notificaion>
Gets received notification keys of the target.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/activity_notification/models/concerns/subscriber.rb', line 124 def notification_keys( = {}) subscription_keys = subscriptions.uniq_keys target_notifications = notifications.(.select { |k, _| [:filtered_by_key, :custom_filter].include?(k) }) target_notifications = [:reverse] ? target_notifications.earliest_order : target_notifications.latest_order target_notifications = [:limit].present? ? target_notifications.limit([:limit] + subscription_keys.size) : target_notifications notification_keys = target_notifications.uniq_keys notification_keys = case [:filter] when :configured, 'configured' notification_keys & subscription_keys when :unconfigured, 'unconfigured' notification_keys - subscription_keys else notification_keys end [:limit].present? ? notification_keys.take([:limit]) : notification_keys end |
#subscription_index(options = {}) ⇒ Array<Notificaion>
Gets configured subscription index of the target.
105 106 107 108 109 110 |
# File 'lib/activity_notification/models/concerns/subscriber.rb', line 105 def subscription_index( = {}) target_index = subscriptions.() target_index = [:reverse] ? target_index.earliest_order : target_index.latest_order target_index = target_index.with_target if [:with_target] [:limit].present? ? target_index.limit([:limit]).to_a : target_index.to_a end |
#subscriptions ⇒ Array<Subscription>, Mongoid::Criteria<Subscription>
Has many subscription instances of this target.
12 13 14 15 |
# File 'lib/activity_notification/models/concerns/subscriber.rb', line 12 has_many_records :subscriptions, class_name: "::ActivityNotification::Subscription", as: :target, dependent: :delete_all |