Sha256: 808e97a5477ad73a46e65cbeeefa78d825759688d2d0182a23cab692a3240f16

Contents?: true

Size: 1.4 KB

Versions: 25

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

# :markup: markdown

require "active_support/core_ext/object/to_param"

module ActionCable
  module Channel
    module Broadcasting
      extend ActiveSupport::Concern

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

        # Returns a unique broadcasting identifier for this `model` 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

        private
          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

      def broadcasting_for(model)
        self.class.broadcasting_for(model)
      end

      def broadcast_to(model, message)
        self.class.broadcast_to(model, message)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
actioncable-next-0.1.2 lib/action_cable/channel/broadcasting.rb
actioncable-8.0.1 lib/action_cable/channel/broadcasting.rb
actioncable-8.0.0.1 lib/action_cable/channel/broadcasting.rb
actioncable-7.2.2.1 lib/action_cable/channel/broadcasting.rb
actioncable-8.0.0 lib/action_cable/channel/broadcasting.rb
actioncable-7.2.2 lib/action_cable/channel/broadcasting.rb
actioncable-8.0.0.rc2 lib/action_cable/channel/broadcasting.rb
actioncable-7.2.1.2 lib/action_cable/channel/broadcasting.rb
actioncable-8.0.0.rc1 lib/action_cable/channel/broadcasting.rb
actioncable-7.2.1.1 lib/action_cable/channel/broadcasting.rb
actioncable-next-0.1.1 lib/action_cable/channel/broadcasting.rb
actioncable-next-0.1.0 lib/action_cable/channel/broadcasting.rb
actioncable-8.0.0.beta1 lib/action_cable/channel/broadcasting.rb
omg-actioncable-8.0.0.alpha9 lib/action_cable/channel/broadcasting.rb
omg-actioncable-8.0.0.alpha8 lib/action_cable/channel/broadcasting.rb
omg-actioncable-8.0.0.alpha7 lib/action_cable/channel/broadcasting.rb
omg-actioncable-8.0.0.alpha4 lib/action_cable/channel/broadcasting.rb
omg-actioncable-8.0.0.alpha3 lib/action_cable/channel/broadcasting.rb
omg-actioncable-8.0.0.alpha2 lib/action_cable/channel/broadcasting.rb
actioncable-7.2.1 lib/action_cable/channel/broadcasting.rb