Module: UserNotification::Common
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/user_notification/common.rb
Overview
Common methods shared across the gem.
Defined Under Namespace
Modules: ClassMethods
Global options collapse
-
#notification_hooks ⇒ Hash<Symbol, Proc>
Hooks/functions that will be used to decide if the notification should get created.
-
#notification_owner_global ⇒ Model
Global version of notification owner.
-
#notification_params_global ⇒ Hash<Symbol, Object>
Global version of notification parameters.
-
#notification_recipient_global ⇒ Model
Global version of notification recipient.
Instance options collapse
-
#notification_custom_fields ⇒ Hash
Set or get custom fields for later processing.
-
#notification_key ⇒ String
Set or get custom i18n key passed to Notification, later used in Renderable#text.
-
#notification_owner ⇒ Model
Set or get owner object responsible for the Notification.
-
#notification_params ⇒ Hash<Symbol, Object>
Set or get parameters that will be passed to Notification when saving.
-
#notification_recipient ⇒ Model
Set or get recipient for notification.
Instance Method Summary collapse
-
#call_hook_safe(key) ⇒ Boolean
private
Calls hook safely.
-
#create_notification(*args) ⇒ Model?
Directly creates notification record in the database, based on supplied options.
-
#extract_key(action, options = {}) ⇒ String
Helper method to serialize class name into relevant key.
-
#get_hook(key) ⇒ Proc?
private
Shortcut for ClassMethods#get_hook.
-
#prepare_settings(*args) ⇒ Hash
private
Prepares settings used during creation of Notification record.
-
#reset_notification_instance_options ⇒ Object
Resets all instance options on the object triggered by a successful #create_notification, should not be called from any other place, or from application code.
-
#user_notification_enabled? ⇒ Boolean
private
Returns true if UserNotification is enabled globally and for this class.
Instance Attribute Details
#notification_custom_fields ⇒ Hash
Set or get custom fields for later processing
115 116 117 |
# File 'lib/user_notification/common.rb', line 115 def notification_custom_fields @notification_custom_fields end |
#notification_hooks ⇒ Hash<Symbol, Proc>
Hooks/functions that will be used to decide if the notification should get created.
The supported keys are:
-
:create
-
:update
-
:destroy
|
# File 'lib/user_notification/common.rb', line 48
|
#notification_key ⇒ String
Set or get custom i18n key passed to Notification, later used in Renderable#text
Usage:
@article = Article.new
@article.notification_key = "my.custom.article.key"
@article.save
@article.notifications.last.key #=> "my.custom.article.key"
109 110 111 |
# File 'lib/user_notification/common.rb', line 109 def notification_key @notification_key end |
#notification_owner ⇒ Model
Set or get owner object responsible for the Notification.
Usage:
# where current_user is an object of logged in user
@article.notification_owner = current_user
# OR: take @article.author association
@article.notification_owner = :author
# OR: provide a Proc with custom code
@article.notification_owner = proc {|controller, model| model. }
@article.save
@article.notifications.last.owner #=> Returns owner object
88 89 90 |
# File 'lib/user_notification/common.rb', line 88 def notification_owner @notification_owner end |
#notification_owner_global ⇒ Model
Global version of notification owner
|
# File 'lib/user_notification/common.rb', line 33
|
#notification_params ⇒ Hash<Symbol, Object>
Set or get parameters that will be passed to Notification when saving
Usage:
@article.notification_params = {:article_title => @article.title}
@article.save
This way you can pass strings that should remain constant, even when model attributes change after creating this Notification.
72 73 74 |
# File 'lib/user_notification/common.rb', line 72 def notification_params @notification_params end |
#notification_params_global ⇒ Hash<Symbol, Object>
Global version of notification parameters
|
# File 'lib/user_notification/common.rb', line 43
|
#notification_recipient ⇒ Model
Set or get recipient for notification.
Association is polymorphic, thus allowing assignment of all types of models. This can be used for example in the case of sending private notifications for only a single user.
97 98 99 |
# File 'lib/user_notification/common.rb', line 97 def notification_recipient @notification_recipient end |
#notification_recipient_global ⇒ Model
Global version of notification recipient
|
# File 'lib/user_notification/common.rb', line 38
|
Instance Method Details
#call_hook_safe(key) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Calls hook safely. If a hook for given action exists, calls it with model (self) and controller (if available, see StoreController)
181 182 183 184 185 186 187 188 189 |
# File 'lib/user_notification/common.rb', line 181 def call_hook_safe(key) hook = self.get_hook(key) if hook # provides hook with model and controller hook.call(self, UserNotification.get_controller) else true end end |
#create_notification(action, options = {}) ⇒ Model? #create_notification(options = {}) ⇒ Model?
Directly creates notification record in the database, based on supplied options.
It’s meant for creating custom notifications while preserving all configuration defined before. If you fire up the simplest of options:
current_user.create_notification(:avatar_changed)
It will still gather data from any procs or symbols you passed as params to Notifiable::ClassMethods#notifiable. It will ask the hooks you defined whether to really save this notification.
But you can also overwrite instance and global settings with your options:
@article.notification :owner => proc {|controller| controller.current_user }
@article.create_notification(:commented_on, :owner => @user)
And it’s smart! It won’t execute your proc, since you’ve chosen to overwrite instance parameter :owner with @user.
- :key
-
The key will be generated from either:
-
the first parameter you pass that is not a hash (action)
-
the :action option in the options hash (action)
-
the :key option in the options hash (it has to be a full key, including model name)
When you pass an action (first two options above), they will be added to parameterized model name:
Given Article model and instance: @article,
@article.create_notification :commented_on @article.notifications.last.key # => "article.commented_on"
-
For other parameters, see Notifiable#notification, and “Instance options” accessors at Notifiable, information on hooks is available at Notifiable::ClassMethods#notifiable.
248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/user_notification/common.rb', line 248 def create_notification(*args) return unless self.user_notification_enabled? = prepare_settings(*args) if call_hook_safe([:key].split('.').last) return UserNotification::Adapter.create_notification(self, ) end nil end |
#extract_key(action, options = {}) ⇒ String
Helper method to serialize class name into relevant key
324 325 326 327 328 |
# File 'lib/user_notification/common.rb', line 324 def extract_key(action, = {}) ([:key] || self.notification_key || ((self.class.name.underscore.gsub('/', '_') + "." + action.to_s) if action) ).try(:to_s) end |
#get_hook(key) ⇒ Proc?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Shortcut for UserNotification::Common::ClassMethods#get_hook
170 171 172 |
# File 'lib/user_notification/common.rb', line 170 def get_hook(key) self.class.get_hook(key) end |
#prepare_settings(action, options = {}) ⇒ Hash #prepare_settings(options = {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prepares settings used during creation of Notification record. params passed directly to notifiable model have priority over settings specified in notifiable() method
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/user_notification/common.rb', line 271 def prepare_settings(*args) # key = args. = { key: .delete(:key), action: .delete(:action), parameters: .delete(:parameters) || .delete(:params) } action = (args.first || [:action]).try(:to_s) [:key] = extract_key(action, ) raise NoKeyProvided, "No key provided for #{self.class.name}" unless [:key] .delete(:action) # user responsible for the notification [:owner] = UserNotification.resolve_value(self, (.has_key?(:owner) ? [:owner] : ( self.notification_owner || self.class.notification_owner_global ) ) ) # recipient of the notification [:recipient] = UserNotification.resolve_value(self, (.has_key?(:recipient) ? [:recipient] : ( self.notification_recipient || self.class.notification_recipient_global ) ) ) #customizable parameters params = {} params.merge!(self.class.notification_params_global) params.merge!(self.notification_params) if self.notification_params params.merge!([:params] || [:parameters] || {}) params.each { |k, v| params[k] = UserNotification.resolve_value(self, v) } [:parameters] = params .delete(:params) customs = self.class.notification_custom_fields_global.clone customs.merge!(self.notification_custom_fields) if self.notification_custom_fields customs.merge!() customs.each do |k, v| customs[k] = UserNotification.resolve_value(self, v) end.merge end |
#reset_notification_instance_options ⇒ Object
Resets all instance options on the object triggered by a successful #create_notification, should not be called from any other place, or from application code.
334 335 336 337 338 339 340 |
# File 'lib/user_notification/common.rb', line 334 def @notification_params = {} @notification_key = nil @notification_owner = nil @notification_recipient = nil @notification_custom_fields = {} end |
#user_notification_enabled? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true if UserNotification is enabled globally and for this class.
161 162 163 |
# File 'lib/user_notification/common.rb', line 161 def user_notification_enabled? UserNotification.enabled? end |