Class: ActivityNotification::OptionalTarget::ActionCableApiChannel
- Defined in:
- lib/activity_notification/optional_targets/action_cable_api_channel.rb
Overview
Optional target implementation to broadcast to Action Cable API channel
Instance Method Summary collapse
-
#format_message(notification, options = {}) ⇒ Hash
Format message to broadcast.
-
#initialize_target(options = {}) ⇒ Object
Initialize method to prepare Action Cable API channel.
-
#notification_action_cable_api_allowed?(notification) ⇒ Boolean
Check if Action Cable notification API is allowed.
-
#notify(notification, options = {}) ⇒ Object
Broadcast to ActionCable API subscribers.
Methods inherited from Base
#initialize, #to_optional_target_name
Constructor Details
This class inherits a constructor from ActivityNotification::OptionalTarget::Base
Instance Method Details
#format_message(notification, options = {}) ⇒ Hash
Format message to broadcast
36 37 38 39 40 41 |
# File 'lib/activity_notification/optional_targets/action_cable_api_channel.rb', line 36 def (notification, = {}) { notification: notification.as_json(.merge()), group_owner: notification.group_owner? ? nil : notification.group_owner.as_json(.merge()) } end |
#initialize_target(options = {}) ⇒ Object
Initialize method to prepare Action Cable API channel
9 10 11 12 |
# File 'lib/activity_notification/optional_targets/action_cable_api_channel.rb', line 9 def initialize_target( = {}) @channel_prefix = .delete(:channel_prefix) || ActivityNotification.config.notification_api_channel_prefix @composite_key_delimiter = .delete(:composite_key_delimiter) || ActivityNotification.config.composite_key_delimiter end |
#notification_action_cable_api_allowed?(notification) ⇒ Boolean
Check if Action Cable notification API is allowed
27 28 29 30 |
# File 'lib/activity_notification/optional_targets/action_cable_api_channel.rb', line 27 def notification_action_cable_api_allowed?(notification) notification.target.notification_action_cable_allowed?(notification.notifiable, notification.key) && notification.notifiable.notifiable_action_cable_api_allowed?(notification.target, notification.key) end |
#notify(notification, options = {}) ⇒ Object
Broadcast to ActionCable API subscribers
17 18 19 20 21 22 |
# File 'lib/activity_notification/optional_targets/action_cable_api_channel.rb', line 17 def notify(notification, = {}) if notification_action_cable_api_allowed?(notification) target_channel_name = "#{@channel_prefix}_#{notification.target_type}#{@composite_key_delimiter}#{notification.target_id}" ActionCable.server.broadcast(target_channel_name, (notification, )) end end |