Module: ActivityNotification::Subscriber

Extended by:
ActiveSupport::Concern
Defined in:
lib/activity_notification/models/concerns/subscriber.rb

Overview

Subscriber implementation included in target model to manage subscriptions, like users or administrators.

Instance Method Summary collapse

Instance Method Details

#build_subscription(subscription_params = {}) ⇒ Subscription

Builds new subscription of the target.

Parameters:

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

    Parameters to build subscription record

Returns:



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.

Parameters:

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

    Parameters to create subscription record

Returns:

Raises:



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.full_messages.first unless subscription.save
  subscription
end

#find_or_create_subscription(key, subscription_params = {}) ⇒ Subscription

Gets subscription of the target and notification key.

Parameters:

  • key (Hash)

    Key of the notification for subscription

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

    Parameters to create subscription record

Returns:



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.

Parameters:

  • key (Hash)

    Key of the notification for subscription

Returns:



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.

Examples:

Get unconfigured notification keys of the @user

@notification_keys = @user.notification_keys(filter: :unconfigured)

Parameters:

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

    Options for unconfigured notification keys

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for subscriptions

  • :reverse (Boolean) — default: false

    If notification keys will be ordered as earliest first

  • :filter (Symbol|String) — default: nil

    Filter option to load notification keys (Nothing as all, ‘configured’ with configured subscriptions or ‘unconfigured’ without subscriptions)

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :custom_filter (Array|Hash) — default: nil

    Custom subscription filter (e.g. [“created_at >= ?”, time.hour.ago])

Returns:

  • (Array<Notificaion>)

    Unconfigured 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(options = {})
  subscription_keys    = subscriptions.uniq_keys
  target_notifications = notifications.filtered_by_options(options.select { |k, _| [:filtered_by_key, :custom_filter].include?(k) })
  target_notifications = options[:reverse] ? target_notifications.earliest_order : target_notifications.latest_order
  target_notifications = options[:limit].present? ? target_notifications.limit(options[:limit] + subscription_keys.size) : target_notifications
  notification_keys    = target_notifications.uniq_keys
  notification_keys    =
    case options[:filter]
    when :configured, 'configured'
      notification_keys & subscription_keys
    when :unconfigured, 'unconfigured'
      notification_keys - subscription_keys
    else
      notification_keys
    end
  options[:limit].present? ? notification_keys.take(options[:limit]) : notification_keys
end

#subscription_index(options = {}) ⇒ Array<Notificaion>

Gets configured subscription index of the target.

Examples:

Get configured subscription index of the @user

@subscriptions = @user.subscription_index

Parameters:

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

    Options for subscription index

Options Hash (options):

  • :limit (Integer) — default: nil

    Limit to query for subscriptions

  • :reverse (Boolean) — default: false

    If subscription index will be ordered as earliest first

  • :filtered_by_key (String) — default: nil

    Key of the notification for filter

  • :custom_filter (Array|Hash) — default: nil

    Custom subscription filter (e.g. [“created_at >= ?”, time.hour.ago])

  • :with_target (Boolean) — default: false

    If it includes target with subscriptions

Returns:

  • (Array<Notificaion>)

    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(options = {})
  target_index = subscriptions.filtered_by_options(options)
  target_index = options[:reverse] ? target_index.earliest_order : target_index.latest_order
  target_index = target_index.with_target if options[:with_target]
  options[:limit].present? ? target_index.limit(options[:limit]).to_a : target_index.to_a
end