Module: UserNotification::ActsAsNotifiable

Extended by:
ActiveSupport::Concern
Defined in:
lib/user_notification/roles/acts_as_notifiable.rb

Overview

Main module extending classes we want to keep track of.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#notification(options = {}) ⇒ nil

A shortcut method for setting custom key, owner and parameters of Notification in one line. Accepts a hash with 3 keys: :key, :owner, :params. You can specify all of them or just the ones you want to overwrite.

Options

:key

See Common#notification_key

:owner

See Common#notification_owner

:params

See Common#notification_params

:recipient

Set the recipient for this notification. Useful for private notifications, which should only be visible to a certain user. See Common#notification_recipient.

Examples:


@article = Article.new
@article.title = "New article"
@article.notification :key => "my.custom.article.key", :owner => @article.author, :params => {:title => @article.title}
@article.save
@article.notifications.last.key #=> "my.custom.article.key"
@article.notifications.last.parameters #=> {:title => "New article"}

Parameters:

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

    instance options to set on the notifiable model

Returns:

  • (nil)


30
31
32
33
34
35
36
37
38
# File 'lib/user_notification/roles/acts_as_notifiable.rb', line 30

def notification(options = {})
  rest = options.clone
  self.notification_key = rest.delete(:key) if rest[:key]
  self.notification_owner = rest.delete(:owner) if rest[:owner]
  self.notification_params = rest.delete(:params) if rest[:params]
  self.notification_recipient = rest.delete(:recipient) if rest[:recipient]
  self.notification_custom_fields = rest if rest.count > 0
  nil
end