Skip to content

Comments

updates-for should respect url and only params on each element#186

Merged
leastbad merged 1 commit intomasterfrom
updates_for_updates_for
Feb 15, 2022
Merged

updates-for should respect url and only params on each element#186
leastbad merged 1 commit intomasterfrom
updates_for_updates_for

Conversation

@leastbad
Copy link
Contributor

This PR used to be #182.

Reworked updates-for element to allow per-block url and only attributes. URL fragment indexes are now tracked on a per-URL basis.

That means the following contrived example works as you'd expect:

index.html.erb

<%= updates_for current_user, :posts do %>
  <% Post.all.each do |p| %>
    <p><%= p.name %>, <%= p.email %></p>
  <% end %>
  <p><%= rand(1..1000) %></p>
<% end %>

<%= updates_for current_user, :posts, only: [:name] do %>
  <% Post.all.each do |p| %>
    <p><%= p.name %>, <%= p.email %></p>
  <% end %>
  <p><%= rand(1..1000) %></p>
<% end %>

<%= updates_for current_user, :posts, url: "/updates_for" do %>
  <% Post.all.each do |p| %>
    <p><%= p.name %>, <%= p.email %></p>
  <% end %>
  <p><%= rand(1..1000) %></p>
<% end %>

<%= updates_for current_user, :posts, only: [:name], url: "/updates_for" do %>
  <% Post.all.each do |p| %>
    <p><%= p.name %>, <%= p.email %></p>
  <% end %>
  <p><%= rand(1..1000) %></p>
<% end %>

updates_for.html.erb

<%= updates_for current_user, :posts do %>
  <p>HAIRY!</p>
  <p><%= rand(1..1000) %></p>
<% end %>

No, I don't know why Post has a name and email. Don't ask hard questions.

post = current_user.posts.create name: "Fred", email: "[email protected]"
post.update name: "Frederick" # updates the first three blocks, plus console warning
post.update email: "[email protected]" # updates only the first and third

Note that the 4th block will not be updated because updates_for.html.erb only has one block; a warning is generated in the console to suggest that there wasn't enough elements for the update to complete.

Unfortunately this will certainly clash with #181 as written, although I have done my best to maintain the general structure and naming to ease merge conflicts.

@leastbad leastbad added enhancement javascript Pull requests that update Javascript code labels Feb 15, 2022
@leastbad leastbad added this to the 5.0 milestone Feb 15, 2022
- reworked updates-for element to allow per-block url and only attributes

- created blocks getter and renamed ignoresInnerUpdates
@marcoroth marcoroth force-pushed the updates_for_updates_for branch from b2a8906 to 7fae989 Compare February 15, 2022 02:48
@marcoroth
Copy link
Member

This PR is now published as a dev-build. To use it to your project, run:

yarn add cable_ready@https://github.com/cableready/dev-builds/archive/cable_ready/07da548.tar.gz

@leastbad leastbad merged commit bea2397 into master Feb 15, 2022
@leastbad leastbad deleted the updates_for_updates_for branch February 15, 2022 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement javascript Pull requests that update Javascript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants