-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Draft: Listen to cable-ready:after-update events and re-request the partial #120
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,10 +68,18 @@ def initialize(extends:, placeholder:, options:) | |
@eager = options.delete(:eager) | ||
@broadcast_each = options.delete(:broadcast_each) | ||
@controller = options.delete(:controller) | ||
@updates_for_object = options.delete(:updates_for) | ||
@html_options = options.delete(:html_options) || {} | ||
@data_attributes = html_options.fetch(:data, {}).except(:sgid, :signed_params) | ||
@model = options.delete(:model) | ||
@wrapped_for_updates_for = options.delete(:wrapped_for_updates_for) | ||
if @wrapped_for_updates_for | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really happy with this variable name, any suggestions? |
||
@html_options[:keep] = 'keep' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to not add the IntersectionObserver (as it will re-request the partial over and over again when it is visible) |
||
@data_attributes['updates-for'] = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instructing the javascript to listen for the CableReady |
||
end | ||
@options = data_attributes.any? ? options.merge(data: data_attributes) : options | ||
|
||
warn "[Futurism] `updates_for` feature is not available for extends: :li or :tr elements." if [:tr, :li].include?(extends) | ||
end | ||
|
||
def dataset | ||
|
@@ -85,6 +93,53 @@ def dataset | |
end | ||
|
||
def render | ||
return render_updates_for if use_updates_for? | ||
|
||
render_tag | ||
end | ||
|
||
def transformed_options | ||
dump_options(options) | ||
end | ||
|
||
private | ||
|
||
############ | ||
# TODO: Include CableReadyHelper | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this and include it from CableReady once it's moved to a module. |
||
include CableReady::Compoundable | ||
include CableReady::StreamIdentifier | ||
include ActionView::Context | ||
|
||
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 | ||
|
||
private | ||
|
||
def build_options(*keys, html_options) | ||
keys.select!(&:itself) | ||
{identifier: signed_stream_identifier(compound(keys))}.merge(html_options) | ||
end | ||
############ | ||
|
||
def render_updates_for | ||
arguments = Array.wrap(@updates_for_object) | ||
kwargs = arguments.last.is_a?(Hash) ? arguments.pop : {} | ||
kwargs[:html_options] ||= {} | ||
kwargs[:html_options][:data] ||= {} | ||
kwargs[:html_options][:data]['ignore-morph'] = true | ||
kwargs[:html_options][:data]['after-update-event-selector'] = 'futurism-element' | ||
|
||
updates_for(*arguments, **kwargs) do | ||
render_tag | ||
end | ||
end | ||
|
||
def render_tag | ||
case extends | ||
when :li | ||
content_tag :li, placeholder, html_options.deep_merge({data: dataset, is: "futurism-li"}) | ||
|
@@ -95,14 +150,24 @@ def render | |
end | ||
end | ||
|
||
def transformed_options | ||
dump_options(options) | ||
def use_updates_for? | ||
@updates_for_object.present? && ![:tr, :li].include?(extends) | ||
end | ||
|
||
private | ||
|
||
def signed_params | ||
message_verifier.generate(transformed_options) | ||
message_verifier.generate(transformed_options.merge(updates_for_params)) | ||
end | ||
|
||
def updates_for_params | ||
return {} unless use_updates_for? || @wrapped_for_updates_for | ||
|
||
{ | ||
wrap_for_updates_for: { | ||
extends: extends, | ||
html_options: html_options, | ||
data_attributes: data_attributes, | ||
} | ||
} | ||
end | ||
|
||
def signed_controller | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,27 +15,48 @@ def initialize(resource_definitions:, connection:, params:) | |
|
||
def resolve | ||
resolved_models.zip(@resources_with_sgids).each do |model, resource_definition| | ||
html = renderer_for(resource_definition: resource_definition).render(model) | ||
options = options_from_resource(resource_definition) | ||
html = render_html(model, resource_definition: resource_definition, render_exceptions: false) | ||
html = wrapped_html(html, options) | ||
|
||
yield(resource_definition.selector, html, resource_definition.broadcast_each) | ||
end | ||
|
||
@resources_without_sgids.each do |resource_definition| | ||
options = options_from_resource(resource_definition) | ||
renderer = renderer_for(resource_definition: resource_definition) | ||
html = | ||
begin | ||
renderer.render(options) | ||
rescue => exception | ||
error_renderer.render(exception) | ||
end | ||
html = render_html(options, resource_definition: resource_definition) | ||
html = wrapped_html(html, options) | ||
|
||
yield(resource_definition.selector, html, resource_definition.broadcast_each) | ||
end | ||
end | ||
|
||
private | ||
|
||
def wrapped_html(html, options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name of this method can be improved |
||
wrap_for_updates_for = options.delete(:wrap_for_updates_for) | ||
return html unless wrap_for_updates_for | ||
|
||
# Only wrap the element again if we were told to for the updates_for feature | ||
options = options.dup | ||
options.merge!(wrap_for_updates_for) | ||
options[:wrapped_for_updates_for] = true | ||
|
||
extends = options.delete(:extends) | ||
|
||
Futurism::Helpers::WrappingFuturismElement.new(extends: extends, placeholder: html, options: options).render | ||
end | ||
|
||
def render_html(model, render_exceptions: true, **kwargs) | ||
return renderer_for(**kwargs).render(model) unless render_exceptions | ||
|
||
begin | ||
renderer_for(**kwargs).render(model) | ||
rescue => exception | ||
error_renderer.render(exception) | ||
end | ||
end | ||
|
||
def error_renderer | ||
ErrorRenderer.new | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to check if this is still needed or a relic of my try & fail & try again approach