Skip to content

Commit

Permalink
updates_for :only option (#163)
Browse files Browse the repository at this point in the history
Co-authored-by: Julian Rubisch <[email protected]>
Co-authored-by: Andrew Erlanger <[email protected]>
  • Loading branch information
3 people authored Nov 12, 2021
1 parent 6eb1234 commit d3e2efc
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 41 deletions.
54 changes: 27 additions & 27 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ GEM
faraday
async-io (1.32.2)
async
async-pool (0.3.8)
async-pool (0.3.9)
async (>= 1.25)
builder (3.2.4)
coderay (1.1.3)
Expand All @@ -95,7 +95,7 @@ GEM
fiber-local
crass (1.0.6)
erubi (1.10.0)
faraday (1.7.0)
faraday (1.8.0)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
Expand Down Expand Up @@ -128,7 +128,7 @@ GEM
rake (>= 10.0)
globalid (0.5.2)
activesupport (>= 5.0)
i18n (1.8.10)
i18n (1.8.11)
concurrent-ruby (~> 1.0)
loofah (2.12.0)
crass (~> 1.0.2)
Expand All @@ -137,35 +137,35 @@ GEM
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.2)
method_source (0.9.2)
method_source (1.0.0)
mini_mime (1.1.2)
minitest (5.14.4)
mocha (1.13.0)
multi_json (1.15.0)
multipart-post (2.1.1)
nio4r (2.5.8)
nokogiri (1.12.5-arm64-darwin)
nokogiri (1.12.5-x86_64-linux)
racc (~> 1.4)
octokit (4.21.0)
faraday (>= 0.9)
sawyer (~> 0.8.0, >= 0.5.3)
parallel (1.20.1)
parallel (1.21.0)
parser (3.0.2.0)
ast (~> 2.4.1)
protocol-hpack (1.4.2)
protocol-http (0.22.5)
protocol-http1 (0.14.1)
protocol-http1 (0.14.2)
protocol-http (~> 0.22)
protocol-http2 (0.14.2)
protocol-hpack (~> 1.4)
protocol-http (~> 0.18)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-nav (0.3.0)
pry (>= 0.9.10, < 0.13.0)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-nav (1.0.0)
pry (>= 0.9.10, < 0.15)
public_suffix (4.0.6)
racc (1.5.2)
racc (1.6.0)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
Expand Down Expand Up @@ -199,18 +199,18 @@ GEM
rake (13.0.6)
regexp_parser (2.1.1)
rexml (3.2.5)
rubocop (1.18.4)
rubocop (1.22.3)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.8.0, < 2.0)
rubocop-ast (>= 1.12.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.10.0)
rubocop-ast (1.13.0)
parser (>= 3.0.1.1)
rubocop-performance (1.11.4)
rubocop-performance (1.11.5)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.11.0)
Expand All @@ -221,29 +221,29 @@ GEM
sprockets (4.0.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.2)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets-rails (3.3.0)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.4.2)
standard (1.1.7)
rubocop (= 1.18.4)
rubocop-performance (= 1.11.4)
standard (1.4.0)
rubocop (= 1.22.3)
rubocop-performance (= 1.11.5)
standardrb (1.0.0)
standard
thor (1.1.0)
thread-local (1.1.0)
timers (4.3.3)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
unicode-display_width (2.1.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.4.2)
zeitwerk (2.5.1)

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
cable_ready!
Expand All @@ -257,4 +257,4 @@ DEPENDENCIES
standardrb

BUNDLED WITH
2.2.19
2.2.27
3 changes: 2 additions & 1 deletion app/helpers/cable_ready_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ def stream_from(*keys, html_options: {})
tag.stream_from(**build_options(*keys, html_options))
end

def updates_for(*keys, url: nil, debounce: nil, html_options: {}, &block)
def updates_for(*keys, url: nil, debounce: nil, only: nil, html_options: {}, &block)
options = build_options(*keys, html_options)
options[:url] = url if url
options[:debounce] = debounce if debounce
options[:only] = only if only
tag.updates_for(**options) { capture(&block) }
end

Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/cable_ready/updatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def cable_ready_collections
@cable_ready_collections ||= CollectionsRegistry.new
end

def cable_ready_update_collection(resource, name)
def cable_ready_update_collection(resource, name, model)
identifier = resource.to_global_id.to_s + ":" + name.to_s
ActionCable.server.broadcast(identifier, {})
ActionCable.server.broadcast(identifier, model.respond_to?(:previous_changes) ? {changed: model.previous_changes.keys} : {})
end

def enrich_association_with_updates(name, option)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def broadcast_for!(model, operation)
resource = find_resource_for_update(collection, model)
next if resource.nil?

collection[:klass].cable_ready_update_collection(resource, collection[:name]) if collection[:options][:if].call(resource)
collection[:klass].cable_ready_update_collection(resource, collection[:name], model) if collection[:options][:if].call(resource)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def broadcast_create(model)

def broadcast_update(model)
ActionCable.server.broadcast(model.class, {})
ActionCable.server.broadcast(model.to_global_id, {})
ActionCable.server.broadcast(model.to_global_id, model.respond_to?(:previous_changes) ? {changed: model.previous_changes.keys} : {})
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion javascript/elements/updates_for_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ export default class UpdatesForElement extends SubscribingElement {
}
}

async update () {
async update (data) {
const identifier = this.getAttribute('identifier')
const query = `updates-for[identifier="${identifier}"]`
const blocks = document.querySelectorAll(query)
if (blocks[0] !== this) return

const only = this.getAttribute('only')
if (
only &&
data.changed &&
!only.split(' ').some(attribute => data.changed.includes(attribute))
)
return

const html = {}
const template = document.createElement('template')

Expand Down
16 changes: 8 additions & 8 deletions test/lib/cable_ready/updatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CableReady::UpdatableTest < ActiveSupport::TestCase
test "updates the collection when an item is added" do
mock_server = mock("server")
mock_server.expects(:broadcast).with(User, {}).once
mock_server.expects(:broadcast).with("gid://dummy/User/1:posts", {}).once
mock_server.expects(:broadcast).with("gid://dummy/User/1:posts", {changed: ["id", "title", "user_id", "created_at", "updated_at"]}).once

ActionCable.stubs(:server).returns(mock_server)
user = User.create(name: "John Doe")
Expand All @@ -26,7 +26,7 @@ class CableReady::UpdatableTest < ActiveSupport::TestCase
post = user.posts.create(title: "Lorem")

mock_server = mock("server")
mock_server.expects(:broadcast).with("gid://dummy/User/1:posts", {}).once
mock_server.expects(:broadcast).with("gid://dummy/User/1:posts", {changed: ["id", "title", "user_id", "created_at", "updated_at"]}).once

ActionCable.stubs(:server).returns(mock_server)

Expand All @@ -38,7 +38,7 @@ class CableReady::UpdatableTest < ActiveSupport::TestCase
post = user.posts.create(title: "Lorem")

mock_server = mock("server")
mock_server.expects(:broadcast).with("gid://dummy/User/1:posts", {}).once
mock_server.expects(:broadcast).with("gid://dummy/User/1:posts", {changed: ["title", "updated_at"]}).once

ActionCable.stubs(:server).returns(mock_server)

Expand All @@ -50,7 +50,7 @@ class CableReady::UpdatableTest < ActiveSupport::TestCase

mock_server = mock("server")
mock_server.expects(:broadcast).with(User, {}).once
mock_server.expects(:broadcast).with(user.to_global_id, {}).once
mock_server.expects(:broadcast).with(user.to_global_id, {changed: ["name", "updated_at"]}).once

ActionCable.stubs(:server).returns(mock_server)

Expand All @@ -63,10 +63,10 @@ class CableReady::UpdatableTest < ActiveSupport::TestCase

mock_server = mock("server")
mock_server.expects(:broadcast).with(User, {}).once
mock_server.expects(:broadcast).with(user.to_global_id, {}).once
mock_server.expects(:broadcast).with("gid://dummy/Team/1:users", {}).once
mock_server.expects(:broadcast).with(user.to_global_id, {changed: ["name", "updated_at"]}).once
mock_server.expects(:broadcast).with("gid://dummy/Team/1:users", {changed: ["name", "updated_at"]}).once
mock_server.expects(:broadcast).with(Team, {}).once
mock_server.expects(:broadcast).with(team.to_global_id, {}).once
mock_server.expects(:broadcast).with(team.to_global_id, {changed: ["id", "created_at", "updated_at"]}).once

ActionCable.stubs(:server).returns(mock_server)

Expand Down Expand Up @@ -94,7 +94,7 @@ class CableReady::UpdatableTest < ActiveSupport::TestCase
section.updates_enabled = true

mock_server.expects(:broadcast).with(Section, {}).once
mock_server.expects(:broadcast).with(section.to_global_id, {}).once
mock_server.expects(:broadcast).with(section.to_global_id, {changed: ["title", "updated_at", "updates_enabled"]}).once
section.update(title: "First Section")
end

Expand Down

0 comments on commit d3e2efc

Please sign in to comment.