Class: ActivityNotification::OptionalTarget::ActionCableChannel
- Defined in:
- lib/activity_notification/optional_targets/action_cable_channel.rb
Overview
Optional target implementation to broadcast to Action Cable channel
Instance Method Summary collapse
-
#format_message(notification, options = {}) ⇒ Hash
Format message to broadcast.
-
#initialize_target(options = {}) ⇒ Object
Initialize method to prepare Action Cable channel.
-
#notification_action_cable_allowed?(notification) ⇒ Boolean
Check if Action Cable notification is allowed.
-
#notify(notification, options = {}) ⇒ Object
Broadcast to ActionCable 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
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/activity_notification/optional_targets/action_cable_channel.rb', line 54 def (notification, = {}) = .slice(:filter, :limit, :without_grouping, :with_group_members, :filtered_by_type, :filtered_by_group_type, :filtered_by_group_id, :filtered_by_key, :later_than, :earlier_than) { id: notification.id, view: (notification, ), text: notification.text(), notifiable_path: notification.notifiable_path, group_owner_id: notification.group_owner_id, group_owner_view: notification.group_owner? ? nil : (notification.group_owner, ), unopened_notification_count: notification.target.unopened_notification_count() } end |
#initialize_target(options = {}) ⇒ Object
Initialize method to prepare Action Cable channel
9 10 11 12 |
# File 'lib/activity_notification/optional_targets/action_cable_channel.rb', line 9 def initialize_target( = {}) @channel_prefix = .delete(:channel_prefix) || ActivityNotification.config.notification_channel_prefix @composite_key_delimiter = .delete(:composite_key_delimiter) || ActivityNotification.config.composite_key_delimiter end |
#notification_action_cable_allowed?(notification) ⇒ Boolean
Check if Action Cable notification is allowed
45 46 47 48 |
# File 'lib/activity_notification/optional_targets/action_cable_channel.rb', line 45 def notification_action_cable_allowed?(notification) notification.target.notification_action_cable_allowed?(notification.notifiable, notification.key) && notification.notifiable.notifiable_action_cable_allowed?(notification.target, notification.key) end |
#notify(notification, options = {}) ⇒ Object
Broadcast to ActionCable subscribers
34 35 36 37 38 39 40 |
# File 'lib/activity_notification/optional_targets/action_cable_channel.rb', line 34 def notify(notification, = {}) if notification_action_cable_allowed?(notification) target_channel_name = "#{@channel_prefix}_#{notification.target_type}#{@composite_key_delimiter}#{notification.target_id}" = .slice(:filter, :limit, :without_grouping, :with_group_members, :filtered_by_type, :filtered_by_group_type, :filtered_by_group_id, :filtered_by_key, :later_than, :earlier_than) ActionCable.server.broadcast(target_channel_name, (notification, )) end end |