Sha256: c9f3fdb130e20541de9cf21b5b5dec67e6cf4a83b28cfaa47180e99d559a6845

Contents?: true

Size: 1.24 KB

Versions: 37

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/object/to_param"

module ActionCable
  module Channel
    module Broadcasting
      extend ActiveSupport::Concern

      delegate :broadcasting_for, :broadcast_to, to: :class

      module ClassMethods
        # Broadcast a hash to a unique broadcasting for this <tt>model</tt> in this channel.
        def broadcast_to(model, message)
          ActionCable.server.broadcast(broadcasting_for(model), message)
        end

        # Returns a unique broadcasting identifier for this <tt>model</tt> in this channel:
        #
        #    CommentsChannel.broadcasting_for("all") # => "comments:all"
        #
        # You can pass any object as a target (e.g. Active Record model), and it
        # would be serialized into a string under the hood.
        def broadcasting_for(model)
          serialize_broadcasting([ channel_name, model ])
        end

        def serialize_broadcasting(object) # :nodoc:
          case
          when object.is_a?(Array)
            object.map { |m| serialize_broadcasting(m) }.join(":")
          when object.respond_to?(:to_gid_param)
            object.to_gid_param
          else
            object.to_param
          end
        end
      end
    end
  end
end

Version data entries

37 entries across 35 versions & 4 rubygems

Version Path
actioncable-7.0.8.6 lib/action_cable/channel/broadcasting.rb
actioncable-7.0.8.5 lib/action_cable/channel/broadcasting.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actioncable-7.0.8.4/lib/action_cable/channel/broadcasting.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actioncable-7.0.5.1/lib/action_cable/channel/broadcasting.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actioncable-7.0.5.1/lib/action_cable/channel/broadcasting.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actioncable-7.0.5.1/lib/action_cable/channel/broadcasting.rb
actioncable-7.0.8.4 lib/action_cable/channel/broadcasting.rb
actioncable-7.0.8.1 lib/action_cable/channel/broadcasting.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actioncable-7.0.3.1/lib/action_cable/channel/broadcasting.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actioncable-7.0.2.3/lib/action_cable/channel/broadcasting.rb
actioncable-7.0.8 lib/action_cable/channel/broadcasting.rb
actioncable-7.0.7.2 lib/action_cable/channel/broadcasting.rb
actioncable-7.0.7.1 lib/action_cable/channel/broadcasting.rb
actioncable-7.0.7 lib/action_cable/channel/broadcasting.rb
actioncable-7.0.6 lib/action_cable/channel/broadcasting.rb
actioncable-7.0.5.1 lib/action_cable/channel/broadcasting.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actioncable-7.0.3.1/lib/action_cable/channel/broadcasting.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actioncable-7.0.2.3/lib/action_cable/channel/broadcasting.rb
actioncable-7.0.5 lib/action_cable/channel/broadcasting.rb
actioncable-7.0.4.3 lib/action_cable/channel/broadcasting.rb