diff --git a/javascript/futurism_channel.js b/javascript/futurism_channel.js index a406fdd..0df7c20 100644 --- a/javascript/futurism_channel.js +++ b/javascript/futurism_channel.js @@ -24,7 +24,8 @@ export const createSubscription = consumer => { 'futurism:appear', debounceEvents(events => { this.send({ - signed_params: events.map(e => e.target.dataset.signedParams) + signed_params: events.map(e => e.target.dataset.signedParams), + sgids: events.map(e => e.target.dataset.sgid) }) }) ) @@ -35,9 +36,12 @@ export const createSubscription = consumer => { CableReady.perform(data.operations, { emitMissingElementWarnings: false }) - + document.dispatchEvent( - new CustomEvent('futurism:appeared', { bubbles: true, cancelable: true }) + new CustomEvent('futurism:appeared', { + bubbles: true, + cancelable: true + }) ) } } diff --git a/lib/futurism/channel.rb b/lib/futurism/channel.rb index 3ba1afd..d29d8f8 100644 --- a/lib/futurism/channel.rb +++ b/lib/futurism/channel.rb @@ -7,21 +7,28 @@ def subscribed end def receive(data) - resources = data["signed_params"].map { |signed_params| - [signed_params, Rails.application.message_verifier("futurism").verify(signed_params)] - } + 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, resource| + resources.each do |signed_params, sgid| + selector = "[data-signed-params='#{signed_params}']" cable_ready["Futurism::Channel"].outer_html( - selector: "[data-signed-params='#{signed_params}']", - html: ApplicationController.render(resource) + 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 diff --git a/test/cable/channel_test.rb b/test/cable/channel_test.rb index 1785a06..ff5fa8a 100644 --- a/test/cable/channel_test.rb +++ b/test/cable/channel_test.rb @@ -22,10 +22,11 @@ class Futurism::ChannelTest < ActionCable::Channel::TestCase renderer_spy = Spy.on(ApplicationController, :render) post = Post.create title: "Lorem" fragment = Nokogiri::HTML.fragment(futurize(post, extends: :div) {}) - signed_params = fragment.children.first["data-signed-params"] + signed_params_array = fragment.children.map { |element| element["data-signed-params"] } + sgids = fragment.children.map { |element| element["data-sgid"]} subscribe - perform :receive, {"signed_params" => [signed_params]} + perform :receive, {"signed_params" => signed_params_array, "sgids" => sgids} assert renderer_spy.has_been_called_with? post end @@ -35,13 +36,11 @@ class Futurism::ChannelTest < ActionCable::Channel::TestCase Post.create title: "Lorem" Post.create title: "Ipsum" fragment = Nokogiri::HTML.fragment(futurize(Post.all, extends: :div) {}) - signed_params = fragment.children.first["data-signed-params"] + signed_params_array = fragment.children.map { |element| element["data-signed-params"] } + sgids = fragment.children.map { |element| element["data-sgid"] } subscribe - perform :receive, {"signed_params" => [signed_params]} - - signed_params = fragment.children.last["data-signed-params"] - perform :receive, {"signed_params" => [signed_params]} + perform :receive, {"signed_params" => signed_params_array, "sgids" => sgids} assert renderer_spy.has_been_called_with? Post.first assert renderer_spy.has_been_called_with? Post.last