Skip to content

Commit

Permalink
Retrieve SGIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
julianrubisch committed Aug 4, 2020
1 parent cdc6f91 commit 6a8bd9d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
10 changes: 7 additions & 3 deletions javascript/futurism_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
)
Expand All @@ -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
})
)
}
}
Expand Down
19 changes: 13 additions & 6 deletions lib/futurism/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 6 additions & 7 deletions test/cable/channel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6a8bd9d

Please sign in to comment.