Sha256: 745905af434069a88b01b2050f25a8077b86097c0451bae796f2820fb12e8124

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

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

    def subscribed
      stream_from "Futurism::Channel"
    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["Futurism::Channel"].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)
    end
  end
end

Version data entries

3 entries across 2 versions & 1 rubygems

Version Path
futurism-0.4.1 lib/futurism/channel.rb~
futurism-0.4.0 lib/futurism/channel.rb
futurism-0.4.0 lib/futurism/channel.rb~