Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates_for :only option #163

Merged
merged 7 commits into from
Nov 12, 2021

Conversation

leastbad
Copy link
Contributor

@leastbad leastbad commented Nov 12, 2021

Closes #161

While I believe that a Model has no business knowing anything about views, @andrewerlanger's desire to eliminate unnecessary HTTP requests is highly reasonable. Therefore, instead of attempting to further complicate the enable_updates method with the potential for multiple calls that each target different views, this PR gives the updates_for helper the ability to optionally filter out messages which don't contain the right keys.

<%= updates_for User.first, only: "name" do %>
  <p><%= rand(1..1000) %></p>
<% end %>
User.first.update name: "Bob" # triggers refresh
User.first.update age: 43, name: "Steve" # triggers refresh
User.first.update age: 43 # does not trigger refresh

Any broadcasts which update individual model instances now transmit an array of attribute names that were updated:

ActionCable.server.broadcast(identifier, {changed: model.previous_changes.keys})

The only option currently accepts only a simple string that exactly matches the name of an attribute. There's no checking to ensure that it is a valid attribute, and there's no support for and/or-ing multiple attributes. I don't think that we need this feature to be Turing complete to be Just The Right Amount of functionality.

Note that this feature has no impact on Model messages (create/destroy) because it only works on after_update callbacks for individual model instances.

@leastbad leastbad added enhancement proposal ruby Pull requests that update Ruby code javascript Pull requests that update Javascript code labels Nov 12, 2021
@leastbad leastbad added this to the 5.0 milestone Nov 12, 2021
julianrubisch and others added 2 commits November 12, 2021 04:38
* Split 'only' attribute into array
* Update updates_for_element.js
* can't use ?. operator quite yet
@leastbad leastbad merged commit d3e2efc into stimulusreflex:master Nov 12, 2021
@leastbad leastbad deleted the updates_for_attributes branch November 12, 2021 20:32
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 proposal ruby Pull requests that update Ruby code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

updates_for: Support multiple enable_updates per model
3 participants