Module: ActivityNotification::ActsAsTarget

Extended by:
ActiveSupport::Concern
Included in:
Models
Defined in:
lib/activity_notification/roles/acts_as_target.rb

Overview

Manages to add all required configurations to target models of notification.

Class Method Summary collapse

Class Method Details

.acts_as_target(options = {}) ⇒ Hash Also known as: acts_as_notification_target

Adds required configurations to notifiable models.

Parameters:

  • :email

    • Email address to send notification email. This is a necessary option when you enables email notification.

  • :email_allowed

    • Whether activity_notification sends notification email to this target. Specified method or symbol is expected to return true (not nil) or false (nil). This parameter is a optional since default value is false. To use notification email, email_allowed option must return true (not nil) in both of notifiable and target model. This can be also configured default option in initializer.

  • :batch_email_allowed

    • Whether activity_notification sends batch notification email to this target. Specified method or symbol is expected to return true (not nil) or false (nil). This parameter is a optional since default value is false. To use batch notification email, both of batch_email_allowed and subscription_allowed options must return true (not nil) in target model. This can be also configured default option in initializer.

  • :subscription_allowed

    • Whether activity_notification manages subscriptions of this target. Specified method or symbol is expected to return true (not nil) or false (nil). This parameter is a optional since default value is false. This can be also configured default option in initializer.

  • :action_cable_allowed

    • Whether activity_notification publishes WebSocket notifications using ActionCable to this target. Specified method or symbol is expected to return true (not nil) or false (nil). This parameter is a optional since default value is false. To use ActionCable for notifications, action_cable_enabled option must return true (not nil) in both of notifiable and target model. This can be also configured default option in initializer.

  • :action_cable_with_devise

    • Whether activity_notification publishes WebSocket notifications using ActionCable only to authenticated target with Devise. Specified method or symbol is expected to return true (not nil) or false (nil). This parameter is a optional since default value is false. To use ActionCable for notifications, also action_cable_enabled option must return true (not nil) in the target model.

  • :devise_resource

    • Integrated resource with devise authentication. This parameter is a optional since ‘self` is used as default value. You also have to configure routing for devise in routes.rb

  • :current_devise_target

    • Current authenticated target by devise authentication. This parameter is a optional since ‘current_<devise_resource_name>` is used as default value. In addition, this parameter is only needed when :devise_default_route in your route.rb is enabled. You also have to configure routing for devise in routes.rb

  • :printable_name or :printable_notification_target_name

    • Printable notification target name. This parameter is a optional since ‘ActivityNotification::Common.printable_name` is used as default value. :printable_name is the same option as :printable_notification_target_name

Examples:

Simply use :email field

# app/models/user.rb
class User < ActiveRecord::Base
  validates :email, presence: true
  acts_as_target email: :email
end

Always enable email notification for this target

# app/models/user.rb
class User < ActiveRecord::Base
  acts_as_target email: :email, email_allowed: true
end

Use confirmed_at of devise field to decide whether activity_notification sends notification email to this user

# app/models/user.rb
class User < ActiveRecord::Base
  acts_as_target email: :email, email_allowed: :confirmed_at
end

Always enable batch email notification for this target

# app/models/user.rb
class User < ActiveRecord::Base
  acts_as_target email: :email, batch_email_allowed: true
end

Use confirmed_at of devise field to decide whether activity_notification sends batch notification email to this user

# app/models/user.rb
class User < ActiveRecord::Base
  acts_as_target email: :email, batch_email_allowed: :confirmed_at
end

Subscribe notifications for this target

# app/models/user.rb
class User < ActiveRecord::Base
  acts_as_target subscription_allowed: true
end

Enable notification ActionCable for this target

# app/models/user.rb
class User < ActiveRecord::Base
  acts_as_target action_cable_allowed: true
end

Enable notification ActionCable for this target

# app/models/user.rb
class User < ActiveRecord::Base
  acts_as_target action_cable_allowed: true, action_cable_with_devise* true
end

No :devise_resource is needed when notification target is the same as authenticated resource

# config/routes.rb
devise_for :users
notify_to :users

# app/models/user.rb
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :confirmable
  acts_as_target email: :email, email_allowed: :confirmed_at
end

Send Admin model and use associated User model with devise authentication

# config/routes.rb
devise_for :users
notify_to :admins, with_devise: :users

# app/models/user.rb
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :confirmable
end

# app/models/admin.rb
class Admin < ActiveRecord::Base
  belongs_to :user
  validates :user, presence: true
  acts_as_notification_target email: :email,
    email_allowed: ->(admin, key) { admin.user.confirmed_at.present? },
    devise_resource: :user
end

No :current_devise_target is needed when notification target is the same as authenticated resource

# config/routes.rb
devise_for :users
notify_to :users

# app/models/user.rb
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :confirmable
  acts_as_target email: :email, email_allowed: :confirmed_at
end

Send Admin model and use associated User model with devise authentication

# config/routes.rb
devise_for :users
notify_to :admins, with_devise: :users

# app/models/user.rb
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :confirmable
end

# app/models/admin.rb
class Admin < ActiveRecord::Base
  belongs_to :user
  validates :user, presence: true
  acts_as_notification_target email: :email,
    email_allowed: ->(admin, key) { admin.user.confirmed_at.present? },
    devise_resource: :user,
    current_devise_target: ->(current_user) { current_user.admin }
end

Define printable name with user name of name field

# app/models/user.rb
class User < ActiveRecord::Base
  acts_as_target printable_name: :name
end

Define printable name with associated user name

# app/models/admin.rb
class Admin < ActiveRecord::Base
  acts_as_target printable_notification_target_name: ->(admin) { "admin (#{admin.user.name})" }
end

Parameters:

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

    Options for notifiable model configuration

Options Hash (options):

  • :email (Symbol, Proc, String) — default: nil

    Email address to send notification email

  • :email_allowed (Symbol, Proc, Boolean) — default: ActivityNotification.config.email_enabled

    Whether activity_notification sends notification email to this target

  • :batch_email_allowed (Symbol, Proc, Boolean) — default: ActivityNotification.config.email_enabled

    Whether activity_notification sends batch notification email to this target

  • :subscription_allowed (Symbol, Proc, Boolean) — default: ActivityNotification.config.subscription_enabled

    Whether activity_notification manages subscriptions of this target

  • :action_cable_allowed (Symbol, Proc, Boolean) — default: ActivityNotification.config.action_cable_enabled

    Whether activity_notification publishes WebSocket notifications using ActionCable to this target

  • :action_cable_with_devise (Symbol, Proc, Boolean) — default: false

    Whether activity_notification publishes WebSocket notifications using ActionCable only to authenticated target with Devise

  • :devise_resource (Symbol, Proc, Object) — default: ->(model) { model }

    Integrated resource with devise authentication

  • :current_devise_target (Symbol, Proc, Object) — default: ->(current_resource) { current_resource }

    Current authenticated target by devise authentication

  • :printable_name (Symbol, Proc, String) — default: ActivityNotification::Common.printable_name

    Printable notification target name

Returns:

  • (Hash)

    Configured parameters as target model



185
186
187
188
189
190
191
192
193
194
# File 'lib/activity_notification/roles/acts_as_target.rb', line 185

def acts_as_target(options = {})
  include Target

  options[:printable_notification_target_name] ||= options.delete(:printable_name)
  options[:batch_notification_email_allowed] ||= options.delete(:batch_email_allowed)
  acts_as_params = set_acts_as_parameters([:email, :email_allowed, :subscription_allowed, :action_cable_allowed, :action_cable_with_devise, :devise_resource, :current_devise_target], options, "notification_")
                     .merge set_acts_as_parameters([:batch_notification_email_allowed, :printable_notification_target_name], options)
  include Subscriber if subscription_enabled?
  acts_as_params
end

.available_target_optionsArray<Symbol>

Returns array of available target options in acts_as_target.

Returns:

  • (Array<Symbol>)

    Array of available target options



199
200
201
# File 'lib/activity_notification/roles/acts_as_target.rb', line 199

def available_target_options
  [:email, :email_allowed, :batch_email_allowed, :subscription_allowed, :action_cable_enabled, :action_cable_with_devise, :devise_resource, :printable_notification_target_name, :printable_name].freeze
end