Module: ActivityNotification::Notifiable
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActionDispatch::Routing::PolymorphicRoutes, Association, Common, PolymorphicHelpers
- Defined in:
- lib/activity_notification/models/concerns/notifiable.rb
Overview
Notifiable implementation included in notifiable model to be notified, like comments or any other user activities.
Class Method Summary collapse
-
.available_as_notifiable? ⇒ Boolean
Checks if the model includes notifiable and notifiable methods are available.
-
.set_notifiable_class_defaults ⇒ NilClass
Sets default values to notifiable class fields.
Instance Method Summary collapse
-
#default_notification_key ⇒ String
Returns default key of the notification.
-
#default_url_options ⇒ Hash
Returns default_url_options for polymorphic_path.
-
#generated_notifications_as_notifiable ⇒ Array<Notificaion>, Mongoid::Criteria<Notificaion>
Has many notification instances for this notifiable.
-
#notifiable_path(target_type, key = nil) ⇒ String
Returns notifiable_path to move after opening notification from configured field or overriden method.
-
#notification_email_allowed?(target, key = nil) ⇒ Boolean
Returns if sending notification email is allowed for the notifiable from configured field or overriden method.
-
#notification_group(target_type, key = nil) ⇒ Object
Returns group unit of the notifications from configured field or overriden method.
-
#notification_group_expiry_delay(target_type, key = nil) ⇒ Object
Returns group expiry period of the notifications from configured field or overriden method.
-
#notification_key_for_tracked_creation ⇒ String
Returns key of the notification for tracked notifiable creation.
-
#notification_key_for_tracked_update ⇒ String
Returns key of the notification for tracked notifiable update.
-
#notification_parameters(target_type, key = nil) ⇒ Hash
Returns additional notification parameters from configured field or overriden method.
-
#notification_targets(target_type, key = nil) ⇒ Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>
Returns notification targets from configured field or overriden method.
-
#notifier(target_type, key = nil) ⇒ Object
Returns notifier of the notification from configured field or overriden method.
-
#notify(target_type, options = {}) ⇒ Array<Notificaion>
Generates notifications to configured targets with notifiable model.
-
#notify_all(targets, options = {}) ⇒ Array<Notificaion>
Generates notifications to one target.
-
#notify_to(target, options = {}) ⇒ Notification
Generates notifications to one target.
-
#optional_target_names(target_type, key = nil) ⇒ Array<Symbol>
Returns optional_target names of the notification from configured field or overriden method.
-
#optional_targets(target_type, key = nil) ⇒ Array<ActivityNotification::OptionalTarget::Base>
Returns optional_targets of the notification from configured field or overriden method.
-
#printable_notifiable_name(target, key = nil) ⇒ String
Returns printable notifiable model name to show in view or email.
Methods included from Association
belongs_to_polymorphic_xdb_record, filtered_by_association, has_many_polymorphic_xdb_records, has_many_records
Methods included from Common
#printable_name, #printable_type, #resolve_value, #to_class_name, #to_resource_name, #to_resources_name
Class Method Details
.available_as_notifiable? ⇒ Boolean
Checks if the model includes notifiable and notifiable methods are available.
43 44 45 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 43 def available_as_notifiable? true end |
.set_notifiable_class_defaults ⇒ NilClass
Sets default values to notifiable class fields.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 49 def set_notifiable_class_defaults self._notification_targets = {} self._notification_group = {} self._notification_group_expiry_delay = {} self._notifier = {} self._notification_parameters = {} self._notification_email_allowed = {} self._notifiable_path = {} self._printable_notifiable_name = {} self._optional_targets = {} nil end |
Instance Method Details
#default_notification_key ⇒ String
Returns default key of the notification. This method is able to be overriden. “#Common#to_resource_name.default” is defined as default key.
295 296 297 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 295 def default_notification_key "#{to_resource_name}.default" end |
#default_url_options ⇒ Hash
Returns default_url_options for polymorphic_path.
36 37 38 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 36 def Rails.application.routes. end |
#generated_notifications_as_notifiable ⇒ Array<Notificaion>, Mongoid::Criteria<Notificaion>
Has many notification instances for this notifiable. Dependency for these notifications can be overriden from acts_as_notifiable.
18 19 20 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 18 has_many_records :generated_notifications_as_notifiable, class_name: "::ActivityNotification::Notification", as: :notifiable |
#notifiable_path(target_type, key = nil) ⇒ String
Returns notifiable_path to move after opening notification from configured field or overriden method. This method is able to be overriden.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 159 def notifiable_path(target_type, key = nil) resolved_parameter = resolve_parameter( "notifiable_path_for_#{cast_to_resources_name(target_type)}", _notifiable_path[cast_to_resources_sym(target_type)], nil, key) unless resolved_parameter begin resolved_parameter = polymorphic_path(self) rescue NoMethodError, ActionController::UrlGenerationError raise NotImplementedError, "You have to implement #{self.class}##{__method__}, "\ "set :notifiable_path in acts_as_notifiable or "\ "set polymorphic_path routing for #{self.class}" end end resolved_parameter end |
#notification_email_allowed?(target, key = nil) ⇒ Boolean
Returns if sending notification email is allowed for the notifiable from configured field or overriden method. This method is able to be overriden.
145 146 147 148 149 150 151 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 145 def notification_email_allowed?(target, key = nil) resolve_parameter( "notification_email_allowed_for_#{cast_to_resources_name(target.class)}?", _notification_email_allowed[cast_to_resources_sym(target.class)], ActivityNotification.config.email_enabled, target, key) end |
#notification_group(target_type, key = nil) ⇒ Object
Returns group unit of the notifications from configured field or overriden method. This method is able to be overriden.
89 90 91 92 93 94 95 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 89 def notification_group(target_type, key = nil) resolve_parameter( "notification_group_for_#{cast_to_resources_name(target_type)}", _notification_group[cast_to_resources_sym(target_type)], nil, key) end |
#notification_group_expiry_delay(target_type, key = nil) ⇒ Object
Returns group expiry period of the notifications from configured field or overriden method. This method is able to be overriden.
103 104 105 106 107 108 109 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 103 def notification_group_expiry_delay(target_type, key = nil) resolve_parameter( "notification_group_expiry_delay_for_#{cast_to_resources_name(target_type)}", _notification_group_expiry_delay[cast_to_resources_sym(target_type)], nil, key) end |
#notification_key_for_tracked_creation ⇒ String
Returns key of the notification for tracked notifiable creation. This method is able to be overriden. “#Common#to_resource_name.create” is defined as default creation key.
304 305 306 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 304 def notification_key_for_tracked_creation "#{to_resource_name}.create" end |
#notification_key_for_tracked_update ⇒ String
Returns key of the notification for tracked notifiable update. This method is able to be overriden. “#Common#to_resource_name.update” is defined as default update key.
313 314 315 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 313 def notification_key_for_tracked_update "#{to_resource_name}.update" end |
#notification_parameters(target_type, key = nil) ⇒ Hash
Returns additional notification parameters from configured field or overriden method. This method is able to be overriden.
117 118 119 120 121 122 123 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 117 def notification_parameters(target_type, key = nil) resolve_parameter( "notification_parameters_for_#{cast_to_resources_name(target_type)}", _notification_parameters[cast_to_resources_sym(target_type)], {}, key) end |
#notification_targets(target_type, key = nil) ⇒ Array<Notificaion> | ActiveRecord_AssociationRelation<Notificaion>
Returns notification targets from configured field or overriden method. This method is able to be overriden.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 69 def notification_targets(target_type, key = nil) target_typed_method_name = "notification_#{cast_to_resources_name(target_type)}" resolved_parameter = resolve_parameter( target_typed_method_name, _notification_targets[cast_to_resources_sym(target_type)], nil, key) unless resolved_parameter raise NotImplementedError, "You have to implement #{self.class}##{target_typed_method_name} "\ "or set :targets in acts_as_notifiable" end resolved_parameter end |
#notifier(target_type, key = nil) ⇒ Object
Returns notifier of the notification from configured field or overriden method. This method is able to be overriden.
131 132 133 134 135 136 137 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 131 def notifier(target_type, key = nil) resolve_parameter( "notifier_for_#{cast_to_resources_name(target_type)}", _notifier[cast_to_resources_sym(target_type)], nil, key) end |
#notify(target_type, options = {}) ⇒ Array<Notificaion>
Generates notifications to configured targets with notifiable model. This method calls NotificationApi#notify internally with self notifiable instance.
246 247 248 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 246 def notify(target_type, = {}) Notification.notify(target_type, self, ) end |
#notify_all(targets, options = {}) ⇒ Array<Notificaion>
Generates notifications to one target. This method calls NotificationApi#notify_all internally with self notifiable instance.
266 267 268 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 266 def notify_all(targets, = {}) Notification.notify_all(targets, self, ) end |
#notify_to(target, options = {}) ⇒ Notification
Generates notifications to one target. This method calls NotificationApi#notify_to internally with self notifiable instance.
286 287 288 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 286 def notify_to(target, = {}) Notification.notify_to(target, self, ) end |
#optional_target_names(target_type, key = nil) ⇒ Array<Symbol>
Returns optional_target names of the notification from configured field or overriden method. This method is able to be overriden.
207 208 209 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 207 def optional_target_names(target_type, key = nil) optional_targets(target_type, key).map { |optional_target| optional_target.to_optional_target_name } end |
#optional_targets(target_type, key = nil) ⇒ Array<ActivityNotification::OptionalTarget::Base>
Returns optional_targets of the notification from configured field or overriden method. This method is able to be overriden.
193 194 195 196 197 198 199 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 193 def optional_targets(target_type, key = nil) resolve_parameter( "optional_targets_for_#{cast_to_resources_name(target_type)}", _optional_targets[cast_to_resources_sym(target_type)], [], key) end |
#printable_notifiable_name(target, key = nil) ⇒ String
Returns printable notifiable model name to show in view or email.
179 180 181 182 183 184 185 |
# File 'lib/activity_notification/models/concerns/notifiable.rb', line 179 def printable_notifiable_name(target, key = nil) resolve_parameter( "printable_notifiable_name_for_#{cast_to_resources_name(target.class)}?", _printable_notifiable_name[cast_to_resources_sym(target.class)], printable_name, target, key) end |