Sha256: afc5bd533a9be4c1234e10842ac2eec35cf5348a9fdb952934433b806f3b0f57

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 KB

Contents

module Futurism
  class Channel < ActionCable::Channel::Base
    include CableReady::Broadcaster

    def stream_name
      ids = connection.identifiers.map { |identifier| send(identifier).try(:id) || send(identifier) }
      [
        params[:channel],
        ids.select(&:present?).join(";")
      ].select(&:present?).join(":")
    end

    def subscribed
      stream_from stream_name
    end

    def receive(data)
      resources = data.fetch_values("signed_params", "sgids") { |key| [nil] }.transpose

      new_env = connection.env.merge(ApplicationController.renderer.instance_variable_get(:@env))
      ApplicationController.renderer.instance_variable_set(:@env, new_env)

      resources.each do |signed_params, sgid|
        selector = "[data-signed-params='#{signed_params}']"
        selector << "[data-sgid='#{sgid}']" if sgid.present?
        cable_ready[stream_name].outer_html(
          selector: selector,
          html: ApplicationController.render(resource(signed_params: signed_params, sgid: sgid))
        )
      end

      cable_ready.broadcast
    end

    private

    def resource(signed_params:, sgid:)
      return GlobalID::Locator.locate_signed(sgid) if sgid.present?

      Rails
        .application
        .message_verifier("futurism")
        .verify(signed_params)
        .deep_transform_values { |value| value.is_a?(String) && value.start_with?("gid://") ? GlobalID::Locator.locate(value) : value }
    end
  end
end

Version data entries

7 entries across 4 versions & 1 rubygems

Version Path
futurism-0.5.4 lib/futurism/channel.rb
futurism-0.5.4 lib/futurism/channel.rb~
futurism-0.5.2 lib/futurism/channel.rb
futurism-0.5.2 lib/futurism/channel.rb~
futurism-0.5.1 lib/futurism/channel.rb
futurism-0.5.1 lib/futurism/channel.rb~
futurism-0.5.0 lib/futurism/channel.rb