From 83f41b434c2830e5414cada8d6a6b0cfe6eb4319 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Tue, 6 May 2025 22:35:37 -0400 Subject: [PATCH 01/33] Remove redirection --- lib/superglue.rb | 2 -- lib/superglue/redirection.rb | 29 ----------------------------- 2 files changed, 31 deletions(-) delete mode 100644 lib/superglue/redirection.rb diff --git a/lib/superglue.rb b/lib/superglue.rb index b0a095d..abe56b5 100644 --- a/lib/superglue.rb +++ b/lib/superglue.rb @@ -1,5 +1,4 @@ require "superglue/helpers" -require "superglue/redirection" require "superglue/rendering" require "superglue/resolver" require "props_template" @@ -7,7 +6,6 @@ module Superglue module Controller - include Redirection include Helpers def self.included(base) diff --git a/lib/superglue/redirection.rb b/lib/superglue/redirection.rb deleted file mode 100644 index 36c049d..0000000 --- a/lib/superglue/redirection.rb +++ /dev/null @@ -1,29 +0,0 @@ -module Superglue - module Redirection - def _compute_redirect_to_location(request, options) - computed_location = URI.parse(super) - next_param = Rack::Utils - .parse_nested_query(computed_location.query) - - if request.params[:__] == "0" - computed_location.query = next_param.merge({__: "0"}).to_query - end - - computed_location.to_s - end - - def redirect_back_with_props_at(opts) - if request.referrer && params[:props_at] - referrer_url = URI.parse(request.referrer) - referrer_url.query = Rack::Utils - .parse_nested_query(referrer_url.query) - .merge({props_at: params[:props_at]}) - .to_query - - redirect_to referrer_url.to_s, opts - else - redirect_back(opts) - end - end - end -end From 96a67410e226dfd8c7ff5da78d7d97ac6bde6c3b Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Tue, 6 May 2025 22:39:58 -0400 Subject: [PATCH 02/33] Add dummy test app --- test/dummy/Rakefile | 6 + test/dummy/app/assets/images/.keep | 0 .../app/assets/stylesheets/application.css | 15 +++ .../app/controllers/application_controller.rb | 4 + test/dummy/app/controllers/concerns/.keep | 0 test/dummy/app/helpers/application_helper.rb | 2 + test/dummy/app/jobs/application_job.rb | 7 ++ test/dummy/app/mailers/application_mailer.rb | 4 + test/dummy/app/models/application_record.rb | 3 + test/dummy/app/models/concerns/.keep | 0 .../app/views/layouts/application.html.erb | 27 +++++ test/dummy/app/views/layouts/mailer.html.erb | 13 ++ test/dummy/app/views/layouts/mailer.text.erb | 1 + test/dummy/app/views/pwa/manifest.json.erb | 22 ++++ test/dummy/app/views/pwa/service-worker.js | 26 ++++ test/dummy/bin/dev | 2 + test/dummy/bin/rails | 4 + test/dummy/bin/rake | 4 + test/dummy/bin/setup | 34 ++++++ test/dummy/config.ru | 6 + test/dummy/config/application.rb | 29 +++++ test/dummy/config/boot.rb | 5 + test/dummy/config/cable.yml | 10 ++ test/dummy/config/database.yml | 32 +++++ test/dummy/config/environment.rb | 5 + test/dummy/config/environments/development.rb | 69 +++++++++++ test/dummy/config/environments/production.rb | 89 ++++++++++++++ test/dummy/config/environments/test.rb | 53 ++++++++ test/dummy/config/initializers/assets.rb | 7 ++ .../initializers/content_security_policy.rb | 25 ++++ .../initializers/filter_parameter_logging.rb | 8 ++ test/dummy/config/initializers/inflections.rb | 16 +++ test/dummy/config/locales/en.yml | 31 +++++ test/dummy/config/puma.rb | 38 ++++++ test/dummy/config/routes.rb | 14 +++ test/dummy/config/storage.yml | 34 ++++++ test/dummy/public/400.html | 114 ++++++++++++++++++ test/dummy/public/404.html | 114 ++++++++++++++++++ .../dummy/public/406-unsupported-browser.html | 114 ++++++++++++++++++ test/dummy/public/422.html | 114 ++++++++++++++++++ test/dummy/public/500.html | 114 ++++++++++++++++++ test/dummy/public/icon.png | Bin 0 -> 4166 bytes test/dummy/public/icon.svg | 3 + test/dummy/storage/.keep | 0 44 files changed, 1218 insertions(+) create mode 100644 test/dummy/Rakefile create mode 100644 test/dummy/app/assets/images/.keep create mode 100644 test/dummy/app/assets/stylesheets/application.css create mode 100644 test/dummy/app/controllers/application_controller.rb create mode 100644 test/dummy/app/controllers/concerns/.keep create mode 100644 test/dummy/app/helpers/application_helper.rb create mode 100644 test/dummy/app/jobs/application_job.rb create mode 100644 test/dummy/app/mailers/application_mailer.rb create mode 100644 test/dummy/app/models/application_record.rb create mode 100644 test/dummy/app/models/concerns/.keep create mode 100644 test/dummy/app/views/layouts/application.html.erb create mode 100644 test/dummy/app/views/layouts/mailer.html.erb create mode 100644 test/dummy/app/views/layouts/mailer.text.erb create mode 100644 test/dummy/app/views/pwa/manifest.json.erb create mode 100644 test/dummy/app/views/pwa/service-worker.js create mode 100755 test/dummy/bin/dev create mode 100755 test/dummy/bin/rails create mode 100755 test/dummy/bin/rake create mode 100755 test/dummy/bin/setup create mode 100644 test/dummy/config.ru create mode 100644 test/dummy/config/application.rb create mode 100644 test/dummy/config/boot.rb create mode 100644 test/dummy/config/cable.yml create mode 100644 test/dummy/config/database.yml create mode 100644 test/dummy/config/environment.rb create mode 100644 test/dummy/config/environments/development.rb create mode 100644 test/dummy/config/environments/production.rb create mode 100644 test/dummy/config/environments/test.rb create mode 100644 test/dummy/config/initializers/assets.rb create mode 100644 test/dummy/config/initializers/content_security_policy.rb create mode 100644 test/dummy/config/initializers/filter_parameter_logging.rb create mode 100644 test/dummy/config/initializers/inflections.rb create mode 100644 test/dummy/config/locales/en.yml create mode 100644 test/dummy/config/puma.rb create mode 100644 test/dummy/config/routes.rb create mode 100644 test/dummy/config/storage.yml create mode 100644 test/dummy/public/400.html create mode 100644 test/dummy/public/404.html create mode 100644 test/dummy/public/406-unsupported-browser.html create mode 100644 test/dummy/public/422.html create mode 100644 test/dummy/public/500.html create mode 100644 test/dummy/public/icon.png create mode 100644 test/dummy/public/icon.svg create mode 100644 test/dummy/storage/.keep diff --git a/test/dummy/Rakefile b/test/dummy/Rakefile new file mode 100644 index 0000000..9a5ea73 --- /dev/null +++ b/test/dummy/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require_relative "config/application" + +Rails.application.load_tasks diff --git a/test/dummy/app/assets/images/.keep b/test/dummy/app/assets/images/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/dummy/app/assets/stylesheets/application.css b/test/dummy/app/assets/stylesheets/application.css new file mode 100644 index 0000000..0ebd7fe --- /dev/null +++ b/test/dummy/app/assets/stylesheets/application.css @@ -0,0 +1,15 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS + * files in this directory. Styles in this file should be added after the last require_* statement. + * It is generally better to create a new file per style scope. + * + *= require_tree . + *= require_self + */ diff --git a/test/dummy/app/controllers/application_controller.rb b/test/dummy/app/controllers/application_controller.rb new file mode 100644 index 0000000..0d95db2 --- /dev/null +++ b/test/dummy/app/controllers/application_controller.rb @@ -0,0 +1,4 @@ +class ApplicationController < ActionController::Base + # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. + allow_browser versions: :modern +end diff --git a/test/dummy/app/controllers/concerns/.keep b/test/dummy/app/controllers/concerns/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/dummy/app/helpers/application_helper.rb b/test/dummy/app/helpers/application_helper.rb new file mode 100644 index 0000000..de6be79 --- /dev/null +++ b/test/dummy/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/test/dummy/app/jobs/application_job.rb b/test/dummy/app/jobs/application_job.rb new file mode 100644 index 0000000..d394c3d --- /dev/null +++ b/test/dummy/app/jobs/application_job.rb @@ -0,0 +1,7 @@ +class ApplicationJob < ActiveJob::Base + # Automatically retry jobs that encountered a deadlock + # retry_on ActiveRecord::Deadlocked + + # Most jobs are safe to ignore if the underlying records are no longer available + # discard_on ActiveJob::DeserializationError +end diff --git a/test/dummy/app/mailers/application_mailer.rb b/test/dummy/app/mailers/application_mailer.rb new file mode 100644 index 0000000..3c34c81 --- /dev/null +++ b/test/dummy/app/mailers/application_mailer.rb @@ -0,0 +1,4 @@ +class ApplicationMailer < ActionMailer::Base + default from: "from@example.com" + layout "mailer" +end diff --git a/test/dummy/app/models/application_record.rb b/test/dummy/app/models/application_record.rb new file mode 100644 index 0000000..b63caeb --- /dev/null +++ b/test/dummy/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + primary_abstract_class +end diff --git a/test/dummy/app/models/concerns/.keep b/test/dummy/app/models/concerns/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/dummy/app/views/layouts/application.html.erb b/test/dummy/app/views/layouts/application.html.erb new file mode 100644 index 0000000..f25ae92 --- /dev/null +++ b/test/dummy/app/views/layouts/application.html.erb @@ -0,0 +1,27 @@ + + + + <%= content_for(:title) || "Dummy" %> + + + + <%= csrf_meta_tags %> + <%= csp_meta_tag %> + + <%= yield :head %> + + <%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %> + <%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %> + + + + + + <%# Includes all stylesheet files in app/assets/stylesheets %> + <%= stylesheet_link_tag :app %> + + + + <%= yield %> + + diff --git a/test/dummy/app/views/layouts/mailer.html.erb b/test/dummy/app/views/layouts/mailer.html.erb new file mode 100644 index 0000000..3aac900 --- /dev/null +++ b/test/dummy/app/views/layouts/mailer.html.erb @@ -0,0 +1,13 @@ + + + + + + + + + <%= yield %> + + diff --git a/test/dummy/app/views/layouts/mailer.text.erb b/test/dummy/app/views/layouts/mailer.text.erb new file mode 100644 index 0000000..37f0bdd --- /dev/null +++ b/test/dummy/app/views/layouts/mailer.text.erb @@ -0,0 +1 @@ +<%= yield %> diff --git a/test/dummy/app/views/pwa/manifest.json.erb b/test/dummy/app/views/pwa/manifest.json.erb new file mode 100644 index 0000000..a3c046e --- /dev/null +++ b/test/dummy/app/views/pwa/manifest.json.erb @@ -0,0 +1,22 @@ +{ + "name": "Dummy", + "icons": [ + { + "src": "/icon.png", + "type": "image/png", + "sizes": "512x512" + }, + { + "src": "/icon.png", + "type": "image/png", + "sizes": "512x512", + "purpose": "maskable" + } + ], + "start_url": "/", + "display": "standalone", + "scope": "/", + "description": "Dummy.", + "theme_color": "red", + "background_color": "red" +} diff --git a/test/dummy/app/views/pwa/service-worker.js b/test/dummy/app/views/pwa/service-worker.js new file mode 100644 index 0000000..b3a13fb --- /dev/null +++ b/test/dummy/app/views/pwa/service-worker.js @@ -0,0 +1,26 @@ +// Add a service worker for processing Web Push notifications: +// +// self.addEventListener("push", async (event) => { +// const { title, options } = await event.data.json() +// event.waitUntil(self.registration.showNotification(title, options)) +// }) +// +// self.addEventListener("notificationclick", function(event) { +// event.notification.close() +// event.waitUntil( +// clients.matchAll({ type: "window" }).then((clientList) => { +// for (let i = 0; i < clientList.length; i++) { +// let client = clientList[i] +// let clientPath = (new URL(client.url)).pathname +// +// if (clientPath == event.notification.data.path && "focus" in client) { +// return client.focus() +// } +// } +// +// if (clients.openWindow) { +// return clients.openWindow(event.notification.data.path) +// } +// }) +// ) +// }) diff --git a/test/dummy/bin/dev b/test/dummy/bin/dev new file mode 100755 index 0000000..5f91c20 --- /dev/null +++ b/test/dummy/bin/dev @@ -0,0 +1,2 @@ +#!/usr/bin/env ruby +exec "./bin/rails", "server", *ARGV diff --git a/test/dummy/bin/rails b/test/dummy/bin/rails new file mode 100755 index 0000000..efc0377 --- /dev/null +++ b/test/dummy/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path("../config/application", __dir__) +require_relative "../config/boot" +require "rails/commands" diff --git a/test/dummy/bin/rake b/test/dummy/bin/rake new file mode 100755 index 0000000..4fbf10b --- /dev/null +++ b/test/dummy/bin/rake @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require_relative "../config/boot" +require "rake" +Rake.application.run diff --git a/test/dummy/bin/setup b/test/dummy/bin/setup new file mode 100755 index 0000000..be3db3c --- /dev/null +++ b/test/dummy/bin/setup @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby +require "fileutils" + +APP_ROOT = File.expand_path("..", __dir__) + +def system!(*args) + system(*args, exception: true) +end + +FileUtils.chdir APP_ROOT do + # This script is a way to set up or update your development environment automatically. + # This script is idempotent, so that you can run it at any time and get an expectable outcome. + # Add necessary setup steps to this file. + + puts "== Installing dependencies ==" + system("bundle check") || system!("bundle install") + + # puts "\n== Copying sample files ==" + # unless File.exist?("config/database.yml") + # FileUtils.cp "config/database.yml.sample", "config/database.yml" + # end + + puts "\n== Preparing database ==" + system! "bin/rails db:prepare" + + puts "\n== Removing old logs and tempfiles ==" + system! "bin/rails log:clear tmp:clear" + + unless ARGV.include?("--skip-server") + puts "\n== Starting development server ==" + STDOUT.flush # flush the output before exec(2) so that it displays + exec "bin/dev" + end +end diff --git a/test/dummy/config.ru b/test/dummy/config.ru new file mode 100644 index 0000000..4a3c09a --- /dev/null +++ b/test/dummy/config.ru @@ -0,0 +1,6 @@ +# This file is used by Rack-based servers to start the application. + +require_relative "config/environment" + +run Rails.application +Rails.application.load_server diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb new file mode 100644 index 0000000..6e61f6b --- /dev/null +++ b/test/dummy/config/application.rb @@ -0,0 +1,29 @@ +require_relative "boot" + +require "rails/all" + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module Dummy + class Application < Rails::Application + config.load_defaults Rails::VERSION::STRING.to_f + + # For compatibility with applications that use this config + config.action_controller.include_all_helpers = false + + # Please, add to the `ignore` list any other `lib` subdirectories that do + # not contain `.rb` files, or that should not be reloaded or eager loaded. + # Common ones are `templates`, `generators`, or `middleware`, for example. + config.autoload_lib(ignore: %w[assets tasks]) + + # Configuration for the application, engines, and railties goes here. + # + # These settings can be overridden in specific environments using the files + # in config/environments, which are processed later. + # + # config.time_zone = "Central Time (US & Canada)" + # config.eager_load_paths << Rails.root.join("extras") + end +end diff --git a/test/dummy/config/boot.rb b/test/dummy/config/boot.rb new file mode 100644 index 0000000..116591a --- /dev/null +++ b/test/dummy/config/boot.rb @@ -0,0 +1,5 @@ +# Set up gems listed in the Gemfile. +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../Gemfile", __dir__) + +require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"]) +$LOAD_PATH.unshift File.expand_path("../../../lib", __dir__) diff --git a/test/dummy/config/cable.yml b/test/dummy/config/cable.yml new file mode 100644 index 0000000..98367f8 --- /dev/null +++ b/test/dummy/config/cable.yml @@ -0,0 +1,10 @@ +development: + adapter: async + +test: + adapter: test + +production: + adapter: redis + url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> + channel_prefix: dummy_production diff --git a/test/dummy/config/database.yml b/test/dummy/config/database.yml new file mode 100644 index 0000000..01bebb5 --- /dev/null +++ b/test/dummy/config/database.yml @@ -0,0 +1,32 @@ +# SQLite. Versions 3.8.0 and up are supported. +# gem install sqlite3 +# +# Ensure the SQLite 3 gem is defined in your Gemfile +# gem "sqlite3" +# +default: &default + adapter: sqlite3 + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + timeout: 5000 + +development: + <<: *default + database: storage/development.sqlite3 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: storage/test.sqlite3 + + +# SQLite3 write its data on the local filesystem, as such it requires +# persistent disks. If you are deploying to a managed service, you should +# make sure it provides disk persistence, as many don't. +# +# Similarly, if you deploy your application as a Docker container, you must +# ensure the database is located in a persisted volume. +production: + <<: *default + # database: path/to/persistent/storage/production.sqlite3 diff --git a/test/dummy/config/environment.rb b/test/dummy/config/environment.rb new file mode 100644 index 0000000..cac5315 --- /dev/null +++ b/test/dummy/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative "application" + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/test/dummy/config/environments/development.rb b/test/dummy/config/environments/development.rb new file mode 100644 index 0000000..263e0c4 --- /dev/null +++ b/test/dummy/config/environments/development.rb @@ -0,0 +1,69 @@ +require "active_support/core_ext/integer/time" + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Make code changes take effect immediately without server restart. + config.enable_reloading = true + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable server timing. + config.server_timing = true + + # Enable/disable Action Controller caching. By default Action Controller caching is disabled. + # Run rails dev:cache to toggle Action Controller caching. + if Rails.root.join("tmp/caching-dev.txt").exist? + config.action_controller.perform_caching = true + config.action_controller.enable_fragment_cache_logging = true + config.public_file_server.headers = { "cache-control" => "public, max-age=#{2.days.to_i}" } + else + config.action_controller.perform_caching = false + end + + # Change to :null_store to avoid any caching. + config.cache_store = :memory_store + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.service = :local + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + # Make template changes take effect immediately. + config.action_mailer.perform_caching = false + + # Set localhost to be used by links generated in mailer templates. + config.action_mailer.default_url_options = { host: "localhost", port: 3000 } + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + + # Append comments with runtime information tags to SQL queries in logs. + config.active_record.query_log_tags_enabled = true + + # Highlight code that enqueued background job in logs. + config.active_job.verbose_enqueue_logs = true + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + config.action_view.annotate_rendered_view_with_filenames = true + + # Uncomment if you wish to allow Action Cable access from any origin. + # config.action_cable.disable_request_forgery_protection = true + + # Raise error when a before_action's only/except options reference missing actions. + config.action_controller.raise_on_missing_callback_actions = true +end diff --git a/test/dummy/config/environments/production.rb b/test/dummy/config/environments/production.rb new file mode 100644 index 0000000..1749607 --- /dev/null +++ b/test/dummy/config/environments/production.rb @@ -0,0 +1,89 @@ +require "active_support/core_ext/integer/time" + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Code is not reloaded between requests. + config.enable_reloading = false + + # Eager load code on boot for better performance and memory savings (ignored by Rake tasks). + config.eager_load = true + + # Full error reports are disabled. + config.consider_all_requests_local = false + + # Turn on fragment caching in view templates. + config.action_controller.perform_caching = true + + # Cache assets for far-future expiry since they are all digest stamped. + config.public_file_server.headers = { "cache-control" => "public, max-age=#{1.year.to_i}" } + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.asset_host = "http://assets.example.com" + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.service = :local + + # Assume all access to the app is happening through a SSL-terminating reverse proxy. + config.assume_ssl = true + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + config.force_ssl = true + + # Skip http-to-https redirect for the default health check endpoint. + # config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } } + + # Log to STDOUT with the current request id as a default log tag. + config.log_tags = [ :request_id ] + config.logger = ActiveSupport::TaggedLogging.logger(STDOUT) + + # Change to "debug" to log everything (including potentially personally-identifiable information!) + config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info") + + # Prevent health checks from clogging up the logs. + config.silence_healthcheck_path = "/up" + + # Don't log any deprecations. + config.active_support.report_deprecations = false + + # Replace the default in-process memory cache store with a durable alternative. + # config.cache_store = :mem_cache_store + + # Replace the default in-process and non-durable queuing backend for Active Job. + # config.active_job.queue_adapter = :resque + + # Ignore bad email addresses and do not raise email delivery errors. + # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # config.action_mailer.raise_delivery_errors = false + + # Set host to be used by links generated in mailer templates. + config.action_mailer.default_url_options = { host: "example.com" } + + # Specify outgoing SMTP server. Remember to add smtp/* credentials via rails credentials:edit. + # config.action_mailer.smtp_settings = { + # user_name: Rails.application.credentials.dig(:smtp, :user_name), + # password: Rails.application.credentials.dig(:smtp, :password), + # address: "smtp.example.com", + # port: 587, + # authentication: :plain + # } + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false + + # Only use :id for inspections in production. + config.active_record.attributes_for_inspect = [ :id ] + + # Enable DNS rebinding protection and other `Host` header attacks. + # config.hosts = [ + # "example.com", # Allow requests from example.com + # /.*\.example\.com/ # Allow requests from subdomains like `www.example.com` + # ] + # + # Skip DNS rebinding protection for the default health check endpoint. + # config.host_authorization = { exclude: ->(request) { request.path == "/up" } } +end diff --git a/test/dummy/config/environments/test.rb b/test/dummy/config/environments/test.rb new file mode 100644 index 0000000..c2095b1 --- /dev/null +++ b/test/dummy/config/environments/test.rb @@ -0,0 +1,53 @@ +# The test environment is used exclusively to run your application's +# test suite. You never need to work with it otherwise. Remember that +# your test database is "scratch space" for the test suite and is wiped +# and recreated between test runs. Don't rely on the data there! + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # While tests run files are not watched, reloading is not necessary. + config.enable_reloading = false + + # Eager loading loads your entire application. When running a single test locally, + # this is usually not necessary, and can slow down your test suite. However, it's + # recommended that you enable it in continuous integration systems to ensure eager + # loading is working properly before deploying your code. + config.eager_load = ENV["CI"].present? + + # Configure public file server for tests with cache-control for performance. + config.public_file_server.headers = { "cache-control" => "public, max-age=3600" } + + # Show full error reports. + config.consider_all_requests_local = true + config.cache_store = :null_store + + # Render exception templates for rescuable exceptions and raise for other exceptions. + config.action_dispatch.show_exceptions = :rescuable + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Store uploaded files on the local file system in a temporary directory. + config.active_storage.service = :test + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Set host to be used by links generated in mailer templates. + config.action_mailer.default_url_options = { host: "example.com" } + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = true + + # Raise error when a before_action's only/except options reference missing actions. + config.action_controller.raise_on_missing_callback_actions = true +end diff --git a/test/dummy/config/initializers/assets.rb b/test/dummy/config/initializers/assets.rb new file mode 100644 index 0000000..4873244 --- /dev/null +++ b/test/dummy/config/initializers/assets.rb @@ -0,0 +1,7 @@ +# Be sure to restart your server when you modify this file. + +# Version of your assets, change this if you want to expire all your assets. +Rails.application.config.assets.version = "1.0" + +# Add additional assets to the asset load path. +# Rails.application.config.assets.paths << Emoji.images_path diff --git a/test/dummy/config/initializers/content_security_policy.rb b/test/dummy/config/initializers/content_security_policy.rb new file mode 100644 index 0000000..b3076b3 --- /dev/null +++ b/test/dummy/config/initializers/content_security_policy.rb @@ -0,0 +1,25 @@ +# Be sure to restart your server when you modify this file. + +# Define an application-wide content security policy. +# See the Securing Rails Applications Guide for more information: +# https://guides.rubyonrails.org/security.html#content-security-policy-header + +# Rails.application.configure do +# config.content_security_policy do |policy| +# policy.default_src :self, :https +# policy.font_src :self, :https, :data +# policy.img_src :self, :https, :data +# policy.object_src :none +# policy.script_src :self, :https +# policy.style_src :self, :https +# # Specify URI for violation reports +# # policy.report_uri "/csp-violation-report-endpoint" +# end +# +# # Generate session nonces for permitted importmap, inline scripts, and inline styles. +# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } +# config.content_security_policy_nonce_directives = %w(script-src style-src) +# +# # Report violations without enforcing the policy. +# # config.content_security_policy_report_only = true +# end diff --git a/test/dummy/config/initializers/filter_parameter_logging.rb b/test/dummy/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000..c0b717f --- /dev/null +++ b/test/dummy/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,8 @@ +# Be sure to restart your server when you modify this file. + +# Configure parameters to be partially matched (e.g. passw matches password) and filtered from the log file. +# Use this to limit dissemination of sensitive information. +# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors. +Rails.application.config.filter_parameters += [ + :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn, :cvv, :cvc +] diff --git a/test/dummy/config/initializers/inflections.rb b/test/dummy/config/initializers/inflections.rb new file mode 100644 index 0000000..3860f65 --- /dev/null +++ b/test/dummy/config/initializers/inflections.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format. Inflections +# are locale specific, and you may define rules for as many different +# locales as you wish. All of these examples are active by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.plural /^(ox)$/i, "\\1en" +# inflect.singular /^(ox)en/i, "\\1" +# inflect.irregular "person", "people" +# inflect.uncountable %w( fish sheep ) +# end + +# These inflection rules are supported but not enabled by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.acronym "RESTful" +# end diff --git a/test/dummy/config/locales/en.yml b/test/dummy/config/locales/en.yml new file mode 100644 index 0000000..6c349ae --- /dev/null +++ b/test/dummy/config/locales/en.yml @@ -0,0 +1,31 @@ +# Files in the config/locales directory are used for internationalization and +# are automatically loaded by Rails. If you want to use locales other than +# English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t "hello" +# +# In views, this is aliased to just `t`: +# +# <%= t("hello") %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# To learn more about the API, please read the Rails Internationalization guide +# at https://guides.rubyonrails.org/i18n.html. +# +# Be aware that YAML interprets the following case-insensitive strings as +# booleans: `true`, `false`, `on`, `off`, `yes`, `no`. Therefore, these strings +# must be quoted to be interpreted as strings. For example: +# +# en: +# "yes": yup +# enabled: "ON" + +en: + hello: "Hello world" diff --git a/test/dummy/config/puma.rb b/test/dummy/config/puma.rb new file mode 100644 index 0000000..787e4ce --- /dev/null +++ b/test/dummy/config/puma.rb @@ -0,0 +1,38 @@ +# This configuration file will be evaluated by Puma. The top-level methods that +# are invoked here are part of Puma's configuration DSL. For more information +# about methods provided by the DSL, see https://puma.io/puma/Puma/DSL.html. +# +# Puma starts a configurable number of processes (workers) and each process +# serves each request in a thread from an internal thread pool. +# +# You can control the number of workers using ENV["WEB_CONCURRENCY"]. You +# should only set this value when you want to run 2 or more workers. The +# default is already 1. +# +# The ideal number of threads per worker depends both on how much time the +# application spends waiting for IO operations and on how much you wish to +# prioritize throughput over latency. +# +# As a rule of thumb, increasing the number of threads will increase how much +# traffic a given process can handle (throughput), but due to CRuby's +# Global VM Lock (GVL) it has diminishing returns and will degrade the +# response time (latency) of the application. +# +# The default is set to 3 threads as it's deemed a decent compromise between +# throughput and latency for the average Rails application. +# +# Any libraries that use a connection pool or another resource pool should +# be configured to provide at least as many connections as the number of +# threads. This includes Active Record's `pool` parameter in `database.yml`. +threads_count = ENV.fetch("RAILS_MAX_THREADS", 3) +threads threads_count, threads_count + +# Specifies the `port` that Puma will listen on to receive requests; default is 3000. +port ENV.fetch("PORT", 3000) + +# Allow puma to be restarted by `bin/rails restart` command. +plugin :tmp_restart + +# Specify the PID file. Defaults to tmp/pids/server.pid in development. +# In other environments, only set the PID file if requested. +pidfile ENV["PIDFILE"] if ENV["PIDFILE"] diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb new file mode 100644 index 0000000..48254e8 --- /dev/null +++ b/test/dummy/config/routes.rb @@ -0,0 +1,14 @@ +Rails.application.routes.draw do + # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html + + # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. + # Can be used by load balancers and uptime monitors to verify that the app is live. + get "up" => "rails/health#show", as: :rails_health_check + + # Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb) + # get "manifest" => "rails/pwa#manifest", as: :pwa_manifest + # get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker + + # Defines the root path route ("/") + # root "posts#index" +end diff --git a/test/dummy/config/storage.yml b/test/dummy/config/storage.yml new file mode 100644 index 0000000..4942ab6 --- /dev/null +++ b/test/dummy/config/storage.yml @@ -0,0 +1,34 @@ +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + +local: + service: Disk + root: <%= Rails.root.join("storage") %> + +# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) +# amazon: +# service: S3 +# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> +# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> +# region: us-east-1 +# bucket: your_own_bucket-<%= Rails.env %> + +# Remember not to checkin your GCS keyfile to a repository +# google: +# service: GCS +# project: your_project +# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> +# bucket: your_own_bucket-<%= Rails.env %> + +# Use bin/rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) +# microsoft: +# service: AzureStorage +# storage_account_name: your_account_name +# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> +# container: your_container_name-<%= Rails.env %> + +# mirror: +# service: Mirror +# primary: local +# mirrors: [ amazon, google, microsoft ] diff --git a/test/dummy/public/400.html b/test/dummy/public/400.html new file mode 100644 index 0000000..282dbc8 --- /dev/null +++ b/test/dummy/public/400.html @@ -0,0 +1,114 @@ + + + + + + + The server cannot process the request due to a client error (400 Bad Request) + + + + + + + + + + + + + +
+
+ +
+
+

The server cannot process the request due to a client error. Please check the request and try again. If you’re the application owner check the logs for more information.

+
+
+ + + + diff --git a/test/dummy/public/404.html b/test/dummy/public/404.html new file mode 100644 index 0000000..c0670bc --- /dev/null +++ b/test/dummy/public/404.html @@ -0,0 +1,114 @@ + + + + + + + The page you were looking for doesn’t exist (404 Not found) + + + + + + + + + + + + + +
+
+ +
+
+

The page you were looking for doesn’t exist. You may have mistyped the address or the page may have moved. If you’re the application owner check the logs for more information.

+
+
+ + + + diff --git a/test/dummy/public/406-unsupported-browser.html b/test/dummy/public/406-unsupported-browser.html new file mode 100644 index 0000000..9532a9c --- /dev/null +++ b/test/dummy/public/406-unsupported-browser.html @@ -0,0 +1,114 @@ + + + + + + + Your browser is not supported (406 Not Acceptable) + + + + + + + + + + + + + +
+
+ +
+
+

Your browser is not supported.
Please upgrade your browser to continue.

+
+
+ + + + diff --git a/test/dummy/public/422.html b/test/dummy/public/422.html new file mode 100644 index 0000000..8bcf060 --- /dev/null +++ b/test/dummy/public/422.html @@ -0,0 +1,114 @@ + + + + + + + The change you wanted was rejected (422 Unprocessable Entity) + + + + + + + + + + + + + +
+
+ +
+
+

The change you wanted was rejected. Maybe you tried to change something you didn’t have access to. If you’re the application owner check the logs for more information.

+
+
+ + + + diff --git a/test/dummy/public/500.html b/test/dummy/public/500.html new file mode 100644 index 0000000..d77718c --- /dev/null +++ b/test/dummy/public/500.html @@ -0,0 +1,114 @@ + + + + + + + We’re sorry, but something went wrong (500 Internal Server Error) + + + + + + + + + + + + + +
+
+ +
+
+

We’re sorry, but something went wrong.
If you’re the application owner check the logs for more information.

+
+
+ + + + diff --git a/test/dummy/public/icon.png b/test/dummy/public/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..c4c9dbfbbd2f7c1421ffd5727188146213abbcef GIT binary patch literal 4166 zcmd6qU;WFw?|v@m)Sk^&NvB8tcujdV-r1b=i(NJxn&7{KTb zX$3(M+3TP2o^#KAo{#tIjl&t~(8D-k004kqPglzn0HFG(Q~(I*AKsD#M*g7!XK0T7 zN6P7j>HcT8rZgKl$v!xr806dyN19Bd4C0x_R*I-a?#zsTvb_89cyhuC&T**i|Rc zq5b8M;+{8KvoJ~uj9`u~d_f6`V&3+&ZX9x5pc8s)d175;@pjm(?dapmBcm0&vl9+W zx1ZD2o^nuyUHWj|^A8r>lUorO`wFF;>9XL-Jy!P}UXC{(z!FO%SH~8k`#|9;Q|eue zqWL0^Bp(fg_+Pkm!fDKRSY;+^@BF?AJE zCUWpXPst~hi_~u)SzYBDZroR+Z4xeHIlm_3Yc_9nZ(o_gg!jDgVa=E}Y8uDgem9`b zf=mfJ_@(BXSkW53B)F2s!&?_R4ptb1fYXlF++@vPhd=marQgEGRZS@B4g1Mu?euknL= z67P~tZ?*>-Hmi7GwlisNHHJDku-dSm7g@!=a}9cSL6Pa^w^2?&?$Oi8ibrr>w)xqx zOH_EMU@m05)9kuNR>>4@H%|){U$^yvVQ(YgOlh;5oU_-vivG-p4=LrN-k7D?*?u1u zsWly%tfAzKd6Fb=`eU2un_uaTXmcT#tlOL+aRS=kZZf}A7qT8lvcTx~7j` z*b>=z)mwg7%B2_!D0!1IZ?Nq{^Y$uI4Qx*6T!E2Col&2{k?ImCO=dD~A&9f9diXy^$x{6CwkBimn|1E09 zAMSezYtiL?O6hS37KpvDM?22&d{l)7h-!F)C-d3j8Z`c@($?mfd{R82)H>Qe`h{~G z!I}(2j(|49{LR?w4Jspl_i!(4T{31|dqCOpI52r5NhxYV+cDAu(xp*4iqZ2e-$YP= zoFOPmm|u*7C?S{Fp43y+V;>~@FFR76bCl@pTtyB93vNWy5yf;HKr8^0d7&GVIslYm zo3Tgt@M!`8B6IW&lK{Xk>%zp41G%`(DR&^u z5^pwD4>E6-w<8Kl2DzJ%a@~QDE$(e87lNhy?-Qgep!$b?5f7+&EM7$e>|WrX+=zCb z=!f5P>MxFyy;mIRxjc(H*}mceXw5a*IpC0PEYJ8Y3{JdoIW)@t97{wcUB@u+$FCCO z;s2Qe(d~oJC^`m$7DE-dsha`glrtu&v&93IZadvl_yjp!c89>zo;Krk+d&DEG4?x$ zufC1n+c1XD7dolX1q|7}uelR$`pT0Z)1jun<39$Sn2V5g&|(j~Z!wOddfYiZo7)A< z!dK`aBHOOk+-E_xbWCA3VR-+o$i5eO9`rMI#p_0xQ}rjEpGW;U!&&PKnivOcG(|m9 z!C8?WC6nCXw25WVa*eew)zQ=h45k8jSIPbq&?VE{oG%?4>9rwEeB4&qe#?-y_es4c|7ufw%+H5EY#oCgv!Lzv291#-oNlX~X+Jl5(riC~r z=0M|wMOP)Tt8@hNg&%V@Z9@J|Q#K*hE>sr6@oguas9&6^-=~$*2Gs%h#GF@h)i=Im z^iKk~ipWJg1VrvKS;_2lgs3n1zvNvxb27nGM=NXE!D4C!U`f*K2B@^^&ij9y}DTLB*FI zEnBL6y{jc?JqXWbkIZd7I16hA>(f9T!iwbIxJj~bKPfrO;>%*5nk&Lf?G@c2wvGrY&41$W{7HM9+b@&XY@>NZM5s|EK_Dp zQX60CBuantx>|d#DsaZ*8MW(we|#KTYZ=vNa#d*DJQe6hr~J6{_rI#?wi@s|&O}FR zG$kfPxheXh1?IZ{bDT-CWB4FTvO-k5scW^mi8?iY5Q`f8JcnnCxiy@m@D-%lO;y0pTLhh6i6l@x52j=#^$5_U^os}OFg zzdHbo(QI`%9#o*r8GCW~T3UdV`szO#~)^&X_(VW>o~umY9-ns9-V4lf~j z`QBD~pJ4a#b`*6bJ^3RS5y?RAgF7K5$ll97Y8#WZduZ`j?IEY~H(s^doZg>7-tk*t z4_QE1%%bb^p~4F5SB$t2i1>DBG1cIo;2(xTaj*Y~hlM{tSDHojL-QPg%Mo%6^7FrpB*{ z4G0@T{-77Por4DCMF zB_5Y~Phv%EQ64W8^GS6h?x6xh;w2{z3$rhC;m+;uD&pR74j+i22P5DS-tE8ABvH(U~indEbBUTAAAXfHZg5QpB@TgV9eI<)JrAkOI z8!TSOgfAJiWAXeM&vR4Glh;VxH}WG&V$bVb`a`g}GSpwggti*&)taV1@Ak|{WrV|5 zmNYx)Ans=S{c52qv@+jmGQ&vd6>6yX6IKq9O$3r&0xUTdZ!m1!irzn`SY+F23Rl6# zFRxws&gV-kM1NX(3(gnKpGi0Q)Dxi~#?nyzOR9!en;Ij>YJZVFAL*=R%7y%Mz9hU% zs>+ZB?qRmZ)nISx7wxY)y#cd$iaC~{k0avD>BjyF1q^mNQ1QcwsxiTySe<6C&cC6P zE`vwO9^k-d`9hZ!+r@Jnr+MF*2;2l8WjZ}DrwDUHzSF{WoG zucbSWguA!3KgB3MU%HH`R;XqVv0CcaGq?+;v_A5A2kpmk5V%qZE3yzQ7R5XWhq=eR zyUezH=@V)y>L9T-M-?tW(PQYTRBKZSVb_!$^H-Pn%ea;!vS_?M<~Tm>_rWIW43sPW z=!lY&fWc1g7+r?R)0p8(%zp&vl+FK4HRkns%BW+Up&wK8!lQ2~bja|9bD12WrKn#M zK)Yl9*8$SI7MAwSK$%)dMd>o+1UD<2&aQMhyjS5R{-vV+M;Q4bzl~Z~=4HFj_#2V9 zB)Gfzx3ncy@uzx?yzi}6>d%-?WE}h7v*w)Jr_gBl!2P&F3DX>j_1#--yjpL%<;JMR z*b70Gr)MMIBWDo~#<5F^Q0$VKI;SBIRneuR7)yVsN~A9I@gZTXe)E?iVII+X5h0~H zx^c(fP&4>!*q>fb6dAOC?MI>Cz3kld#J*;uik+Ps49cwm1B4 zZc1|ZxYyTv;{Z!?qS=D)sgRKx^1AYf%;y_V&VgZglfU>d+Ufk5&LV$sKv}Hoj+s; xK3FZRYdhbXT_@RW*ff3@`D1#ps#~H)p+y&j#(J|vk^lW{fF9OJt5(B-_&*Xgn9~3N literal 0 HcmV?d00001 diff --git a/test/dummy/public/icon.svg b/test/dummy/public/icon.svg new file mode 100644 index 0000000..04b34bf --- /dev/null +++ b/test/dummy/public/icon.svg @@ -0,0 +1,3 @@ + + + diff --git a/test/dummy/storage/.keep b/test/dummy/storage/.keep new file mode 100644 index 0000000..e69de29 From db0dd8344e5d6f9cf9c55ea107441391edb29c33 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Wed, 7 May 2025 16:29:17 -0400 Subject: [PATCH 03/33] Use dummy test app --- lib/superglue.rb | 10 +- .../controllers/admin/comments_controller.rb | 16 +++ .../app/controllers/admin/posts_controller.rb | 14 +++ .../app/controllers/application_controller.rb | 2 + test/dummy/app/controllers/jsx_controller.rb | 72 +++++++++++ test/dummy/config/initializers/assets.rb | 2 +- test/dummy/config/routes.rb | 1 + test/dummy/storage/test.sqlite3 | Bin 0 -> 20480 bytes test/engine_test.rb | 4 +- test/render_test.rb | 116 ++---------------- test/test_helper.rb | 41 ++++--- .../{render => jsx}/admin/index.json.props | 0 test/views/{render => jsx}/admin/index.jsx | 0 test/views/{render => jsx}/bad_pair.html.erb | 0 .../views/{render => jsx}/bad_pair.json.props | 0 .../views/{render => jsx}/bad_single.html.erb | 0 test/views/{render => jsx}/simple.html.erb | 0 test/views/{render => jsx}/simple.json.props | 0 test/views/{render => jsx}/simple.jsx | 0 .../single_props_only.json.props | 0 .../{render => jsx}/uncommon_pair.html.erb | 0 test/views/{render => jsx}/uncommon_pair.jsx | 0 .../{render => jsx}/valid_pair.json.props | 0 test/views/{render => jsx}/valid_pair.jsx | 0 test/views/{render => jsx}/valid_single.jsx | 0 ...tion.html.erb => jsx_application.html.erb} | 0 ....json.props => jsx_application.json.props} | 0 27 files changed, 149 insertions(+), 129 deletions(-) create mode 100644 test/dummy/app/controllers/admin/comments_controller.rb create mode 100644 test/dummy/app/controllers/admin/posts_controller.rb create mode 100644 test/dummy/app/controllers/jsx_controller.rb create mode 100644 test/dummy/storage/test.sqlite3 rename test/views/{render => jsx}/admin/index.json.props (100%) rename test/views/{render => jsx}/admin/index.jsx (100%) rename test/views/{render => jsx}/bad_pair.html.erb (100%) rename test/views/{render => jsx}/bad_pair.json.props (100%) rename test/views/{render => jsx}/bad_single.html.erb (100%) rename test/views/{render => jsx}/simple.html.erb (100%) rename test/views/{render => jsx}/simple.json.props (100%) rename test/views/{render => jsx}/simple.jsx (100%) rename test/views/{render => jsx}/single_props_only.json.props (100%) rename test/views/{render => jsx}/uncommon_pair.html.erb (100%) rename test/views/{render => jsx}/uncommon_pair.jsx (100%) rename test/views/{render => jsx}/valid_pair.json.props (100%) rename test/views/{render => jsx}/valid_pair.jsx (100%) rename test/views/{render => jsx}/valid_single.jsx (100%) rename test/views/layouts/{application.html.erb => jsx_application.html.erb} (100%) rename test/views/layouts/{application.json.props => jsx_application.json.props} (100%) diff --git a/lib/superglue.rb b/lib/superglue.rb index abe56b5..a9d5d00 100644 --- a/lib/superglue.rb +++ b/lib/superglue.rb @@ -25,13 +25,11 @@ class Engine < ::Rails::Engine ActiveSupport.on_load(:action_controller) do next if self != ActionController::Base - if app.config.superglue.auto_include - include Controller + include Controller - prepend_view_path( - Superglue::Resolver.new(Rails.root.join("app/views")) - ) - end + prepend_view_path( + Superglue::Resolver.new(Rails.root.join("app/views")) + ) end end end diff --git a/test/dummy/app/controllers/admin/comments_controller.rb b/test/dummy/app/controllers/admin/comments_controller.rb new file mode 100644 index 0000000..71c16fe --- /dev/null +++ b/test/dummy/app/controllers/admin/comments_controller.rb @@ -0,0 +1,16 @@ +module Admin + class ScopedController < ApplicationController + # require "action_view/testing/resolvers" + + before_action :use_jsx_rendering_defaults + + append_view_path(Superglue::Resolver.new("test/views")) + append_view_path "test/views" + + layout "application" + + def show + render "admin/posts/index" + end + end +end diff --git a/test/dummy/app/controllers/admin/posts_controller.rb b/test/dummy/app/controllers/admin/posts_controller.rb new file mode 100644 index 0000000..be94dde --- /dev/null +++ b/test/dummy/app/controllers/admin/posts_controller.rb @@ -0,0 +1,14 @@ +module Admin + class PostsController < ApplicationController + before_action :use_jsx_rendering_defaults + + append_view_path(Superglue::Resolver.new("test/views")) + append_view_path "test/views" + + layout "layouts/jsx_application" + + def show + render "admin/posts/index" + end + end +end diff --git a/test/dummy/app/controllers/application_controller.rb b/test/dummy/app/controllers/application_controller.rb index 0d95db2..8d08250 100644 --- a/test/dummy/app/controllers/application_controller.rb +++ b/test/dummy/app/controllers/application_controller.rb @@ -1,4 +1,6 @@ class ApplicationController < ActionController::Base # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. allow_browser versions: :modern + + # superglue_template "application/superglue" end diff --git a/test/dummy/app/controllers/jsx_controller.rb b/test/dummy/app/controllers/jsx_controller.rb new file mode 100644 index 0000000..c77eabb --- /dev/null +++ b/test/dummy/app/controllers/jsx_controller.rb @@ -0,0 +1,72 @@ +class JsxController < ApplicationController + before_action :use_jsx_rendering_defaults, except: [:valid_pair_no_defaults] + + append_view_path(Superglue::Resolver.new("test/views")) + append_view_path "test/views" + + layout "layouts/jsx_application" + + def simple + end + + def simple_explicit + render :simple + end + + def valid_pair + end + + def valid_pair_no_defaults + render :valid_pair + end + + def valid_single + render :valid_single + end + + def uncommon_pair + end + + def bad_single + end + + def bad_pair + end + + def render_does_not_exist + render :does_not_exist + end + + def simple_render_with_no_superglue_template + self._superglue_template = "superglue-template-does-not-exist" + render :valid_pair + end + + def unsupported_option_file + render file: "jsx/simple.html.erb" + end + + def unsupported_option_inline + render inline: "blah" + end + + def unsupported_option_html + render html: "

" + end + + def unsupported_option_body + render body: "

" + end + + def unsupported_option_partial + render partial: "some-partial" + end + + def unsupported_option_plain + render plain: "plain" + end + + def form_authenticity_token + "secret" + end +end diff --git a/test/dummy/config/initializers/assets.rb b/test/dummy/config/initializers/assets.rb index 4873244..68fd3a0 100644 --- a/test/dummy/config/initializers/assets.rb +++ b/test/dummy/config/initializers/assets.rb @@ -1,7 +1,7 @@ # Be sure to restart your server when you modify this file. # Version of your assets, change this if you want to expire all your assets. -Rails.application.config.assets.version = "1.0" +# Rails.application.config.assets.version = "1.0" # Add additional assets to the asset load path. # Rails.application.config.assets.paths << Emoji.images_path diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index 48254e8..a4c09dd 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -11,4 +11,5 @@ # Defines the root path route ("/") # root "posts#index" + get ":controller(/:action)" end diff --git a/test/dummy/storage/test.sqlite3 b/test/dummy/storage/test.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..d3cc8350db4bc3628d3808660dd63e06967cd221 GIT binary patch literal 20480 zcmeI%F;Buk6u|LoMKLCV8v}!vPLLRhF}S#hrZGlDMI4MnbHb^O6to-!M;!e=elaIU zuOt!;Fq@EnleTy7-nD(d-qQ5yvKksK@4IoNjNBC)!ga-gltKvC?#p)n*^++hr(@5~ zulBsAE4Byk>)9`n%H&0MKl_o%XFlg86Gs361Q0*~0R#|0009IL_!oguV=0|0mz}p$ zqwd;T#X;CHI_{`;5NV@Y%BWef)KT59GL+KF`i(sGZ?>d2Q0=~+WZ6J7*2?Hspo}N&{j~`rUDzv5=G&D1b22bLrFfiL z$=#M65q4U7*n4T)&)-1xO?TW6W<74OGb_40bkey}$r){qd3w!99jPD+A7W*~Zl^aD zaR0(gjVCGt9rx_yNzIeWe#;aq?n>^uG_QoI3kN$>p(VSbR+Ck=o*Xuk0~-PeAbk-7SqKOqfB*sr oAb" - end - - def unsupported_option_body - render body: "

" - end - - def unsupported_option_partial - render partial: "some-partial" - end - - def unsupported_option_plain - render plain: "plain" - end - - def form_authenticity_token - "secret" - end -end - -module Admin - class ScopedController < TestController - require "action_view/testing/resolvers" - - before_action :use_jsx_rendering_defaults - - append_view_path(Superglue::Resolver.new("test/views")) - append_view_path "test/views" - - layout "application" - - def show - render "admin/posts/index" - end - end -end - class ReprodTest < ActionController::TestCase - tests Admin::ScopedController + tests Admin::PostsController test "templates with prefixes render" do get :show @@ -113,7 +21,7 @@ class ReprodTest < ActionController::TestCase end class RenderTest < ActionController::TestCase - tests RenderController + tests JsxController test "simple render with 3 templates (jsx, html, props)" do get :simple @@ -168,7 +76,7 @@ class RenderTest < ActionController::TestCase get :valid_pair_no_defaults } - assert_match("Missing template render/valid_pair", exception.message) + assert_match("Missing template jsx/valid_pair", exception.message) end test "render with a valid single template (jsx)" do @@ -208,7 +116,7 @@ class RenderTest < ActionController::TestCase get :bad_single } - assert_match("Missing template render/bad_single", exception.message) + assert_match("Missing template jsx/bad_single", exception.message) end test "render with bad pair of templates (html, json)" do @@ -216,7 +124,7 @@ class RenderTest < ActionController::TestCase get :bad_pair } - assert_match("Missing template render/bad_pair", exception.message) + assert_match("Missing template jsx/bad_pair", exception.message) end test "rendering props only" do @@ -235,7 +143,7 @@ class RenderTest < ActionController::TestCase exception = assert_raise(ActionView::MissingTemplate) { get :render_does_not_exist } - assert_match("Missing template render/does_not_exist", exception.message) + assert_match("Missing template jsx/does_not_exist", exception.message) end test "non existant superglue template" do @@ -244,37 +152,37 @@ class RenderTest < ActionController::TestCase } assert_match("Missing template /superglue-template-does-not-exist", exception.message) end - + test "unsupported render file:" do assert_raise(Superglue::Rendering::UnsupportedOption) { get :unsupported_option_file } end - + test "unsupported render partial:" do assert_raise(Superglue::Rendering::UnsupportedOption) { get :unsupported_option_partial } end - + test "unsupported render body:" do assert_raise(Superglue::Rendering::UnsupportedOption) { get :unsupported_option_body } end - + test "unsupported render plain:" do assert_raise(Superglue::Rendering::UnsupportedOption) { get :unsupported_option_plain } end - + test "unsupported render html:" do assert_raise(Superglue::Rendering::UnsupportedOption) { get :unsupported_option_html } end - + test "unsupported render inline:" do assert_raise(Superglue::Rendering::UnsupportedOption) { get :unsupported_option_inline diff --git a/test/test_helper.rb b/test/test_helper.rb index 21fc425..def2ae1 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,19 +1,28 @@ -require "rails" -require "abstract_controller" -require "abstract_controller/railties/routes_helpers" -require "action_controller" -require "superglue" -require "active_support" -require "active_record" -require "active_support/testing/autorun" -require "active_support/test_case" +# Configure Rails Environment +ENV["RAILS_ENV"] = "test" -require "props_template" +require_relative "../test/dummy/config/environment" +# ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)] +require "rails/test_help" -ActiveSupport::TestCase.test_order = :random if ActiveSupport::TestCase.respond_to?(:test_order=) -ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" -Rails.cache = ActiveSupport::Cache::MemoryStore.new +ActionCable.server.config.logger = Logger.new(STDOUT) if ENV["VERBOSE"] -load File.dirname(__FILE__) + "/support/schema.rb" -require "support/models" -require "support/test_application" +module ActionViewTestCaseExtensions + delegate :render, to: ApplicationController +end + +class ActiveSupport::TestCase + include ActiveJob::TestHelper + + setup do + # Turbo.current_request_id = nil + end +end + +class ActionDispatch::IntegrationTest + include ActionViewTestCaseExtensions +end + +class ActionCable::Channel::TestCase + include ActionViewTestCaseExtensions +end diff --git a/test/views/render/admin/index.json.props b/test/views/jsx/admin/index.json.props similarity index 100% rename from test/views/render/admin/index.json.props rename to test/views/jsx/admin/index.json.props diff --git a/test/views/render/admin/index.jsx b/test/views/jsx/admin/index.jsx similarity index 100% rename from test/views/render/admin/index.jsx rename to test/views/jsx/admin/index.jsx diff --git a/test/views/render/bad_pair.html.erb b/test/views/jsx/bad_pair.html.erb similarity index 100% rename from test/views/render/bad_pair.html.erb rename to test/views/jsx/bad_pair.html.erb diff --git a/test/views/render/bad_pair.json.props b/test/views/jsx/bad_pair.json.props similarity index 100% rename from test/views/render/bad_pair.json.props rename to test/views/jsx/bad_pair.json.props diff --git a/test/views/render/bad_single.html.erb b/test/views/jsx/bad_single.html.erb similarity index 100% rename from test/views/render/bad_single.html.erb rename to test/views/jsx/bad_single.html.erb diff --git a/test/views/render/simple.html.erb b/test/views/jsx/simple.html.erb similarity index 100% rename from test/views/render/simple.html.erb rename to test/views/jsx/simple.html.erb diff --git a/test/views/render/simple.json.props b/test/views/jsx/simple.json.props similarity index 100% rename from test/views/render/simple.json.props rename to test/views/jsx/simple.json.props diff --git a/test/views/render/simple.jsx b/test/views/jsx/simple.jsx similarity index 100% rename from test/views/render/simple.jsx rename to test/views/jsx/simple.jsx diff --git a/test/views/render/single_props_only.json.props b/test/views/jsx/single_props_only.json.props similarity index 100% rename from test/views/render/single_props_only.json.props rename to test/views/jsx/single_props_only.json.props diff --git a/test/views/render/uncommon_pair.html.erb b/test/views/jsx/uncommon_pair.html.erb similarity index 100% rename from test/views/render/uncommon_pair.html.erb rename to test/views/jsx/uncommon_pair.html.erb diff --git a/test/views/render/uncommon_pair.jsx b/test/views/jsx/uncommon_pair.jsx similarity index 100% rename from test/views/render/uncommon_pair.jsx rename to test/views/jsx/uncommon_pair.jsx diff --git a/test/views/render/valid_pair.json.props b/test/views/jsx/valid_pair.json.props similarity index 100% rename from test/views/render/valid_pair.json.props rename to test/views/jsx/valid_pair.json.props diff --git a/test/views/render/valid_pair.jsx b/test/views/jsx/valid_pair.jsx similarity index 100% rename from test/views/render/valid_pair.jsx rename to test/views/jsx/valid_pair.jsx diff --git a/test/views/render/valid_single.jsx b/test/views/jsx/valid_single.jsx similarity index 100% rename from test/views/render/valid_single.jsx rename to test/views/jsx/valid_single.jsx diff --git a/test/views/layouts/application.html.erb b/test/views/layouts/jsx_application.html.erb similarity index 100% rename from test/views/layouts/application.html.erb rename to test/views/layouts/jsx_application.html.erb diff --git a/test/views/layouts/application.json.props b/test/views/layouts/jsx_application.json.props similarity index 100% rename from test/views/layouts/application.json.props rename to test/views/layouts/jsx_application.json.props From 8539daa5471ce91a11b7f6b720ca59689ccfaf67 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Wed, 7 May 2025 22:25:21 -0400 Subject: [PATCH 04/33] first-tst-pass --- .gitignore | 1 - app/channels/superglue/streams/broadcasts.rb | 128 ++++++ app/channels/superglue/streams/stream_name.rb | 32 ++ app/channels/superglue/streams_channel.rb | 13 + lib/superglue.rb | 54 +-- lib/superglue/engine.rb | 37 ++ test/streams/streams_channel_test.rb | 365 ++++++++++++++++++ 7 files changed, 603 insertions(+), 27 deletions(-) create mode 100644 app/channels/superglue/streams/broadcasts.rb create mode 100644 app/channels/superglue/streams/stream_name.rb create mode 100644 app/channels/superglue/streams_channel.rb create mode 100644 lib/superglue/engine.rb create mode 100644 test/streams/streams_channel_test.rb diff --git a/.gitignore b/.gitignore index 80066c2..f9a7334 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,3 @@ breezy/build/**/*.js props_template/performance/**/*.png .tool-versions testapp/ -superglue/ diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb new file mode 100644 index 0000000..ebc209b --- /dev/null +++ b/app/channels/superglue/streams/broadcasts.rb @@ -0,0 +1,128 @@ +# Provides the broadcast actions in synchronous and asynchronous form for the Superglue::StreamsChannel. +# See Superglue::Broadcastable for the user-facing API that invokes these methods with most of the paperwork filled out already. +# +# Can be used directly using something like Superglue::StreamsChannel.broadcast_remove_to :entries, target: 1. +module Superglue::Streams::Broadcasts + # include Superglue::Streams::ActionHelper + + def broadcast_remove_to(*streamables, **opts) + broadcast_action_to(*streamables, action: :remove, render: false, **opts) + end + + def broadcast_replace_to(*streamables, **opts) + broadcast_action_to(*streamables, action: :replace, **opts) + end + + def broadcast_update_to(*streamables, **opts) + broadcast_action_to(*streamables, action: :update, **opts) + end + + def broadcast_before_to(*streamables, **opts) + broadcast_action_to(*streamables, action: :before, **opts) + end + + def broadcast_after_to(*streamables, **opts) + broadcast_action_to(*streamables, action: :after, **opts) + end + + def broadcast_append_to(*streamables, **opts) + broadcast_action_to(*streamables, action: :append, **opts) + end + + def broadcast_prepend_to(*streamables, **opts) + broadcast_action_to(*streamables, action: :prepend, **opts) + end + + def broadcast_refresh_to(*streamables, **opts) + broadcast_stream_to(*streamables, content: superglue_stream_refresh_tag) + end + + def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) + broadcast_stream_to(*streamables, content: superglue_stream_action_tag( + action, target: target, targets: targets, template: render_broadcast_action(rendering), **attributes + )) + end + + def broadcast_replace_later_to(*streamables, **opts) + broadcast_action_later_to(*streamables, action: :replace, **opts) + end + + def broadcast_update_later_to(*streamables, **opts) + broadcast_action_later_to(*streamables, action: :update, **opts) + end + + def broadcast_before_later_to(*streamables, **opts) + broadcast_action_later_to(*streamables, action: :before, **opts) + end + + def broadcast_after_later_to(*streamables, **opts) + broadcast_action_later_to(*streamables, action: :after, **opts) + end + + def broadcast_append_later_to(*streamables, **opts) + broadcast_action_later_to(*streamables, action: :append, **opts) + end + + def broadcast_prepend_later_to(*streamables, **opts) + broadcast_action_later_to(*streamables, action: :prepend, **opts) + end + + def broadcast_refresh_later_to(*streamables, request_id: Superglue.current_request_id, **opts) + stream_name = stream_name_from(streamables) + + refresh_debouncer_for(*streamables, request_id: request_id).debounce do + Superglue::Streams::BroadcastStreamJob.perform_later stream_name, content: superglue_stream_refresh_tag(request_id: request_id, **opts).to_str # Sidekiq requires job arguments to be valid JSON types, such as String + end + end + + def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) + streamables.flatten! + streamables.compact_blank! + + return unless streamables.present? + + target = convert_to_superglue_stream_dom_id(target) + targets = convert_to_superglue_stream_dom_id(targets, include_selector: true) + Superglue::Streams::ActionBroadcastJob.perform_later \ + stream_name_from(streamables), action: action, target: target, targets: targets, attributes: attributes, **rendering + end + + def broadcast_render_to(*streamables, **rendering) + broadcast_stream_to(*streamables, content: render_format(:superglue_stream, **rendering)) + end + + def broadcast_render_later_to(*streamables, **rendering) + Superglue::Streams::BroadcastJob.perform_later stream_name_from(streamables), **rendering + end + + def broadcast_stream_to(*streamables, content:) + streamables.flatten! + streamables.compact_blank! + + return unless streamables.present? + + ActionCable.server.broadcast stream_name_from(streamables), content + end + + def refresh_debouncer_for(*streamables, request_id: nil) # :nodoc: + Superglue::ThreadDebouncer.for("superglue-refresh-debouncer-#{stream_name_from(streamables.including(request_id))}") + end + + private + + def render_format(format, **rendering) + ApplicationController.render(formats: [format], **rendering) + end + + def render_broadcast_action(rendering) + content = rendering.delete(:content) + html = rendering.delete(:html) + render = rendering.delete(:render) + + if render == false + nil + else + content || html || (render_format(:html, **rendering) if rendering.present?) + end + end +end diff --git a/app/channels/superglue/streams/stream_name.rb b/app/channels/superglue/streams/stream_name.rb new file mode 100644 index 0000000..c20cb5f --- /dev/null +++ b/app/channels/superglue/streams/stream_name.rb @@ -0,0 +1,32 @@ +# Stream names are how we identify which updates should go to which users. All streams run over the same +# Superglue::StreamsChannel, but each with their own subscription. Since stream names are exposed directly to the user +# via the HTML stream subscription tags, we need to ensure that the name isn't tampered with, so the names are signed +# upon generation and verified upon receipt. All verification happens through the Superglue.signed_stream_verifier. +module Superglue::Streams::StreamName + # Used by Superglue::StreamsChannel to verify a signed stream name. + def verified_stream_name(signed_stream_name) + Superglue.signed_stream_verifier.verified signed_stream_name + end + + # Used by Superglue::StreamsHelper#Superglue_stream_from(*streamables) to generate a signed stream name. + def signed_stream_name(streamables) + Superglue.signed_stream_verifier.generate stream_name_from(streamables) + end + + module ClassMethods + # Can be used by custom Superglue stream channels to obtain signed stream name from params + def verified_stream_name_from_params + self.class.verified_stream_name(params[:signed_stream_name]) + end + end + + private + + def stream_name_from(streamables) + if streamables.is_a?(Array) + streamables.map { |streamable| stream_name_from(streamable) }.join(':') + else + streamables.then { |streamable| streamable.try(:to_gid_param) || streamable.to_param } + end + end +end diff --git a/app/channels/superglue/streams_channel.rb b/app/channels/superglue/streams_channel.rb new file mode 100644 index 0000000..c43be4d --- /dev/null +++ b/app/channels/superglue/streams_channel.rb @@ -0,0 +1,13 @@ +class Superglue::StreamsChannel < ActionCable::Channel::Base + extend Superglue::Streams::StreamName + extend Superglue::Streams::Broadcasts + include Superglue::Streams::StreamName::ClassMethods + + def subscribed + if stream_name = verified_stream_name_from_params + stream_from stream_name + else + reject + end + end +end diff --git a/lib/superglue.rb b/lib/superglue.rb index a9d5d00..c456bf4 100644 --- a/lib/superglue.rb +++ b/lib/superglue.rb @@ -1,36 +1,38 @@ -require "superglue/helpers" -require "superglue/rendering" -require "superglue/resolver" -require "props_template" -require "form_props" +require 'superglue/helpers' +require 'superglue/rendering' +require 'superglue/resolver' +require 'superglue/engine' +require 'props_template' +require 'form_props' module Superglue - module Controller - include Helpers + extend ActiveSupport::Autoload - def self.included(base) - base.include ::Superglue::Rendering - if base.respond_to?(:helper_method) - base.helper_method :param_to_dig_path - base.helper_method :render_props - end - end - end + mattr_accessor :draw_routes, default: true - class Engine < ::Rails::Engine - config.superglue = ActiveSupport::OrderedOptions.new - config.superglue.auto_include = true + thread_mattr_accessor :current_request_id - initializer :superglue do |app| - ActiveSupport.on_load(:action_controller) do - next if self != ActionController::Base + class << self + attr_writer :signed_stream_verifier_key - include Controller + def signed_stream_verifier + @signed_stream_verifier ||= ActiveSupport::MessageVerifier.new( + signed_stream_verifier_key, + digest: 'SHA256', + serializer: JSON + ) + end + + def signed_stream_verifier_key + @signed_stream_verifier_key or raise ArgumentError, 'Superglue requires a signed_stream_verifier_key' + end - prepend_view_path( - Superglue::Resolver.new(Rails.root.join("app/views")) - ) - end + def with_request_id(request_id) + old_request_id = current_request_id + self.current_request_id = request_id + yield + ensure + self.current_request_id = old_request_id end end end diff --git a/lib/superglue/engine.rb b/lib/superglue/engine.rb new file mode 100644 index 0000000..1de2a72 --- /dev/null +++ b/lib/superglue/engine.rb @@ -0,0 +1,37 @@ +module Superglue + module Controller + include Helpers + + def self.included(base) + base.include ::Superglue::Rendering + return unless base.respond_to?(:helper_method) + + base.helper_method :param_to_dig_path + base.helper_method :render_props + end + end + + class Engine < ::Rails::Engine + config.superglue = ActiveSupport::OrderedOptions.new + config.superglue.auto_include = true + + initializer :superglue do |app| + ActiveSupport.on_load(:action_controller) do + next if self != ActionController::Base + + include Controller + + prepend_view_path( + Superglue::Resolver.new(Rails.root.join('app/views')) + ) + end + end + + initializer 'superglue.signed_stream_verifier_key' do + config.after_initialize do + Superglue.signed_stream_verifier_key = config.superglue.signed_stream_verifier_key || + Rails.application.key_generator.generate_key('superglue/signed_stream_verifier_key') + end + end + end +end diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb new file mode 100644 index 0000000..e74d35e --- /dev/null +++ b/test/streams/streams_channel_test.rb @@ -0,0 +1,365 @@ +require 'test_helper' +require 'action_cable' + +class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase + # include Superglue::Streams::ActionHelper + include ActiveJob::TestHelper + + test 'verified stream name' do + assert_equal 'stream', + Superglue::StreamsChannel.verified_stream_name(Superglue::StreamsChannel.signed_stream_name('stream')) + end + + # test 'broadcasting remove now' do + # assert_broadcast_on 'stream', turbo_stream_action_tag('remove', target: 'message_1') do + # Superglue::StreamsChannel.broadcast_remove_to 'stream', target: 'message_1' + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('remove', targets: '.message') do + # Superglue::StreamsChannel.broadcast_remove_to 'stream', targets: '.message' + # end + # end + + # test 'broadcasting remove now with record' do + # assert_broadcast_on 'stream', turbo_stream_action_tag('remove', target: 'message_1') do + # Superglue::StreamsChannel.broadcast_remove_to 'stream', target: Message.new(id: 1, content: 'hello!') + # end + # end + + # test 'broadcasting replace now' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', target: 'message_1', template: render(options)) do + # Superglue::StreamsChannel.broadcast_replace_to 'stream', target: 'message_1', **options + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', targets: '.message', template: render(options)) do + # Superglue::StreamsChannel.broadcast_replace_to 'stream', targets: '.message', **options + # end + # end + + # test 'broadcasting update now' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('update', target: 'message_1', template: render(options)) do + # Superglue::StreamsChannel.broadcast_update_to 'stream', target: 'message_1', **options + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('update', targets: '.message', template: render(options)) do + # Superglue::StreamsChannel.broadcast_update_to 'stream', targets: '.message', **options + # end + # end + + # test 'broadcasting append now' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('append', target: 'messages', template: render(options)) do + # Superglue::StreamsChannel.broadcast_append_to 'stream', target: 'messages', **options + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('append', targets: '.message', template: render(options)) do + # Superglue::StreamsChannel.broadcast_append_to 'stream', targets: '.message', **options + # end + # end + + # test 'broadcasting append now with empty template' do + # assert_broadcast_on 'stream', + # %() do + # Superglue::StreamsChannel.broadcast_append_to 'stream', target: 'message_1', content: '' + # end + # end + + # test 'broadcasting prepend now' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', target: 'messages', template: render(options)) do + # Superglue::StreamsChannel.broadcast_prepend_to 'stream', target: 'messages', **options + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', targets: '.message', template: render(options)) do + # Superglue::StreamsChannel.broadcast_prepend_to 'stream', targets: '.message', **options + # end + # end + + # test 'broadcasting action now' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', target: 'messages', template: render(options)) do + # Superglue::StreamsChannel.broadcast_action_to 'stream', action: 'prepend', target: 'messages', **options + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', targets: '.message', template: render(options)) do + # Superglue::StreamsChannel.broadcast_action_to 'stream', action: 'prepend', targets: '.message', **options + # end + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('prepend', targets: '.message', template: 'test') do + # Superglue::StreamsChannel.broadcast_action_to 'stream', action: 'prepend', targets: '.message', + # content: 'test' + # end + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('prepend', targets: '.message', template: 'test') do + # Superglue::StreamsChannel.broadcast_action_to 'stream', action: 'prepend', targets: '.message', + # html: 'test' + # end + # end + + # test 'broadcasting replace later' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', target: 'message_1', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_replace_later_to \ + # 'stream', target: 'message_1', **options + # end + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', targets: '.message', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_replace_later_to \ + # 'stream', targets: '.message', **options + # end + # end + # end + + # test 'broadcasting update later' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('update', target: 'message_1', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_update_later_to \ + # 'stream', target: 'message_1', **options + # end + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('update', targets: '.message', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_update_later_to \ + # 'stream', targets: '.message', **options + # end + # end + # end + + # test 'broadcasting append later' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('append', target: 'messages', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_append_later_to \ + # 'stream', target: 'messages', **options + # end + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('append', targets: '.message', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_append_later_to \ + # 'stream', targets: '.message', **options + # end + # end + # end + + # test 'broadcasting prepend later' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', target: 'messages', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_prepend_later_to \ + # 'stream', target: 'messages', **options + # end + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', targets: '.message', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_prepend_later_to \ + # 'stream', targets: '.message', **options + # end + # end + # end + + # test 'broadcasting refresh later' do + # assert_broadcast_on 'stream', turbo_stream_refresh_tag do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' + # Superglue::StreamsChannel.refresh_debouncer_for('stream').wait + # end + # end + + # Turbo.current_request_id = '123' + # assert_broadcast_on 'stream', turbo_stream_refresh_tag(request_id: '123') do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' + # Superglue::StreamsChannel.refresh_debouncer_for('stream', request_id: '123').wait + # end + # end + # end + + # test 'broadcasting refresh later is debounced' do + # assert_broadcast_on 'stream', turbo_stream_refresh_tag do + # assert_broadcasts('stream', 1) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' + + # Superglue::StreamsChannel.refresh_debouncer_for('stream').wait + # end + # end + # end + # end + + # test 'broadcasting refresh later is debounced considering the current request id' do + # assert_broadcasts('stream', 2) do + # perform_enqueued_jobs do + # assert_broadcast_on 'stream', turbo_stream_refresh_tag("request-id": '123') do + # assert_broadcast_on 'stream', turbo_stream_refresh_tag("request-id": '456') do + # Turbo.current_request_id = '123' + # 3.times { Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' } + + # Turbo.current_request_id = '456' + # 3.times { Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' } + + # Superglue::StreamsChannel.refresh_debouncer_for('stream', request_id: '123').wait + # Superglue::StreamsChannel.refresh_debouncer_for('stream', request_id: '456').wait + # end + # end + # end + # end + # end + + # test 'broadcasting action later' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', target: 'messages', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_action_later_to \ + # 'stream', action: 'prepend', target: 'messages', **options + # end + # end + + # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', targets: '.message', template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_action_later_to \ + # 'stream', action: 'prepend', targets: '.message', **options + # end + # end + # end + + # test 'broadcasting action later with ActiveModel array target' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # message = Message.new(id: 42) + # target = [message, 'opt'] + # expected_target = 'opt_message_42' + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('prepend', target: expected_target, template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_action_later_to \ + # 'stream', action: 'prepend', target: target, **options + # end + # end + # end + + # test 'broadcasting action later with multiple ActiveModel targets' do + # options = { partial: 'messages/message', locals: { message: 'hello!' } } + + # one = Message.new(id: 1) + # targets = [one, 'messages'] + # expected_targets = '#messages_message_1' + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('prepend', targets: expected_targets, template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_action_later_to \ + # 'stream', action: 'prepend', targets: targets, **options + # end + # end + # end + + # test 'broadcasting render now' do + # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', target: 'message_1', template: 'Goodbye!') do + # Superglue::StreamsChannel.broadcast_render_to 'stream', partial: 'messages/message' + # end + # end + + # test 'broadcasting render later' do + # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', target: 'message_1', template: 'Goodbye!') do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_render_later_to 'stream', partial: 'messages/message' + # end + # end + # end + + # test 'broadcasting direct update now' do + # assert_broadcast_on 'stream', %(direct) do + # Superglue::StreamsChannel.broadcast_stream_to 'stream', content: 'direct' + # end + # end + + # test 'broadcasting actions with method morph now' do + # options = { attributes: { method: :morph }, partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('replace', target: 'message_1', method: :morph, + # template: render(options)) do + # Superglue::StreamsChannel.broadcast_replace_to 'stream', target: 'message_1', **options + # end + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('replace', targets: '.message', method: :morph, + # template: render(options)) do + # Superglue::StreamsChannel.broadcast_replace_to 'stream', targets: '.message', **options + # end + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('update', target: 'message_1', method: :morph, + # template: render(options)) do + # Superglue::StreamsChannel.broadcast_update_to 'stream', target: 'message_1', **options + # end + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('update', targets: '.message', method: :morph, + # template: render(options)) do + # Superglue::StreamsChannel.broadcast_update_to 'stream', targets: '.message', **options + # end + # end + + # test 'broadcasting actions with method morph later' do + # options = { attributes: { method: :morph }, partial: 'messages/message', locals: { message: 'hello!' } } + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('replace', target: 'message_1', method: :morph, + # template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_replace_later_to \ + # 'stream', target: 'message_1', **options + # end + # end + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('replace', targets: '.message', method: :morph, + # template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_replace_later_to \ + # 'stream', targets: '.message', **options + # end + # end + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('update', target: 'message_1', method: :morph, + # template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_update_later_to \ + # 'stream', target: 'message_1', **options + # end + # end + + # assert_broadcast_on 'stream', + # turbo_stream_action_tag('update', targets: '.message', method: :morph, + # template: render(options)) do + # perform_enqueued_jobs do + # Superglue::StreamsChannel.broadcast_update_later_to \ + # 'stream', targets: '.message', **options + # end + # end + # end +end From b556f225089ed14e01177211960dbaaa8a18761e Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Wed, 7 May 2025 23:10:15 -0400 Subject: [PATCH 05/33] focusonone --- app/channels/superglue/streams/broadcasts.rb | 126 +++++++++--------- .../app/views/messages/_message.json.props | 1 + test/streams/streams_channel_test.rb | 18 +-- 3 files changed, 73 insertions(+), 72 deletions(-) create mode 100644 test/dummy/app/views/messages/_message.json.props diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index ebc209b..0856edf 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -5,37 +5,37 @@ module Superglue::Streams::Broadcasts # include Superglue::Streams::ActionHelper - def broadcast_remove_to(*streamables, **opts) - broadcast_action_to(*streamables, action: :remove, render: false, **opts) - end + # def broadcast_remove_to(*streamables, **opts) + # broadcast_action_to(*streamables, action: :remove, render: false, **opts) + # end def broadcast_replace_to(*streamables, **opts) broadcast_action_to(*streamables, action: :replace, **opts) end - def broadcast_update_to(*streamables, **opts) - broadcast_action_to(*streamables, action: :update, **opts) - end + # def broadcast_update_to(*streamables, **opts) + # broadcast_action_to(*streamables, action: :update, **opts) + # end - def broadcast_before_to(*streamables, **opts) - broadcast_action_to(*streamables, action: :before, **opts) - end + # def broadcast_before_to(*streamables, **opts) + # broadcast_action_to(*streamables, action: :before, **opts) + # end - def broadcast_after_to(*streamables, **opts) - broadcast_action_to(*streamables, action: :after, **opts) - end + # def broadcast_after_to(*streamables, **opts) + # broadcast_action_to(*streamables, action: :after, **opts) + # end - def broadcast_append_to(*streamables, **opts) - broadcast_action_to(*streamables, action: :append, **opts) - end + # def broadcast_append_to(*streamables, **opts) + # broadcast_action_to(*streamables, action: :append, **opts) + # end - def broadcast_prepend_to(*streamables, **opts) - broadcast_action_to(*streamables, action: :prepend, **opts) - end + # def broadcast_prepend_to(*streamables, **opts) + # broadcast_action_to(*streamables, action: :prepend, **opts) + # end - def broadcast_refresh_to(*streamables, **opts) - broadcast_stream_to(*streamables, content: superglue_stream_refresh_tag) - end + # def broadcast_refresh_to(*streamables, **opts) + # broadcast_stream_to(*streamables, content: superglue_stream_refresh_tag) + # end def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) broadcast_stream_to(*streamables, content: superglue_stream_action_tag( @@ -43,57 +43,57 @@ def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attrib )) end - def broadcast_replace_later_to(*streamables, **opts) - broadcast_action_later_to(*streamables, action: :replace, **opts) - end + # def broadcast_replace_later_to(*streamables, **opts) + # broadcast_action_later_to(*streamables, action: :replace, **opts) + # end - def broadcast_update_later_to(*streamables, **opts) - broadcast_action_later_to(*streamables, action: :update, **opts) - end + # def broadcast_update_later_to(*streamables, **opts) + # broadcast_action_later_to(*streamables, action: :update, **opts) + # end - def broadcast_before_later_to(*streamables, **opts) - broadcast_action_later_to(*streamables, action: :before, **opts) - end + # def broadcast_before_later_to(*streamables, **opts) + # broadcast_action_later_to(*streamables, action: :before, **opts) + # end - def broadcast_after_later_to(*streamables, **opts) - broadcast_action_later_to(*streamables, action: :after, **opts) - end + # def broadcast_after_later_to(*streamables, **opts) + # broadcast_action_later_to(*streamables, action: :after, **opts) + # end - def broadcast_append_later_to(*streamables, **opts) - broadcast_action_later_to(*streamables, action: :append, **opts) - end + # def broadcast_append_later_to(*streamables, **opts) + # broadcast_action_later_to(*streamables, action: :append, **opts) + # end - def broadcast_prepend_later_to(*streamables, **opts) - broadcast_action_later_to(*streamables, action: :prepend, **opts) - end + # def broadcast_prepend_later_to(*streamables, **opts) + # broadcast_action_later_to(*streamables, action: :prepend, **opts) + # end - def broadcast_refresh_later_to(*streamables, request_id: Superglue.current_request_id, **opts) - stream_name = stream_name_from(streamables) + # def broadcast_refresh_later_to(*streamables, request_id: Superglue.current_request_id, **opts) + # stream_name = stream_name_from(streamables) - refresh_debouncer_for(*streamables, request_id: request_id).debounce do - Superglue::Streams::BroadcastStreamJob.perform_later stream_name, content: superglue_stream_refresh_tag(request_id: request_id, **opts).to_str # Sidekiq requires job arguments to be valid JSON types, such as String - end - end + # refresh_debouncer_for(*streamables, request_id: request_id).debounce do + # Superglue::Streams::BroadcastStreamJob.perform_later stream_name, content: superglue_stream_refresh_tag(request_id: request_id, **opts).to_str # Sidekiq requires job arguments to be valid JSON types, such as String + # end + # end - def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) - streamables.flatten! - streamables.compact_blank! + # def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) + # streamables.flatten! + # streamables.compact_blank! - return unless streamables.present? + # return unless streamables.present? - target = convert_to_superglue_stream_dom_id(target) - targets = convert_to_superglue_stream_dom_id(targets, include_selector: true) - Superglue::Streams::ActionBroadcastJob.perform_later \ - stream_name_from(streamables), action: action, target: target, targets: targets, attributes: attributes, **rendering - end + # target = convert_to_superglue_stream_dom_id(target) + # targets = convert_to_superglue_stream_dom_id(targets, include_selector: true) + # Superglue::Streams::ActionBroadcastJob.perform_later \ + # stream_name_from(streamables), action: action, target: target, targets: targets, attributes: attributes, **rendering + # end - def broadcast_render_to(*streamables, **rendering) - broadcast_stream_to(*streamables, content: render_format(:superglue_stream, **rendering)) - end + # def broadcast_render_to(*streamables, **rendering) + # broadcast_stream_to(*streamables, content: render_format(:superglue_stream, **rendering)) + # end - def broadcast_render_later_to(*streamables, **rendering) - Superglue::Streams::BroadcastJob.perform_later stream_name_from(streamables), **rendering - end + # def broadcast_render_later_to(*streamables, **rendering) + # Superglue::Streams::BroadcastJob.perform_later stream_name_from(streamables), **rendering + # end def broadcast_stream_to(*streamables, content:) streamables.flatten! @@ -104,9 +104,9 @@ def broadcast_stream_to(*streamables, content:) ActionCable.server.broadcast stream_name_from(streamables), content end - def refresh_debouncer_for(*streamables, request_id: nil) # :nodoc: - Superglue::ThreadDebouncer.for("superglue-refresh-debouncer-#{stream_name_from(streamables.including(request_id))}") - end + # def refresh_debouncer_for(*streamables, request_id: nil) # :nodoc: + # Superglue::ThreadDebouncer.for("superglue-refresh-debouncer-#{stream_name_from(streamables.including(request_id))}") + # end private diff --git a/test/dummy/app/views/messages/_message.json.props b/test/dummy/app/views/messages/_message.json.props new file mode 100644 index 0000000..4c86bd1 --- /dev/null +++ b/test/dummy/app/views/messages/_message.json.props @@ -0,0 +1 @@ +json.body "Goodbye!" \ No newline at end of file diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index e74d35e..557fc01 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -26,17 +26,17 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase # end # end - # test 'broadcasting replace now' do - # options = { partial: 'messages/message', locals: { message: 'hello!' } } + test 'broadcasting replace now' do + options = { partial: 'messages/message', locals: { message: 'hello!' } } - # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', target: 'message_1', template: render(options)) do - # Superglue::StreamsChannel.broadcast_replace_to 'stream', target: 'message_1', **options - # end + assert_broadcast_on 'stream', turbo_stream_action_tag('replace', target: 'message_1', template: render(options)) do + Superglue::StreamsChannel.broadcast_replace_to 'stream', target: 'message_1', **options + end - # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', targets: '.message', template: render(options)) do - # Superglue::StreamsChannel.broadcast_replace_to 'stream', targets: '.message', **options - # end - # end + # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', targets: '.message', template: render(options)) do + # Superglue::StreamsChannel.broadcast_replace_to 'stream', targets: '.message', **options + # end + end # test 'broadcasting update now' do # options = { partial: 'messages/message', locals: { message: 'hello!' } } From c11ba498cf0fc071ea7aac289b98d281249d46e9 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Thu, 8 May 2025 21:06:58 -0400 Subject: [PATCH 06/33] Works with layout --- Gemfile | 2 +- app/channels/superglue/streams/broadcasts.rb | 7 +++---- app/views/superglue/layouts/_fragment.json.props | 3 +++ test/streams/streams_channel_test.rb | 9 +++++++-- test/test_helper.rb | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 app/views/superglue/layouts/_fragment.json.props diff --git a/Gemfile b/Gemfile index 3b3d0a2..0a587ec 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,7 @@ gemspec gem 'rails', '~> 7.2.0' gem 'selenium-webdriver' -gem 'props_template' +gem 'props_template', path: "../props_template" gem 'standard' gem 'capybara' gem 'minitest' diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 0856edf..820f5bb 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -38,9 +38,7 @@ def broadcast_replace_to(*streamables, **opts) # end def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) - broadcast_stream_to(*streamables, content: superglue_stream_action_tag( - action, target: target, targets: targets, template: render_broadcast_action(rendering), **attributes - )) + broadcast_stream_to(*streamables, content: render_broadcast_action(rendering)) end # def broadcast_replace_later_to(*streamables, **opts) @@ -111,6 +109,7 @@ def broadcast_stream_to(*streamables, content:) private def render_format(format, **rendering) + rendering[:layout] = "superglue/layouts/fragment" ApplicationController.render(formats: [format], **rendering) end @@ -122,7 +121,7 @@ def render_broadcast_action(rendering) if render == false nil else - content || html || (render_format(:html, **rendering) if rendering.present?) + content || html || (render_format(:json, **rendering) if rendering.present?) end end end diff --git a/app/views/superglue/layouts/_fragment.json.props b/app/views/superglue/layouts/_fragment.json.props new file mode 100644 index 0000000..0f560e1 --- /dev/null +++ b/app/views/superglue/layouts/_fragment.json.props @@ -0,0 +1,3 @@ +json.data do + yield +end diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 557fc01..64cb63d 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -10,6 +10,11 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase Superglue::StreamsChannel.verified_stream_name(Superglue::StreamsChannel.signed_stream_name('stream')) end + + def render_message(options) + ApplicationController.render(formats: [:json], **options) + end + # test 'broadcasting remove now' do # assert_broadcast_on 'stream', turbo_stream_action_tag('remove', target: 'message_1') do # Superglue::StreamsChannel.broadcast_remove_to 'stream', target: 'message_1' @@ -27,9 +32,9 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase # end test 'broadcasting replace now' do - options = { partial: 'messages/message', locals: { message: 'hello!' } } + options = { partial: 'messages/message', locals: { message: 'hello!' }, layout: "superglue/layouts/fragment" } - assert_broadcast_on 'stream', turbo_stream_action_tag('replace', target: 'message_1', template: render(options)) do + assert_broadcast_on 'stream', render_message(options) do Superglue::StreamsChannel.broadcast_replace_to 'stream', target: 'message_1', **options end diff --git a/test/test_helper.rb b/test/test_helper.rb index def2ae1..500599d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,5 @@ # Configure Rails Environment ENV["RAILS_ENV"] = "test" - require_relative "../test/dummy/config/environment" # ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)] require "rails/test_help" @@ -26,3 +25,4 @@ class ActionDispatch::IntegrationTest class ActionCable::Channel::TestCase include ActionViewTestCaseExtensions end + From 0aa793001d78eb434b9aed10dd2e8b574e99da5d Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Thu, 8 May 2025 22:40:48 -0400 Subject: [PATCH 07/33] linter --- app/channels/superglue/streams/broadcasts.rb | 8 ++++- .../superglue/layouts/_fragment.json.props | 8 +++++ test/streams/streams_channel_test.rb | 35 +++++++++++-------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 820f5bb..70cc01c 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -38,6 +38,12 @@ def broadcast_replace_to(*streamables, **opts) # end def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) + locals = rendering[:locals] || {} + locals[:broadcast_targets] = Array(target || targets) + locals[:broadcast_action] = action + locals[:broadcast_attributes] = attributes + rendering[:locals] = locals + broadcast_stream_to(*streamables, content: render_broadcast_action(rendering)) end @@ -109,7 +115,7 @@ def broadcast_stream_to(*streamables, content:) private def render_format(format, **rendering) - rendering[:layout] = "superglue/layouts/fragment" + rendering[:layout] = 'superglue/layouts/fragment' ApplicationController.render(formats: [format], **rendering) end diff --git a/app/views/superglue/layouts/_fragment.json.props b/app/views/superglue/layouts/_fragment.json.props index 0f560e1..ecb16a2 100644 --- a/app/views/superglue/layouts/_fragment.json.props +++ b/app/views/superglue/layouts/_fragment.json.props @@ -1,3 +1,11 @@ +json.disable_deferments! + json.data do yield end + +json.fragments json.fragments! +json.type "message" +json.action broadcast_action +json.targets broadcast_targets +json.attributes broadcast_attributes diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 64cb63d..f771aff 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -1,16 +1,15 @@ -require 'test_helper' -require 'action_cable' +require "test_helper" +require "action_cable" class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase # include Superglue::Streams::ActionHelper include ActiveJob::TestHelper - test 'verified stream name' do - assert_equal 'stream', - Superglue::StreamsChannel.verified_stream_name(Superglue::StreamsChannel.signed_stream_name('stream')) + test "verified stream name" do + assert_equal "stream", + Superglue::StreamsChannel.verified_stream_name(Superglue::StreamsChannel.signed_stream_name("stream")) end - def render_message(options) ApplicationController.render(formats: [:json], **options) end @@ -31,16 +30,22 @@ def render_message(options) # end # end - test 'broadcasting replace now' do - options = { partial: 'messages/message', locals: { message: 'hello!' }, layout: "superglue/layouts/fragment" } - - assert_broadcast_on 'stream', render_message(options) do - Superglue::StreamsChannel.broadcast_replace_to 'stream', target: 'message_1', **options + test "broadcasting replace now" do + locals = {message: "hello!"} + options = {partial: "messages/message", locals: locals} + + expected_options = options.clone.merge( + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: ["message_1"], + broadcast_action: "replace", + broadcast_attributes: {} + }) + ) + + assert_broadcast_on "stream", render_message(expected_options) do + Superglue::StreamsChannel.broadcast_replace_to "stream", target: "message_1", **options end - - # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', targets: '.message', template: render(options)) do - # Superglue::StreamsChannel.broadcast_replace_to 'stream', targets: '.message', **options - # end end # test 'broadcasting update now' do From 28eaad1d07f32753d2d302cbefb0cb3f685c6366 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Thu, 8 May 2025 23:30:43 -0400 Subject: [PATCH 08/33] renaming --- app/channels/superglue/streams/broadcasts.rb | 22 +++++----- .../superglue/layouts/_fragment.json.props | 2 +- test/streams/streams_channel_test.rb | 42 ++++++++++++------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 70cc01c..967062e 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -25,9 +25,9 @@ def broadcast_replace_to(*streamables, **opts) # broadcast_action_to(*streamables, action: :after, **opts) # end - # def broadcast_append_to(*streamables, **opts) - # broadcast_action_to(*streamables, action: :append, **opts) - # end + def broadcast_append_to(*streamables, **opts) + broadcast_action_to(*streamables, action: :append, **opts) + end # def broadcast_prepend_to(*streamables, **opts) # broadcast_action_to(*streamables, action: :prepend, **opts) @@ -37,11 +37,11 @@ def broadcast_replace_to(*streamables, **opts) # broadcast_stream_to(*streamables, content: superglue_stream_refresh_tag) # end - def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) + def broadcast_action_to(*streamables, action:, target: nil, targets: nil, options: {}, **rendering) locals = rendering[:locals] || {} locals[:broadcast_targets] = Array(target || targets) locals[:broadcast_action] = action - locals[:broadcast_attributes] = attributes + locals[:broadcast_options] = options rendering[:locals] = locals broadcast_stream_to(*streamables, content: render_broadcast_action(rendering)) @@ -79,7 +79,7 @@ def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attrib # end # end - # def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, attributes: {}, **rendering) + # def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, options: {}, **rendering) # streamables.flatten! # streamables.compact_blank! @@ -88,7 +88,7 @@ def broadcast_action_to(*streamables, action:, target: nil, targets: nil, attrib # target = convert_to_superglue_stream_dom_id(target) # targets = convert_to_superglue_stream_dom_id(targets, include_selector: true) # Superglue::Streams::ActionBroadcastJob.perform_later \ - # stream_name_from(streamables), action: action, target: target, targets: targets, attributes: attributes, **rendering + # stream_name_from(streamables), action: action, target: target, targets: targets, options: options, **rendering # end # def broadcast_render_to(*streamables, **rendering) @@ -115,14 +115,14 @@ def broadcast_stream_to(*streamables, content:) private def render_format(format, **rendering) - rendering[:layout] = 'superglue/layouts/fragment' + rendering[:layout] = "superglue/layouts/fragment" ApplicationController.render(formats: [format], **rendering) end def render_broadcast_action(rendering) - content = rendering.delete(:content) - html = rendering.delete(:html) - render = rendering.delete(:render) + content = rendering.delete(:content) # i should remove content + html = rendering.delete(:html) # i should add json and stringify it + render = rendering.delete(:render) if render == false nil diff --git a/app/views/superglue/layouts/_fragment.json.props b/app/views/superglue/layouts/_fragment.json.props index ecb16a2..4ad42ae 100644 --- a/app/views/superglue/layouts/_fragment.json.props +++ b/app/views/superglue/layouts/_fragment.json.props @@ -8,4 +8,4 @@ json.fragments json.fragments! json.type "message" json.action broadcast_action json.targets broadcast_targets -json.attributes broadcast_attributes +json.attributes broadcast_options diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index f771aff..29c2141 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -32,19 +32,23 @@ def render_message(options) test "broadcasting replace now" do locals = {message: "hello!"} - options = {partial: "messages/message", locals: locals} + rendering = {partial: "messages/message", locals: locals} - expected_options = options.clone.merge( + expected = rendering.clone.merge( layout: "superglue/layouts/fragment", locals: locals.merge({ broadcast_targets: ["message_1"], broadcast_action: "replace", - broadcast_attributes: {} + broadcast_options: {} }) ) - assert_broadcast_on "stream", render_message(expected_options) do - Superglue::StreamsChannel.broadcast_replace_to "stream", target: "message_1", **options + assert_broadcast_on "stream", render_message(expected) do + Superglue::StreamsChannel.broadcast_replace_to "stream", target: "message_1", **rendering + end + + assert_broadcast_on "stream", render_message(expected) do + Superglue::StreamsChannel.broadcast_replace_to "stream", targets: ["message_1"], **rendering end end @@ -60,17 +64,27 @@ def render_message(options) # end # end - # test 'broadcasting append now' do - # options = { partial: 'messages/message', locals: { message: 'hello!' } } + test "broadcasting append now" do + locals = {message: "hello!"} + rendering = {partial: "messages/message", locals: locals} - # assert_broadcast_on 'stream', turbo_stream_action_tag('append', target: 'messages', template: render(options)) do - # Superglue::StreamsChannel.broadcast_append_to 'stream', target: 'messages', **options - # end + expected = rendering.clone.merge( + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: ["messages"], + broadcast_action: "append", + broadcast_options: {} + }) + ) - # assert_broadcast_on 'stream', turbo_stream_action_tag('append', targets: '.message', template: render(options)) do - # Superglue::StreamsChannel.broadcast_append_to 'stream', targets: '.message', **options - # end - # end + assert_broadcast_on "stream", render_message(expected) do + Superglue::StreamsChannel.broadcast_append_to "stream", target: "messages", **rendering + end + + assert_broadcast_on "stream", render_message(expected) do + Superglue::StreamsChannel.broadcast_append_to "stream", targets: ["messages"], **rendering + end + end # test 'broadcasting append now with empty template' do # assert_broadcast_on 'stream', From 673c089db0e518cf495e93f02f2d2629b528d642 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 9 May 2025 14:56:38 -0400 Subject: [PATCH 09/33] wip --- test/streams/streams_channel_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 29c2141..dafbe9e 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -34,7 +34,7 @@ def render_message(options) locals = {message: "hello!"} rendering = {partial: "messages/message", locals: locals} - expected = rendering.clone.merge( + expected = rendering.merge( layout: "superglue/layouts/fragment", locals: locals.merge({ broadcast_targets: ["message_1"], @@ -68,7 +68,7 @@ def render_message(options) locals = {message: "hello!"} rendering = {partial: "messages/message", locals: locals} - expected = rendering.clone.merge( + expected = rendering.merge( layout: "superglue/layouts/fragment", locals: locals.merge({ broadcast_targets: ["messages"], From 15fd99dd470e49ec702b4978e5aee0a23fc5bf32 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 9 May 2025 15:00:12 -0400 Subject: [PATCH 10/33] addprepend --- test/streams/streams_channel_test.rb | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index dafbe9e..0e1e217 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -93,17 +93,27 @@ def render_message(options) # end # end - # test 'broadcasting prepend now' do - # options = { partial: 'messages/message', locals: { message: 'hello!' } } + test "broadcasting prepend now" do + locals = {message: "hello!"} + rendering = {partial: "messages/message", locals: locals} - # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', target: 'messages', template: render(options)) do - # Superglue::StreamsChannel.broadcast_prepend_to 'stream', target: 'messages', **options - # end + expected = rendering.merge( + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: ["messages"], + broadcast_action: "prepend", + broadcast_options: {} + }) + ) - # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', targets: '.message', template: render(options)) do - # Superglue::StreamsChannel.broadcast_prepend_to 'stream', targets: '.message', **options - # end - # end + assert_broadcast_on "stream", render_message(expected) do + Superglue::StreamsChannel.broadcast_prepend_to "stream", target: "messages", **rendering + end + + assert_broadcast_on "stream", render_message(expected) do + Superglue::StreamsChannel.broadcast_prepend_to "stream", targets: ["messages"], **rendering + end + end # test 'broadcasting action now' do # options = { partial: 'messages/message', locals: { message: 'hello!' } } From f9c006a672ec845830f6caf38525caef2f5adbe0 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 9 May 2025 15:28:52 -0400 Subject: [PATCH 11/33] Add jobs --- app/channels/superglue/streams/broadcasts.rb | 30 +++++++-------- .../superglue/streams/action_broadcast_job.rb | 8 ++++ test/streams/streams_channel_test.rb | 38 +++++++++++-------- 3 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 app/jobs/superglue/streams/action_broadcast_job.rb diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 967062e..69befb5 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -29,9 +29,9 @@ def broadcast_append_to(*streamables, **opts) broadcast_action_to(*streamables, action: :append, **opts) end - # def broadcast_prepend_to(*streamables, **opts) - # broadcast_action_to(*streamables, action: :prepend, **opts) - # end + def broadcast_prepend_to(*streamables, **opts) + broadcast_action_to(*streamables, action: :prepend, **opts) + end # def broadcast_refresh_to(*streamables, **opts) # broadcast_stream_to(*streamables, content: superglue_stream_refresh_tag) @@ -47,9 +47,9 @@ def broadcast_action_to(*streamables, action:, target: nil, targets: nil, option broadcast_stream_to(*streamables, content: render_broadcast_action(rendering)) end - # def broadcast_replace_later_to(*streamables, **opts) - # broadcast_action_later_to(*streamables, action: :replace, **opts) - # end + def broadcast_replace_later_to(*streamables, **opts) + broadcast_action_later_to(*streamables, action: :replace, **opts) + end # def broadcast_update_later_to(*streamables, **opts) # broadcast_action_later_to(*streamables, action: :update, **opts) @@ -79,17 +79,17 @@ def broadcast_action_to(*streamables, action:, target: nil, targets: nil, option # end # end - # def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, options: {}, **rendering) - # streamables.flatten! - # streamables.compact_blank! + def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, options: {}, **rendering) + streamables.flatten! + streamables.compact_blank! - # return unless streamables.present? + return unless streamables.present? - # target = convert_to_superglue_stream_dom_id(target) - # targets = convert_to_superglue_stream_dom_id(targets, include_selector: true) - # Superglue::Streams::ActionBroadcastJob.perform_later \ - # stream_name_from(streamables), action: action, target: target, targets: targets, options: options, **rendering - # end + # target = convert_to_superglue_stream_dom_id(target) + # targets = convert_to_superglue_stream_dom_id(targets, include_selector: true) + Superglue::Streams::ActionBroadcastJob.perform_later \ + stream_name_from(streamables), action: action, target: target, targets: targets, options: options, **rendering + end # def broadcast_render_to(*streamables, **rendering) # broadcast_stream_to(*streamables, content: render_format(:superglue_stream, **rendering)) diff --git a/app/jobs/superglue/streams/action_broadcast_job.rb b/app/jobs/superglue/streams/action_broadcast_job.rb new file mode 100644 index 0000000..99b4bcc --- /dev/null +++ b/app/jobs/superglue/streams/action_broadcast_job.rb @@ -0,0 +1,8 @@ +# The job that powers all the broadcast_$action_later broadcasts available in Turbo::Streams::Broadcasts. +class Superglue::Streams::ActionBroadcastJob < ActiveJob::Base + discard_on ActiveJob::DeserializationError + + def perform(stream, action:, target:, options: {}, **rendering) + Superglue::StreamsChannel.broadcast_action_to stream, action: action, target: target, options: options, **rendering + end +end diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 0e1e217..afc31f2 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -139,23 +139,31 @@ def render_message(options) # end # end - # test 'broadcasting replace later' do - # options = { partial: 'messages/message', locals: { message: 'hello!' } } + test "broadcasting replace later" do + locals = {message: "hello!"} + rendering = {partial: "messages/message", locals: locals} - # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', target: 'message_1', template: render(options)) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_replace_later_to \ - # 'stream', target: 'message_1', **options - # end - # end + expected = rendering.merge( + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: ["message_1"], + broadcast_action: "replace", + broadcast_options: {} + }) + ) - # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', targets: '.message', template: render(options)) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_replace_later_to \ - # 'stream', targets: '.message', **options - # end - # end - # end + assert_broadcast_on "stream", render_message(expected) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_replace_later_to "stream", target: "message_1", **rendering + end + end + + assert_broadcast_on "stream", render_message(expected) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_replace_later_to "stream", targets: ["message_1"], **rendering + end + end + end # test 'broadcasting update later' do # options = { partial: 'messages/message', locals: { message: 'hello!' } } From e94a0bc28d3927254b3361b631f5dcee2183dc9f Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 9 May 2025 15:52:24 -0400 Subject: [PATCH 12/33] other jobs --- app/channels/superglue/streams/broadcasts.rb | 12 ++-- test/streams/streams_channel_test.rb | 76 ++++++++++++-------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 69befb5..b077857 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -63,13 +63,13 @@ def broadcast_replace_later_to(*streamables, **opts) # broadcast_action_later_to(*streamables, action: :after, **opts) # end - # def broadcast_append_later_to(*streamables, **opts) - # broadcast_action_later_to(*streamables, action: :append, **opts) - # end + def broadcast_append_later_to(*streamables, **opts) + broadcast_action_later_to(*streamables, action: :append, **opts) + end - # def broadcast_prepend_later_to(*streamables, **opts) - # broadcast_action_later_to(*streamables, action: :prepend, **opts) - # end + def broadcast_prepend_later_to(*streamables, **opts) + broadcast_action_later_to(*streamables, action: :prepend, **opts) + end # def broadcast_refresh_later_to(*streamables, request_id: Superglue.current_request_id, **opts) # stream_name = stream_name_from(streamables) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index afc31f2..441444b 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -183,41 +183,57 @@ def render_message(options) # end # end - # test 'broadcasting append later' do - # options = { partial: 'messages/message', locals: { message: 'hello!' } } + test "broadcasting append later" do + locals = {message: "hello!"} + rendering = {partial: "messages/message", locals: locals} - # assert_broadcast_on 'stream', turbo_stream_action_tag('append', target: 'messages', template: render(options)) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_append_later_to \ - # 'stream', target: 'messages', **options - # end - # end + expected = rendering.merge( + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: ["messages"], + broadcast_action: "append", + broadcast_options: {} + }) + ) - # assert_broadcast_on 'stream', turbo_stream_action_tag('append', targets: '.message', template: render(options)) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_append_later_to \ - # 'stream', targets: '.message', **options - # end - # end - # end + assert_broadcast_on "stream", render_message(expected) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_append_later_to "stream", target: "messages", **rendering + end + end - # test 'broadcasting prepend later' do - # options = { partial: 'messages/message', locals: { message: 'hello!' } } + assert_broadcast_on "stream", render_message(expected) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_append_later_to "stream", targets: ["messages"], **rendering + end + end + end - # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', target: 'messages', template: render(options)) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_prepend_later_to \ - # 'stream', target: 'messages', **options - # end - # end + test "broadcasting prepend later" do + locals = {message: "hello!"} + rendering = {partial: "messages/message", locals: locals} - # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', targets: '.message', template: render(options)) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_prepend_later_to \ - # 'stream', targets: '.message', **options - # end - # end - # end + expected = rendering.merge( + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: ["messages"], + broadcast_action: "prepend", + broadcast_options: {} + }) + ) + + assert_broadcast_on "stream", render_message(expected) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_prepend_later_to "stream", target: "messages", **rendering + end + end + + assert_broadcast_on "stream", render_message(expected) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_prepend_later_to "stream", targets: ["messages"], **rendering + end + end + end # test 'broadcasting refresh later' do # assert_broadcast_on 'stream', turbo_stream_refresh_tag do From 3e4291f23987ad488a15b1af31f341d622304614 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 9 May 2025 21:44:02 -0400 Subject: [PATCH 13/33] refresh --- app/channels/superglue/streams/broadcasts.rb | 29 ++++++---- .../superglue/streams/broadcast_stream_job.rb | 7 +++ app/models/superglue/debouncer.rb | 25 +++++++++ app/models/superglue/thread_debouncer.rb | 29 ++++++++++ .../superglue/layouts/_fragment.json.props | 2 +- test/streams/streams_channel_test.rb | 54 ++++++++++++------- 6 files changed, 114 insertions(+), 32 deletions(-) create mode 100644 app/jobs/superglue/streams/broadcast_stream_job.rb create mode 100644 app/models/superglue/debouncer.rb create mode 100644 app/models/superglue/thread_debouncer.rb diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index b077857..50f1253 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -63,6 +63,8 @@ def broadcast_replace_later_to(*streamables, **opts) # broadcast_action_later_to(*streamables, action: :after, **opts) # end + ### convert_to_turbo_stream_dom_id ican use this as the fragment name! + def broadcast_append_later_to(*streamables, **opts) broadcast_action_later_to(*streamables, action: :append, **opts) end @@ -71,13 +73,20 @@ def broadcast_prepend_later_to(*streamables, **opts) broadcast_action_later_to(*streamables, action: :prepend, **opts) end - # def broadcast_refresh_later_to(*streamables, request_id: Superglue.current_request_id, **opts) - # stream_name = stream_name_from(streamables) + def broadcast_refresh_later_to(*streamables, request_id: Superglue.current_request_id, **opts) + stream_name = stream_name_from(streamables) - # refresh_debouncer_for(*streamables, request_id: request_id).debounce do - # Superglue::Streams::BroadcastStreamJob.perform_later stream_name, content: superglue_stream_refresh_tag(request_id: request_id, **opts).to_str # Sidekiq requires job arguments to be valid JSON types, such as String - # end - # end + refresh_debouncer_for(*streamables, request_id: request_id).debounce do + content = JSON.generate({ + type: "message", + action: "refresh", + requestId: request_id, + options: opts + }) + + Superglue::Streams::BroadcastStreamJob.perform_later stream_name, content: content + end + end def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, options: {}, **rendering) streamables.flatten! @@ -85,8 +94,6 @@ def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, return unless streamables.present? - # target = convert_to_superglue_stream_dom_id(target) - # targets = convert_to_superglue_stream_dom_id(targets, include_selector: true) Superglue::Streams::ActionBroadcastJob.perform_later \ stream_name_from(streamables), action: action, target: target, targets: targets, options: options, **rendering end @@ -108,9 +115,9 @@ def broadcast_stream_to(*streamables, content:) ActionCable.server.broadcast stream_name_from(streamables), content end - # def refresh_debouncer_for(*streamables, request_id: nil) # :nodoc: - # Superglue::ThreadDebouncer.for("superglue-refresh-debouncer-#{stream_name_from(streamables.including(request_id))}") - # end + def refresh_debouncer_for(*streamables, request_id: nil) # :nodoc: + Superglue::ThreadDebouncer.for("superglue-refresh-debouncer-#{stream_name_from(streamables.including(request_id))}") + end private diff --git a/app/jobs/superglue/streams/broadcast_stream_job.rb b/app/jobs/superglue/streams/broadcast_stream_job.rb new file mode 100644 index 0000000..1d0cbb0 --- /dev/null +++ b/app/jobs/superglue/streams/broadcast_stream_job.rb @@ -0,0 +1,7 @@ +class Superglue::Streams::BroadcastStreamJob < ActiveJob::Base + discard_on ActiveJob::DeserializationError + + def perform(stream, content:) + Superglue::StreamsChannel.broadcast_stream_to(stream, content: content) + end +end diff --git a/app/models/superglue/debouncer.rb b/app/models/superglue/debouncer.rb new file mode 100644 index 0000000..40dfc1b --- /dev/null +++ b/app/models/superglue/debouncer.rb @@ -0,0 +1,25 @@ +class Superglue::Debouncer + attr_reader :delay, :scheduled_task + + DEFAULT_DELAY = 0.5 + + def initialize(delay: DEFAULT_DELAY) + @delay = delay + @scheduled_task = nil + end + + def debounce(&block) + scheduled_task&.cancel unless scheduled_task&.complete? + @scheduled_task = Concurrent::ScheduledTask.execute(delay, &block) + end + + def wait + scheduled_task&.wait(wait_timeout) + end + + private + + def wait_timeout + delay + 1 + end +end diff --git a/app/models/superglue/thread_debouncer.rb b/app/models/superglue/thread_debouncer.rb new file mode 100644 index 0000000..f1e6abf --- /dev/null +++ b/app/models/superglue/thread_debouncer.rb @@ -0,0 +1,29 @@ +# A decorated debouncer that will store instances in the current thread clearing them +# after the debounced logic triggers. +class Superglue::ThreadDebouncer + delegate :wait, to: :debouncer + + def self.for(key, delay: Superglue::Debouncer::DEFAULT_DELAY) + Thread.current[key] ||= new(key, Thread.current, delay: delay) + end + + private_class_method :new + + def initialize(key, thread, delay:) + @key = key + @debouncer = Superglue::Debouncer.new(delay: delay) + @thread = thread + end + + def debounce + debouncer.debounce do + yield.tap do + thread[key] = nil + end + end + end + + private + + attr_reader :key, :debouncer, :thread +end diff --git a/app/views/superglue/layouts/_fragment.json.props b/app/views/superglue/layouts/_fragment.json.props index 4ad42ae..6761cb8 100644 --- a/app/views/superglue/layouts/_fragment.json.props +++ b/app/views/superglue/layouts/_fragment.json.props @@ -8,4 +8,4 @@ json.fragments json.fragments! json.type "message" json.action broadcast_action json.targets broadcast_targets -json.attributes broadcast_options +json.options broadcast_options diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 441444b..7636eca 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -235,30 +235,44 @@ def render_message(options) end end - # test 'broadcasting refresh later' do - # assert_broadcast_on 'stream', turbo_stream_refresh_tag do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' - # Superglue::StreamsChannel.refresh_debouncer_for('stream').wait - # end - # end + test "broadcasting refresh later" do + content = { + type: "message", + action: "refresh", + requestId: nil, + options: {} + } + + assert_broadcast_on "stream", JSON.generate(content) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_refresh_later_to "stream" + Superglue::StreamsChannel.refresh_debouncer_for("stream").wait + end + end - # Turbo.current_request_id = '123' - # assert_broadcast_on 'stream', turbo_stream_refresh_tag(request_id: '123') do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' - # Superglue::StreamsChannel.refresh_debouncer_for('stream', request_id: '123').wait - # end - # end - # end + content = { + type: "message", + action: "refresh", + requestId: "123", + options: {} + } + + Superglue.current_request_id = "123" + assert_broadcast_on "stream", JSON.generate(content) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_refresh_later_to "stream" + Superglue::StreamsChannel.refresh_debouncer_for("stream", request_id: "123").wait + end + end + end - # test 'broadcasting refresh later is debounced' do - # assert_broadcast_on 'stream', turbo_stream_refresh_tag do - # assert_broadcasts('stream', 1) do + # test "broadcasting refresh later is debounced" do + # assert_broadcast_on "stream", "hi" do + # assert_broadcasts("stream", 1) do # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' + # Superglue::StreamsChannel.broadcast_refresh_later_to "stream" - # Superglue::StreamsChannel.refresh_debouncer_for('stream').wait + # Superglue::StreamsChannel.refresh_debouncer_for("stream").wait # end # end # end From fb8a524a38429c7adcb7f5bf1e8ec32cd42c3e89 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 9 May 2025 22:15:36 -0400 Subject: [PATCH 14/33] continue --- test/streams/streams_channel_test.rb | 27 +++++++++++++++++---------- test/test_helper.rb | 3 +-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 7636eca..24c3c79 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -266,17 +266,24 @@ def render_message(options) end end - # test "broadcasting refresh later is debounced" do - # assert_broadcast_on "stream", "hi" do - # assert_broadcasts("stream", 1) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_refresh_later_to "stream" + test "broadcasting refresh later is debounced" do + content = { + type: "message", + action: "refresh", + requestId: nil, + options: {} + } - # Superglue::StreamsChannel.refresh_debouncer_for("stream").wait - # end - # end - # end - # end + assert_broadcast_on "stream", JSON.generate(content) do + assert_broadcasts("stream", 1) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_refresh_later_to "stream" + + Superglue::StreamsChannel.refresh_debouncer_for("stream").wait + end + end + end + end # test 'broadcasting refresh later is debounced considering the current request id' do # assert_broadcasts('stream', 2) do diff --git a/test/test_helper.rb b/test/test_helper.rb index 500599d..df2eea6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,7 +14,7 @@ class ActiveSupport::TestCase include ActiveJob::TestHelper setup do - # Turbo.current_request_id = nil + Superglue.current_request_id = nil end end @@ -25,4 +25,3 @@ class ActionDispatch::IntegrationTest class ActionCable::Channel::TestCase include ActionViewTestCaseExtensions end - From 3919cd2f58e28ac0b181996b71faa4fde63fe74e Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 9 May 2025 22:17:34 -0400 Subject: [PATCH 15/33] wip --- test/streams/streams_channel_test.rb | 43 ++++++++++++++++------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 24c3c79..5b84cce 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -285,24 +285,31 @@ def render_message(options) end end - # test 'broadcasting refresh later is debounced considering the current request id' do - # assert_broadcasts('stream', 2) do - # perform_enqueued_jobs do - # assert_broadcast_on 'stream', turbo_stream_refresh_tag("request-id": '123') do - # assert_broadcast_on 'stream', turbo_stream_refresh_tag("request-id": '456') do - # Turbo.current_request_id = '123' - # 3.times { Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' } - - # Turbo.current_request_id = '456' - # 3.times { Superglue::StreamsChannel.broadcast_refresh_later_to 'stream' } - - # Superglue::StreamsChannel.refresh_debouncer_for('stream', request_id: '123').wait - # Superglue::StreamsChannel.refresh_debouncer_for('stream', request_id: '456').wait - # end - # end - # end - # end - # end + test "broadcasting refresh later is debounced considering the current request id" do + content = { + type: "message", + action: "refresh", + requestId: "123", + options: {} + } + assert_broadcasts("stream", 2) do + perform_enqueued_jobs do + assert_broadcast_on "stream", JSON.generate(content) do + content[:requestId] = "456" + assert_broadcast_on "stream", JSON.generate(content) do + Superglue.current_request_id = "123" + 3.times { Superglue::StreamsChannel.broadcast_refresh_later_to "stream" } + + Superglue.current_request_id = "456" + 3.times { Superglue::StreamsChannel.broadcast_refresh_later_to "stream" } + + Superglue::StreamsChannel.refresh_debouncer_for("stream", request_id: "123").wait + Superglue::StreamsChannel.refresh_debouncer_for("stream", request_id: "456").wait + end + end + end + end + end # test 'broadcasting action later' do # options = { partial: 'messages/message', locals: { message: 'hello!' } } From fad8e0880ea1a97de1b4e4db555f46d3e381b990 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 9 May 2025 22:52:48 -0400 Subject: [PATCH 16/33] wip --- test/streams/streams_channel_test.rb | 48 +++++++++++++++++----------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 5b84cce..0699d54 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -115,29 +115,39 @@ def render_message(options) end end - # test 'broadcasting action now' do - # options = { partial: 'messages/message', locals: { message: 'hello!' } } + test "broadcasting action now" do + locals = {message: "hello!"} + rendering = {partial: "messages/message", locals: locals} - # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', target: 'messages', template: render(options)) do - # Superglue::StreamsChannel.broadcast_action_to 'stream', action: 'prepend', target: 'messages', **options - # end + expected = rendering.merge( + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: ["messages"], + broadcast_action: "prepend", + broadcast_options: {} + }) + ) - # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', targets: '.message', template: render(options)) do - # Superglue::StreamsChannel.broadcast_action_to 'stream', action: 'prepend', targets: '.message', **options - # end + assert_broadcast_on "stream", render_message(expected) do + Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", target: "messages", **rendering + end - # assert_broadcast_on 'stream', - # turbo_stream_action_tag('prepend', targets: '.message', template: 'test') do - # Superglue::StreamsChannel.broadcast_action_to 'stream', action: 'prepend', targets: '.message', - # content: 'test' - # end + assert_broadcast_on "stream", render_message(expected) do + Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: "messages", **rendering + end - # assert_broadcast_on 'stream', - # turbo_stream_action_tag('prepend', targets: '.message', template: 'test') do - # Superglue::StreamsChannel.broadcast_action_to 'stream', action: 'prepend', targets: '.message', - # html: 'test' - # end - # end + # assert_broadcast_on "stream", + # turbo_stream_action_tag("prepend", targets: ".message", template: "test") do + # Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ".message", + # content: "test" + # end + + # assert_broadcast_on "stream", + # turbo_stream_action_tag("prepend", targets: ".message", template: "test") do + # Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ".message", + # html: "test" + # end + end test "broadcasting replace later" do locals = {message: "hello!"} From 3c854edda34ea70623e15d9d123cd4d33967bde9 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 9 May 2025 23:51:00 -0400 Subject: [PATCH 17/33] Request ids --- .../concerns/superglue/request_id_tracking.rb | 13 +++++++++++++ lib/superglue/engine.rb | 7 ++++--- .../dummy/app/controllers/request_ids_controller.rb | 5 +++++ test/dummy/config/routes.rb | 3 ++- test/refreshes/request_id_tracking_test.rb | 8 ++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 app/controllers/concerns/superglue/request_id_tracking.rb create mode 100644 test/dummy/app/controllers/request_ids_controller.rb create mode 100644 test/refreshes/request_id_tracking_test.rb diff --git a/app/controllers/concerns/superglue/request_id_tracking.rb b/app/controllers/concerns/superglue/request_id_tracking.rb new file mode 100644 index 0000000..0b94446 --- /dev/null +++ b/app/controllers/concerns/superglue/request_id_tracking.rb @@ -0,0 +1,13 @@ +module Superglue::RequestIdTracking + extend ActiveSupport::Concern + + included do + around_action :superglue_tracking_request_id + end + + private + + def superglue_tracking_request_id(&block) + Superglue.with_request_id(request.headers["X-Superglue-Request-Id"], &block) + end +end diff --git a/lib/superglue/engine.rb b/lib/superglue/engine.rb index 1de2a72..b6ebdb3 100644 --- a/lib/superglue/engine.rb +++ b/lib/superglue/engine.rb @@ -20,17 +20,18 @@ class Engine < ::Rails::Engine next if self != ActionController::Base include Controller + include Superglue::RequestIdTracking prepend_view_path( - Superglue::Resolver.new(Rails.root.join('app/views')) + Superglue::Resolver.new(Rails.root.join("app/views")) ) end end - initializer 'superglue.signed_stream_verifier_key' do + initializer "superglue.signed_stream_verifier_key" do config.after_initialize do Superglue.signed_stream_verifier_key = config.superglue.signed_stream_verifier_key || - Rails.application.key_generator.generate_key('superglue/signed_stream_verifier_key') + Rails.application.key_generator.generate_key("superglue/signed_stream_verifier_key") end end end diff --git a/test/dummy/app/controllers/request_ids_controller.rb b/test/dummy/app/controllers/request_ids_controller.rb new file mode 100644 index 0000000..0add059 --- /dev/null +++ b/test/dummy/app/controllers/request_ids_controller.rb @@ -0,0 +1,5 @@ +class RequestIdsController < ApplicationController + def show + render json: {request_id: Superglue.current_request_id} + end +end diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index a4c09dd..7fa9d1e 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -3,7 +3,7 @@ # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. # Can be used by load balancers and uptime monitors to verify that the app is live. - get "up" => "rails/health#show", as: :rails_health_check + get "up" => "rails/health#show", :as => :rails_health_check # Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb) # get "manifest" => "rails/pwa#manifest", as: :pwa_manifest @@ -12,4 +12,5 @@ # Defines the root path route ("/") # root "posts#index" get ":controller(/:action)" + resource :request_id end diff --git a/test/refreshes/request_id_tracking_test.rb b/test/refreshes/request_id_tracking_test.rb new file mode 100644 index 0000000..9262342 --- /dev/null +++ b/test/refreshes/request_id_tracking_test.rb @@ -0,0 +1,8 @@ +require "test_helper" + +class Superglue::RequestIdTrackingTest < ActionDispatch::IntegrationTest + test "set the current turbo request id from the value in the X-Turbo-Request-Id header" do + get request_id_path, headers: {"X-Superglue-Request-Id" => "123"} + assert_equal "123", JSON.parse(response.body)["request_id"] + end +end From 69ec7dd1b862d97a8be6f64545d51180ecf3190d Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Sat, 10 May 2025 10:38:55 -0400 Subject: [PATCH 18/33] wip --- test/streams/streams_channel_test.rb | 40 +++++++++++++++++----------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 0699d54..899ce07 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -321,23 +321,33 @@ def render_message(options) end end - # test 'broadcasting action later' do - # options = { partial: 'messages/message', locals: { message: 'hello!' } } + test "broadcasting action later" do + locals = {message: "hello!"} + rendering = {partial: "messages/message", locals: locals} - # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', target: 'messages', template: render(options)) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_action_later_to \ - # 'stream', action: 'prepend', target: 'messages', **options - # end - # end + expected = rendering.merge( + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: ["messages"], + broadcast_action: "prepend", + broadcast_options: {} + }) + ) - # assert_broadcast_on 'stream', turbo_stream_action_tag('prepend', targets: '.message', template: render(options)) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_action_later_to \ - # 'stream', action: 'prepend', targets: '.message', **options - # end - # end - # end + assert_broadcast_on "stream", render_message(expected) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_action_later_to \ + "stream", action: "prepend", target: "messages", **rendering + end + end + + assert_broadcast_on "stream", render_message(expected) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_action_later_to \ + "stream", action: "prepend", targets: "messages", **rendering + end + end + end # test 'broadcasting action later with ActiveModel array target' do # options = { partial: 'messages/message', locals: { message: 'hello!' } } From 0bf5631e7ae5944cb18b543899d4c7d2f0df3bd8 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Sun, 11 May 2025 23:09:39 -0400 Subject: [PATCH 19/33] ensure target --- app/channels/superglue/streams/broadcasts.rb | 20 ++++++++++++++++++- .../superglue/streams/action_helper.rb | 12 +++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 app/helpers/superglue/streams/action_helper.rb diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 50f1253..41b249b 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -39,7 +39,13 @@ def broadcast_prepend_to(*streamables, **opts) def broadcast_action_to(*streamables, action:, target: nil, targets: nil, options: {}, **rendering) locals = rendering[:locals] || {} - locals[:broadcast_targets] = Array(target || targets) + targets = (target ? [target] : targets) + + targets = targets.map do |item| + convert_to_superglue_fragment_id(item) + end + + locals[:broadcast_targets] = targets locals[:broadcast_action] = action locals[:broadcast_options] = options rendering[:locals] = locals @@ -94,6 +100,9 @@ def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, return unless streamables.present? + target = convert_to_turbo_stream_dom_id(target) + targets = convert_to_turbo_stream_dom_id(targets, include_selector: true) + Superglue::Streams::ActionBroadcastJob.perform_later \ stream_name_from(streamables), action: action, target: target, targets: targets, options: options, **rendering end @@ -121,6 +130,15 @@ def refresh_debouncer_for(*streamables, request_id: nil) # :nodoc: private + def convert_to_superglue_fragment_id(target, include_selector: false) + target_array = Array.wrap(target) + if target_array.any? { |value| value.respond_to?(:to_key) } + ActionView::RecordIdentifier.dom_id(*target_array) + else + target + end + end + def render_format(format, **rendering) rendering[:layout] = "superglue/layouts/fragment" ApplicationController.render(formats: [format], **rendering) diff --git a/app/helpers/superglue/streams/action_helper.rb b/app/helpers/superglue/streams/action_helper.rb new file mode 100644 index 0000000..245a514 --- /dev/null +++ b/app/helpers/superglue/streams/action_helper.rb @@ -0,0 +1,12 @@ +module Superglue::Streams::ActionHelper + private + + def convert_to_superglue_fragment_id(target, include_selector: false) + target_array = Array.wrap(target) + if target_array.any? { |value| value.respond_to?(:to_key) } + ActionView::RecordIdentifier.dom_id(*target_array) + else + target + end + end +end From 33924d2517697b0df4ff994a42cb62a610d9ae93 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Sun, 11 May 2025 23:19:49 -0400 Subject: [PATCH 20/33] wip --- app/channels/superglue/streams/broadcasts.rb | 5 +++-- test/streams/streams_channel_test.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 41b249b..d2a5996 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -100,8 +100,9 @@ def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, return unless streamables.present? - target = convert_to_turbo_stream_dom_id(target) - targets = convert_to_turbo_stream_dom_id(targets, include_selector: true) + targets = (target ? [target] : targets).map do |item| + convert_to_superglue_fragment_id(item) + end Superglue::Streams::ActionBroadcastJob.perform_later \ stream_name_from(streamables), action: action, target: target, targets: targets, options: options, **rendering diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 899ce07..0187acd 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -133,7 +133,7 @@ def render_message(options) end assert_broadcast_on "stream", render_message(expected) do - Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: "messages", **rendering + Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ["messages"], **rendering end # assert_broadcast_on "stream", @@ -344,7 +344,7 @@ def render_message(options) assert_broadcast_on "stream", render_message(expected) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_action_later_to \ - "stream", action: "prepend", targets: "messages", **rendering + "stream", action: "prepend", targets: ["messages"], **rendering end end end From e70fe5228aeafb4822d542cdbaa9d649f5c6f845 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Sun, 11 May 2025 23:36:21 -0400 Subject: [PATCH 21/33] Import message --- .gitignore | 1 + test/dummy/app/models/message.rb | 3 ++ .../migrate/20250512032735_create_messages.rb | 9 +++++ test/dummy/db/schema.rb | 19 ++++++++++ test/streams/streams_channel_test.rb | 38 +++++++++---------- test/test_helper.rb | 2 +- 6 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 test/dummy/app/models/message.rb create mode 100644 test/dummy/db/migrate/20250512032735_create_messages.rb create mode 100644 test/dummy/db/schema.rb diff --git a/.gitignore b/.gitignore index f9a7334..fce8cc2 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ breezy/build/**/*.js props_template/performance/**/*.png .tool-versions testapp/ +*.sqlite3 diff --git a/test/dummy/app/models/message.rb b/test/dummy/app/models/message.rb new file mode 100644 index 0000000..ebe948c --- /dev/null +++ b/test/dummy/app/models/message.rb @@ -0,0 +1,3 @@ +class Message < ApplicationRecord + delegate :to_s, to: :content, allow_nil: true +end diff --git a/test/dummy/db/migrate/20250512032735_create_messages.rb b/test/dummy/db/migrate/20250512032735_create_messages.rb new file mode 100644 index 0000000..0fa4cb4 --- /dev/null +++ b/test/dummy/db/migrate/20250512032735_create_messages.rb @@ -0,0 +1,9 @@ +class CreateMessages < ActiveRecord::Migration[7.2] + def change + create_table :messages do |t| + t.text :content + + t.timestamps + end + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb new file mode 100644 index 0000000..724b460 --- /dev/null +++ b/test/dummy/db/schema.rb @@ -0,0 +1,19 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.2].define(version: 2025_05_12_032735) do + create_table "messages", force: :cascade do |t| + t.text "content" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end +end diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 0187acd..ac11ff0 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -10,9 +10,9 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase Superglue::StreamsChannel.verified_stream_name(Superglue::StreamsChannel.signed_stream_name("stream")) end - def render_message(options) - ApplicationController.render(formats: [:json], **options) - end + # def render(options) + # ApplicationController.render(formats: [:json], **options) + # end # test 'broadcasting remove now' do # assert_broadcast_on 'stream', turbo_stream_action_tag('remove', target: 'message_1') do @@ -43,11 +43,11 @@ def render_message(options) }) ) - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do Superglue::StreamsChannel.broadcast_replace_to "stream", target: "message_1", **rendering end - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do Superglue::StreamsChannel.broadcast_replace_to "stream", targets: ["message_1"], **rendering end end @@ -77,11 +77,11 @@ def render_message(options) }) ) - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do Superglue::StreamsChannel.broadcast_append_to "stream", target: "messages", **rendering end - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do Superglue::StreamsChannel.broadcast_append_to "stream", targets: ["messages"], **rendering end end @@ -106,11 +106,11 @@ def render_message(options) }) ) - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do Superglue::StreamsChannel.broadcast_prepend_to "stream", target: "messages", **rendering end - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do Superglue::StreamsChannel.broadcast_prepend_to "stream", targets: ["messages"], **rendering end end @@ -128,11 +128,11 @@ def render_message(options) }) ) - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", target: "messages", **rendering end - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ["messages"], **rendering end @@ -162,13 +162,13 @@ def render_message(options) }) ) - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_replace_later_to "stream", target: "message_1", **rendering end end - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_replace_later_to "stream", targets: ["message_1"], **rendering end @@ -206,13 +206,13 @@ def render_message(options) }) ) - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_append_later_to "stream", target: "messages", **rendering end end - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_append_later_to "stream", targets: ["messages"], **rendering end @@ -232,13 +232,13 @@ def render_message(options) }) ) - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_prepend_later_to "stream", target: "messages", **rendering end end - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_prepend_later_to "stream", targets: ["messages"], **rendering end @@ -334,14 +334,14 @@ def render_message(options) }) ) - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_action_later_to \ "stream", action: "prepend", target: "messages", **rendering end end - assert_broadcast_on "stream", render_message(expected) do + assert_broadcast_on "stream", render(expected) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_action_later_to \ "stream", action: "prepend", targets: ["messages"], **rendering diff --git a/test/test_helper.rb b/test/test_helper.rb index df2eea6..8f22a45 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,7 +1,7 @@ # Configure Rails Environment ENV["RAILS_ENV"] = "test" require_relative "../test/dummy/config/environment" -# ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)] +ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)] require "rails/test_help" ActionCable.server.config.logger = Logger.new(STDOUT) if ENV["VERBOSE"] From f9bbf0ae9aa19ffae73fcc4200947c9928d4692c Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Mon, 12 May 2025 10:04:07 -0400 Subject: [PATCH 22/33] wip --- app/channels/superglue/streams/broadcasts.rb | 5 ++- .../superglue/streams/action_broadcast_job.rb | 4 +-- .../superglue/layouts/_fragment.json.props | 2 +- test/dummy/storage/test.sqlite3 | Bin 20480 -> 28672 bytes test/streams/streams_channel_test.rb | 34 +++++++++++------- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index d2a5996..41ab868 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -104,8 +104,11 @@ def broadcast_action_later_to(*streamables, action:, target: nil, targets: nil, convert_to_superglue_fragment_id(item) end + # bugs + # ActionBroadcastJOB doesn't take in targets + # targets does not work with multi ids Superglue::Streams::ActionBroadcastJob.perform_later \ - stream_name_from(streamables), action: action, target: target, targets: targets, options: options, **rendering + stream_name_from(streamables), action: action, targets: targets, options: options, **rendering end # def broadcast_render_to(*streamables, **rendering) diff --git a/app/jobs/superglue/streams/action_broadcast_job.rb b/app/jobs/superglue/streams/action_broadcast_job.rb index 99b4bcc..761b422 100644 --- a/app/jobs/superglue/streams/action_broadcast_job.rb +++ b/app/jobs/superglue/streams/action_broadcast_job.rb @@ -2,7 +2,7 @@ class Superglue::Streams::ActionBroadcastJob < ActiveJob::Base discard_on ActiveJob::DeserializationError - def perform(stream, action:, target:, options: {}, **rendering) - Superglue::StreamsChannel.broadcast_action_to stream, action: action, target: target, options: options, **rendering + def perform(stream, action:, targets:, options: {}, **rendering) + Superglue::StreamsChannel.broadcast_action_to stream, action: action, targets: targets, options: options, **rendering end end diff --git a/app/views/superglue/layouts/_fragment.json.props b/app/views/superglue/layouts/_fragment.json.props index 6761cb8..c29cc10 100644 --- a/app/views/superglue/layouts/_fragment.json.props +++ b/app/views/superglue/layouts/_fragment.json.props @@ -1,4 +1,4 @@ -json.disable_deferments! +# json.disable_deferments! json.data do yield diff --git a/test/dummy/storage/test.sqlite3 b/test/dummy/storage/test.sqlite3 index d3cc8350db4bc3628d3808660dd63e06967cd221..d3cc4f1aa75a6dfba421405a09b4d8126436ca2a 100644 GIT binary patch delta 633 zcmZozz}WDBae_QAI|BkRPt>s#=6lY-#>>UP@5uL_Pm6B}FBk8_jg6Cdm>WY_C(H4f zFtSY!=34+Hzwo&+vsW-rw&mAjWSN}9?*$~c^E=eD0?p$9nHj*uCa$f`7+I2-l#^Oq zn3GwO8eg1RSelxbjLv0t4svx2aa9O$bnvW3sLa%2jWm49~}jyjalGKWl$*cL?8F@D|D*Tt9WWd74$p4Ok|J`Om zfhYVj0*p+WMg~Tv2BwBa2F6C_#-?0Azay!T6J*xpL{-5HRKdjW!N9+b-velh4S#(O z6RUEpqhoP$Mrv+id~rsip{YSyqM?adl9^?iVNzn6sf9(NL26P`a*AbQN=llAnW>3I zqM?zcG00?H15;f?BLxFvD-#ndLsLBib0Z@IpmT7^crdami#j@{=9Og@<>%$5=DC)X vq!yQ8*I;O2Vv1A74CGlR{)Y_wANd~wy>gXbo}XEn5y>A+e9X$62%Aa(v+1YO delta 113 zcmZp8z}T>Wae_QAD+2;BP1LdE=l{XL!Y9qZZ_U4zPnz%3W Date: Mon, 12 May 2025 16:54:44 -0400 Subject: [PATCH 23/33] done --- test/streams/streams_channel_test.rb | 35 +++++++++++++++++----------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 992cfa9..d5e96cb 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -373,21 +373,30 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase end end - # test 'broadcasting action later with multiple ActiveModel targets' do - # options = { partial: 'messages/message', locals: { message: 'hello!' } } + test "broadcasting action later with multiple ActiveModel targets" do + locals = {message: "hello!"} + rendering = {partial: "messages/message", locals: locals} - # one = Message.new(id: 1) - # targets = [one, 'messages'] - # expected_targets = '#messages_message_1' + expected = rendering.merge( + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: ["msg_message_1", "msg_message_2"], + broadcast_action: "prepend", + broadcast_options: {} + }) + ) - # assert_broadcast_on 'stream', - # turbo_stream_action_tag('prepend', targets: expected_targets, template: render(options)) do - # perform_enqueued_jobs do - # Superglue::StreamsChannel.broadcast_action_later_to \ - # 'stream', action: 'prepend', targets: targets, **options - # end - # end - # end + one = Message.new(id: 1) + two = Message.new(id: 2) + targets = [[one, "msg"], [two, "msg"]] + + assert_broadcast_on "stream", render(expected) do + perform_enqueued_jobs do + Superglue::StreamsChannel.broadcast_action_later_to \ + "stream", action: "prepend", targets: targets, **rendering + end + end + end # test 'broadcasting render now' do # assert_broadcast_on 'stream', turbo_stream_action_tag('replace', target: 'message_1', template: 'Goodbye!') do From 0fdcbe8a178c5898f29e81ad419126ca069f07cf Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Mon, 12 May 2025 20:53:27 -0400 Subject: [PATCH 24/33] wip --- test/streams/streams_channel_test.rb | 165 +++++++-------------------- 1 file changed, 40 insertions(+), 125 deletions(-) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index d5e96cb..7cac6e8 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -29,25 +29,30 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase # Superglue::StreamsChannel.broadcast_remove_to 'stream', target: Message.new(id: 1, content: 'hello!') # end # end + # - test "broadcasting replace now" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} + def render_props(action, partial:, locals: {}, target: nil, targets: nil, options: {}) + targets = target ? [target] : targets - expected = rendering.merge( + render({ + partial: partial, layout: "superglue/layouts/fragment", locals: locals.merge({ - broadcast_targets: ["message_1"], - broadcast_action: "replace", - broadcast_options: {} + broadcast_targets: targets, + broadcast_action: action, + broadcast_options: options }) - ) + }) + end - assert_broadcast_on "stream", render(expected) do + test "broadcasting replace now" do + rendering = {partial: "messages/message", locals: {message: "hello!"}} + + assert_broadcast_on "stream", render_props("replace", target: "message_1", **rendering) do Superglue::StreamsChannel.broadcast_replace_to "stream", target: "message_1", **rendering end - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("replace", targets: ["message_1"], **rendering) do Superglue::StreamsChannel.broadcast_replace_to "stream", targets: ["message_1"], **rendering end end @@ -65,23 +70,13 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase # end test "broadcasting append now" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} - - expected = rendering.merge( - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: ["messages"], - broadcast_action: "append", - broadcast_options: {} - }) - ) + rendering = {partial: "messages/message", locals: {message: "hello!"}} - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("append", target: "messages", **rendering) do Superglue::StreamsChannel.broadcast_append_to "stream", target: "messages", **rendering end - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("append", targets: ["messages"], **rendering) do Superglue::StreamsChannel.broadcast_append_to "stream", targets: ["messages"], **rendering end end @@ -94,45 +89,25 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase # end test "broadcasting prepend now" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} - - expected = rendering.merge( - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: ["messages"], - broadcast_action: "prepend", - broadcast_options: {} - }) - ) + rendering = {partial: "messages/message", locals: {message: "hello!"}} - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", target: "messages", **rendering) do Superglue::StreamsChannel.broadcast_prepend_to "stream", target: "messages", **rendering end - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", targets: ["messages"], **rendering) do Superglue::StreamsChannel.broadcast_prepend_to "stream", targets: ["messages"], **rendering end end test "broadcasting action now" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} - - expected = rendering.merge( - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: ["messages"], - broadcast_action: "prepend", - broadcast_options: {} - }) - ) + rendering = {partial: "messages/message", locals: {message: "hello!"}} - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", target: "messages", **rendering) do Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", target: "messages", **rendering end - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", targets: ["messages"], **rendering) do Superglue::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ["messages"], **rendering end @@ -150,25 +125,15 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase end test "broadcasting replace later" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} - - expected = rendering.merge( - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: ["message_1"], - broadcast_action: "replace", - broadcast_options: {} - }) - ) + rendering = {partial: "messages/message", locals: {message: "hello!"}} - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("replace", target: "message_1", **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_replace_later_to "stream", target: "message_1", **rendering end end - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("replace", targets: ["message_1"], **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_replace_later_to "stream", targets: ["message_1"], **rendering end @@ -194,25 +159,15 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase # end test "broadcasting append later" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} + rendering = {partial: "messages/message", locals: {message: "hello!"}} - expected = rendering.merge( - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: ["messages"], - broadcast_action: "append", - broadcast_options: {} - }) - ) - - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("append", target: "messages", **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_append_later_to "stream", target: "messages", **rendering end end - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("append", targets: ["messages"], **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_append_later_to "stream", targets: ["messages"], **rendering end @@ -220,25 +175,15 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase end test "broadcasting prepend later" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} - - expected = rendering.merge( - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: ["messages"], - broadcast_action: "prepend", - broadcast_options: {} - }) - ) + rendering = {partial: "messages/message", locals: {message: "hello!"}} - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", target: "messages", **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_prepend_later_to "stream", target: "messages", **rendering end end - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", targets: ["messages"], **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_prepend_later_to "stream", targets: ["messages"], **rendering end @@ -322,26 +267,16 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase end test "broadcasting action later" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} - - expected = rendering.merge( - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: ["messages"], - broadcast_action: "prepend", - broadcast_options: {} - }) - ) + rendering = {partial: "messages/message", locals: {message: "hello!"}} - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", target: "messages", **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_action_later_to \ "stream", action: "prepend", target: "messages", **rendering end end - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", targets: ["messages"], **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_action_later_to \ "stream", action: "prepend", targets: ["messages"], **rendering @@ -350,22 +285,12 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase end test "broadcasting action later with ActiveModel array target" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} - - expected = rendering.merge( - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: ["opt_message_42"], - broadcast_action: "prepend", - broadcast_options: {} - }) - ) + rendering = {partial: "messages/message", locals: {message: "hello!"}} message = Message.new(id: 42) target = [message, "opt"] - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", target: "opt_message_42", **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_action_later_to \ "stream", action: "prepend", target: target, **rendering @@ -374,23 +299,13 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase end test "broadcasting action later with multiple ActiveModel targets" do - locals = {message: "hello!"} - rendering = {partial: "messages/message", locals: locals} - - expected = rendering.merge( - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: ["msg_message_1", "msg_message_2"], - broadcast_action: "prepend", - broadcast_options: {} - }) - ) + rendering = {partial: "messages/message", locals: {message: "hello!"}} one = Message.new(id: 1) two = Message.new(id: 2) targets = [[one, "msg"], [two, "msg"]] - assert_broadcast_on "stream", render(expected) do + assert_broadcast_on "stream", render_props("prepend", targets: ["msg_message_1", "msg_message_2"], **rendering) do perform_enqueued_jobs do Superglue::StreamsChannel.broadcast_action_later_to \ "stream", action: "prepend", targets: targets, **rendering From 6e75f6feb12b081cf1f11b9cc32c9b0dd5830e45 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Mon, 12 May 2025 20:55:30 -0400 Subject: [PATCH 25/33] wip --- test/streams/streams_channel_test.rb | 15 --------------- test/test_helper.rb | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/test/streams/streams_channel_test.rb b/test/streams/streams_channel_test.rb index 7cac6e8..42cf6e6 100644 --- a/test/streams/streams_channel_test.rb +++ b/test/streams/streams_channel_test.rb @@ -30,21 +30,6 @@ class Superglue::StreamsChannelTest < ActionCable::Channel::TestCase # end # end # - - def render_props(action, partial:, locals: {}, target: nil, targets: nil, options: {}) - targets = target ? [target] : targets - - render({ - partial: partial, - layout: "superglue/layouts/fragment", - locals: locals.merge({ - broadcast_targets: targets, - broadcast_action: action, - broadcast_options: options - }) - }) - end - test "broadcasting replace now" do rendering = {partial: "messages/message", locals: {message: "hello!"}} diff --git a/test/test_helper.rb b/test/test_helper.rb index 8f22a45..7976192 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -25,3 +25,17 @@ class ActionDispatch::IntegrationTest class ActionCable::Channel::TestCase include ActionViewTestCaseExtensions end + +def render_props(action, partial:, locals: {}, target: nil, targets: nil, options: {}) + targets = target ? [target] : targets + + render({ + partial: partial, + layout: "superglue/layouts/fragment", + locals: locals.merge({ + broadcast_targets: targets, + broadcast_action: action, + broadcast_options: options + }) + }) +end From ec33401aab59ddac4016e2633ed9ea6524694054 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Tue, 13 May 2025 00:23:54 -0400 Subject: [PATCH 26/33] broadcastable --- app/channels/superglue/streams/broadcasts.rb | 1 + .../concerns/superglue/broadcastable.rb | 210 +++++ test/streams/broadcastable_test.rb | 729 ++++++++++++++++++ 3 files changed, 940 insertions(+) create mode 100644 app/models/concerns/superglue/broadcastable.rb create mode 100644 test/streams/broadcastable_test.rb diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 41ab868..0f90d3a 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -33,6 +33,7 @@ def broadcast_prepend_to(*streamables, **opts) broadcast_action_to(*streamables, action: :prepend, **opts) end + # i think we need to keep this? # def broadcast_refresh_to(*streamables, **opts) # broadcast_stream_to(*streamables, content: superglue_stream_refresh_tag) # end diff --git a/app/models/concerns/superglue/broadcastable.rb b/app/models/concerns/superglue/broadcastable.rb new file mode 100644 index 0000000..802e36f --- /dev/null +++ b/app/models/concerns/superglue/broadcastable.rb @@ -0,0 +1,210 @@ +module Superglue::Broadcastable + extend ActiveSupport::Concern + + included do + thread_mattr_accessor :suppressed_superglue_broadcasts, instance_accessor: false + delegate :suppressed_superglue_broadcasts?, to: "self.class" + end + + module ClassMethods + def broadcasts_to(stream, inserts_by: :append, target: broadcast_target_default, **rendering) + after_create_commit -> { broadcast_action_later_to(stream.try(:call, self) || send(stream), action: inserts_by, target: target.try(:call, self) || target, **rendering) } + after_update_commit -> { broadcast_replace_later_to(stream.try(:call, self) || send(stream), **rendering) } + after_destroy_commit -> { broadcast_remove_to(stream.try(:call, self) || send(stream)) } + end + + def broadcasts(stream = model_name.plural, inserts_by: :append, target: broadcast_target_default, **rendering) + after_create_commit -> { broadcast_action_later_to(stream, action: inserts_by, target: target.try(:call, self) || target, **rendering) } + after_update_commit -> { broadcast_replace_later(**rendering) } + after_destroy_commit -> { broadcast_remove } + end + + def broadcasts_refreshes_to(stream) + after_commit -> { broadcast_refresh_later_to(stream.try(:call, self) || send(stream)) } + end + + def broadcasts_refreshes(stream = model_name.plural) + after_create_commit -> { broadcast_refresh_later_to(stream) } + after_update_commit -> { broadcast_refresh_later } + after_destroy_commit -> { broadcast_refresh } + end + + def broadcast_target_default + model_name.plural + end + + def suppressing_superglue_broadcasts(&block) + original, self.suppressed_superglue_broadcasts = suppressed_superglue_broadcasts, true + yield + ensure + self.suppressed_superglue_broadcasts = original + end + + def suppressed_superglue_broadcasts? + suppressed_superglue_broadcasts + end + end + + def broadcast_remove_to(*streamables, target: self, **rendering) + Superglue::StreamsChannel.broadcast_remove_to(*streamables, **extract_options_and_add_target(rendering, target: target)) unless suppressed_superglue_broadcasts? + end + + def broadcast_remove(**rendering) + broadcast_remove_to self, **rendering + end + + def broadcast_replace_to(*streamables, **rendering) + Superglue::StreamsChannel.broadcast_replace_to(*streamables, **extract_options_and_add_target(rendering, target: self)) unless suppressed_superglue_broadcasts? + end + + def broadcast_replace(**rendering) + broadcast_replace_to self, **rendering + end + + def broadcast_update_to(*streamables, **rendering) + Superglue::StreamsChannel.broadcast_update_to(*streamables, **extract_options_and_add_target(rendering, target: self)) unless suppressed_superglue_broadcasts? + end + + def broadcast_update(**rendering) + broadcast_update_to self, **rendering + end + + def broadcast_before_to(*streamables, target: nil, targets: nil, **rendering) + raise ArgumentError, "at least one of target or targets is required" unless target || targets + + Superglue::StreamsChannel.broadcast_before_to(*streamables, **extract_options_and_add_target(rendering.merge(target: target, targets: targets))) + end + + def broadcast_after_to(*streamables, target: nil, targets: nil, **rendering) + raise ArgumentError, "at least one of target or targets is required" unless target || targets + + Superglue::StreamsChannel.broadcast_after_to(*streamables, **extract_options_and_add_target(rendering.merge(target: target, targets: targets))) + end + + def broadcast_append_to(*streamables, target: broadcast_target_default, **rendering) + Superglue::StreamsChannel.broadcast_append_to(*streamables, **extract_options_and_add_target(rendering, target: target)) unless suppressed_superglue_broadcasts? + end + + def broadcast_append(target: broadcast_target_default, **rendering) + broadcast_append_to self, target: target, **rendering + end + + def broadcast_prepend_to(*streamables, target: broadcast_target_default, **rendering) + Superglue::StreamsChannel.broadcast_prepend_to(*streamables, **extract_options_and_add_target(rendering, target: target)) unless suppressed_superglue_broadcasts? + end + + def broadcast_prepend(target: broadcast_target_default, **rendering) + broadcast_prepend_to self, target: target, **rendering + end + + def broadcast_refresh_to(*streamables) + Superglue::StreamsChannel.broadcast_refresh_to(*streamables) unless suppressed_superglue_broadcasts? + end + + def broadcast_refresh + broadcast_refresh_to self + end + + def broadcast_action_to(*streamables, action:, target: broadcast_target_default, attributes: {}, **rendering) + Superglue::StreamsChannel.broadcast_action_to(*streamables, action: action, attributes: attributes, **extract_options_and_add_target(rendering, target: target)) unless suppressed_superglue_broadcasts? + end + + def broadcast_action(action, target: broadcast_target_default, attributes: {}, **rendering) + broadcast_action_to self, action: action, target: target, attributes: attributes, **rendering + end + + def broadcast_replace_later_to(*streamables, **rendering) + Superglue::StreamsChannel.broadcast_replace_later_to(*streamables, **extract_options_and_add_target(rendering, target: self)) unless suppressed_superglue_broadcasts? + end + + def broadcast_replace_later(**rendering) + broadcast_replace_later_to self, **rendering + end + + def broadcast_update_later_to(*streamables, **rendering) + Superglue::StreamsChannel.broadcast_update_later_to(*streamables, **extract_options_and_add_target(rendering, target: self)) unless suppressed_superglue_broadcasts? + end + + def broadcast_update_later(**rendering) + broadcast_update_later_to self, **rendering + end + + def broadcast_append_later_to(*streamables, target: broadcast_target_default, **rendering) + Superglue::StreamsChannel.broadcast_append_later_to(*streamables, **extract_options_and_add_target(rendering, target: target)) unless suppressed_superglue_broadcasts? + end + + def broadcast_append_later(target: broadcast_target_default, **rendering) + broadcast_append_later_to self, target: target, **rendering + end + + def broadcast_prepend_later_to(*streamables, target: broadcast_target_default, **rendering) + Superglue::StreamsChannel.broadcast_prepend_later_to(*streamables, **extract_options_and_add_target(rendering, target: target)) unless suppressed_superglue_broadcasts? + end + + def broadcast_prepend_later(target: broadcast_target_default, **rendering) + broadcast_prepend_later_to self, target: target, **rendering + end + + def broadcast_refresh_later_to(*streamables) + Superglue::StreamsChannel.broadcast_refresh_later_to(*streamables, request_id: superglue.current_request_id) unless suppressed_superglue_broadcasts? + end + + def broadcast_refresh_later + broadcast_refresh_later_to self + end + + def broadcast_action_later_to(*streamables, action:, target: broadcast_target_default, attributes: {}, **rendering) + Superglue::StreamsChannel.broadcast_action_later_to(*streamables, action: action, attributes: attributes, **extract_options_and_add_target(rendering, target: target)) unless suppressed_superglue_broadcasts? + end + + def broadcast_action_later(action:, target: broadcast_target_default, attributes: {}, **rendering) + broadcast_action_later_to self, action: action, target: target, attributes: attributes, **rendering + end + + def broadcast_render(**rendering) + broadcast_render_to self, **rendering + end + + def broadcast_render_to(*streamables, **rendering) + Superglue::StreamsChannel.broadcast_render_to(*streamables, **extract_options_and_add_target(rendering, target: self)) unless suppressed_superglue_broadcasts? + end + + def broadcast_render_later(**rendering) + broadcast_render_later_to self, **rendering + end + + def broadcast_render_later_to(*streamables, **rendering) + Superglue::StreamsChannel.broadcast_render_later_to(*streamables, **extract_options_and_add_target(rendering)) unless suppressed_superglue_broadcasts? + end + + private + + def broadcast_target_default + self.class.broadcast_target_default + end + + def extract_options_and_add_target(rendering = {}, target: broadcast_target_default) + broadcast_rendering_with_defaults(rendering).tap do |options| + options[:target] = target if !options.key?(:target) && !options.key?(:targets) + end + end + + def broadcast_rendering_with_defaults(options) + options.tap do |o| + # Add the current instance into the locals with the element name (which is the un-namespaced name) + # as the key. This parallels how the ActionView::ObjectRenderer would create a local variable. + o[:locals] = (o[:locals] || {}).reverse_merge(model_name.element.to_sym => self).compact + + if o[:html] || o[:partial] + return o + elsif o[:template] || o[:renderable] + o[:layout] = false + elsif o[:render] == false + return o + else + # if none of these options are passed in, it will set a partial from #to_partial_path + o[:partial] ||= to_partial_path + end + end + end +end diff --git a/test/streams/broadcastable_test.rb b/test/streams/broadcastable_test.rb new file mode 100644 index 0000000..16e603f --- /dev/null +++ b/test/streams/broadcastable_test.rb @@ -0,0 +1,729 @@ +require "test_helper" +require "action_cable" +require "minitest/mock" + +class Superglue::BroadcastableTest < ActionCable::Channel::TestCase + # include Superglue::Streams::ActionHelper + include ActiveJob::TestHelper + + class MessageThatRendersError < Message + def to_partial_path + "messages/raises_error" + end + end + + setup { @message = Message.new(id: 1, content: "Hello!") } + + test "broadcasting ignores blank streamables" do + ActionCable.server.stub :broadcast, proc { flunk "expected no broadcasts" } do + assert_no_broadcasts @message.to_gid_param do + @message.broadcast_append_to nil + @message.broadcast_append_to [nil] + @message.broadcast_append_to "" + @message.broadcast_append_to [""] + end + end + end + + test "broadcasting later ignores blank streamables" do + assert_no_enqueued_jobs do + @message.broadcast_append_later_to nil + @message.broadcast_append_later_to [nil] + @message.broadcast_append_later_to "" + @message.broadcast_append_later_to [""] + end + end + + # test "broadcasting remove to stream now" do + # assert_broadcast_on "stream", render_props("remove", target: "message_1") do + # @message.broadcast_remove_to "stream" + # end + # end + + # test "broadcasting remove now" do + # assert_broadcast_on @message.to_gid_param, render_props("remove", target: "message_1") do + # @message.broadcast_remove + # end + # end + + # test "broadcasting remove does not render contents" do + # message = MessageThatRendersError.new(id: 1) + + # assert_broadcast_on message.to_gid_param, render_props("remove", target: dom_id(message)) do + # message.broadcast_remove + # end + # end + + test "broadcasting replace to stream now" do + assert_broadcast_on "stream", render_props("replace", target: "message_1", template: render(@message)) do + @message.broadcast_replace_to "stream" + end + end + + test "broadcasting replace now" do + assert_broadcast_on @message.to_gid_param, render_props("replace", target: "message_1", template: render(@message)) do + @message.broadcast_replace + end + end + + # test "broadcasting update to stream now" do + # assert_broadcast_on "stream", render_props("update", target: "message_1", template: render(@message)) do + # @message.broadcast_update_to "stream" + # end + # end + + # test "broadcasting update to stream now with template option" do + # assert_broadcast_on "stream", render_props("update", target: "message_1", template: render("messages/index", layout: false)) do + # @message.broadcast_update_to "stream", template: "messages/index" + # end + # end + + # test "broadcasting update now" do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", template: render(@message)) do + # @message.broadcast_update + # end + # end + + # test "broadcasting before to stream now" do + # assert_broadcast_on "stream", render_props("before", target: "message_1", template: render(@message)) do + # @message.broadcast_before_to "stream", target: "message_1" + # end + # end + + # test "broadcasting after to stream now" do + # assert_broadcast_on "stream", render_props("after", target: "message_1", template: render(@message)) do + # @message.broadcast_after_to "stream", target: "message_1" + # end + # end + + test "broadcasting append to stream now" do + assert_broadcast_on "stream", render_props("append", target: "messages", template: render(@message)) do + @message.broadcast_append_to "stream" + end + end + + test "broadcasting append to stream with custom target now" do + assert_broadcast_on "stream", render_props("append", target: "board_messages", template: render(@message)) do + @message.broadcast_append_to "stream", target: "board_messages" + end + end + + test "broadcasting append now" do + assert_broadcast_on @message.to_gid_param, render_props("append", target: "messages", template: render(@message)) do + @message.broadcast_append + end + end + + test "broadcasting prepend to stream now" do + assert_broadcast_on "stream", render_props("prepend", target: "messages", template: render(@message)) do + @message.broadcast_prepend_to "stream" + end + end + + test "broadcasting prepend to stream with custom target now" do + assert_broadcast_on "stream", render_props("prepend", target: "board_messages", template: render(@message)) do + @message.broadcast_prepend_to "stream", target: "board_messages" + end + end + + test "broadcasting prepend now" do + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: render(@message)) do + @message.broadcast_prepend + end + end + + test "broadcasting refresh to stream now" do + assert_broadcast_on "stream", turbo_stream_refresh_tag do + @message.broadcast_refresh_to "stream" + end + end + + test "broadcasting refresh now" do + assert_broadcast_on @message.to_gid_param, turbo_stream_refresh_tag do + @message.broadcast_refresh + end + end + + test "broadcasting refresh does not render contents" do + message = MessageThatRendersError.new(id: 1) + + assert_broadcast_on message.to_gid_param, render_props("refresh") do + message.broadcast_refresh + end + end + + test "broadcasting refresh later is debounced" do + assert_broadcast_on @message.to_gid_param, turbo_stream_refresh_tag do + assert_broadcasts(@message.to_gid_param, 1) do + perform_enqueued_jobs do + assert_no_changes -> { Thread.current.keys.size } do + # Not leaking thread variables once the debounced code executes + 3.times { @message.broadcast_refresh_later } + Superglue::StreamsChannel.refresh_debouncer_for(@message).wait + end + end + end + end + end + + test "broadcasting action to stream now" do + assert_broadcast_on "stream", render_props("prepend", target: "messages", template: render(@message)) do + @message.broadcast_action_to "stream", action: "prepend" + end + end + + test "broadcasting action now" do + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: render(@message)) do + @message.broadcast_action "prepend" + end + end + + test "broadcasting action with attributes" do + assert_broadcast_on @message.to_gid_param, render_props("prepend", :target => "messages", :template => render(@message), "data-foo" => "bar") do + @message.broadcast_action "prepend", target: "messages", attributes: {"data-foo" => "bar"} + end + end + + test "broadcasting action with no rendering" do + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: nil) do + @message.broadcast_action "prepend", target: "messages", render: false + end + end + + test "broadcasting action to with attributes" do + assert_broadcast_on "stream", render_props("prepend", :target => "messages", :template => render(@message), "data-foo" => "bar") do + @message.broadcast_action_to "stream", action: "prepend", attributes: {"data-foo" => "bar"} + end + end + + test "broadcasting action to with no rendering" do + assert_broadcast_on "stream", render_props("prepend", target: "messages", template: nil) do + @message.broadcast_action_to "stream", action: "prepend", render: false + end + end + + test "broadcasting action later to with attributes" do + @message.save! + + assert_broadcast_on @message.to_gid_param, render_props("prepend", :target => "messages", :template => render(@message), "data-foo" => "bar") do + perform_enqueued_jobs do + @message.broadcast_action_later_to @message, action: "prepend", target: "messages", attributes: {"data-foo" => "bar"} + end + end + end + + test "broadcasting action later to with no rendering" do + @message.save! + + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: nil) do + perform_enqueued_jobs do + @message.broadcast_action_later_to @message, action: "prepend", target: "messages", render: false + end + end + end + + test "broadcasting action later with attributes" do + @message.save! + + assert_broadcast_on @message.to_gid_param, render_props("prepend", :target => "messages", :template => render(@message), "data-foo" => "bar") do + perform_enqueued_jobs do + @message.broadcast_action_later action: "prepend", target: "messages", attributes: {"data-foo" => "bar"} + end + end + end + + test "broadcasting action later with no rendering" do + @message.save! + + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: nil) do + perform_enqueued_jobs do + @message.broadcast_action_later action: "prepend", target: "messages", render: false + end + end + end + + test "render correct local name in partial for namespaced models" do + @profile = Users::Profile.new(id: 1, name: "Ryan") + assert_broadcast_on @profile.to_param, render_props("replace", target: "users_profile_1", template: "

Ryan

\n") do + @profile.broadcast_replace + end + end + + test "local variables don't get overwritten if they collide with the template name" do + @profile = Users::Profile.new(id: 1, name: "Ryan") + assert_broadcast_on @profile.to_param, render_props("replace", target: "users_profile_1", template: render(@message)) do + @profile.broadcast_replace partial: "messages/message", locals: {message: @message} + end + end + + # test "broadcast render now" do + # assert_broadcast_on @message.to_gid_param, render_props("replace", target: "message_1", template: "Goodbye!") do + # @message.broadcast_render + # end + # end + + # test "broadcast render to stream now" do + # @profile = Users::Profile.new(id: 1, name: "Ryan") + # assert_broadcast_on @profile.to_param, render_props("replace", target: "message_1", template: "Goodbye!") do + # @message.broadcast_render_to @profile + # end + # end + + # test "broadcast_update to target string" do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", template: render(@message)) do + # @message.broadcast_update target: "unique_id" + # end + # end + + # test "broadcast_update to target object" do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", template: render(@message)) do + # @message.broadcast_update target: @message + # end + # end + + # test "broadcast_update to targets" do + # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", template: render(@message)) do + # @message.broadcast_update targets: ".message_1" + # end + # end + + # test "broadcast_update_to to target string" do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", template: render(@message)) do + # @message.broadcast_update_to @message, target: "unique_id" + # end + # end + + # test "broadcast_update_to to target object" do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", template: render(@message)) do + # @message.broadcast_update_to @message, target: @message + # end + # end + + # test "broadcast_update_to to targets" do + # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", template: render(@message)) do + # @message.broadcast_update_to @message, targets: ".message_1" + # end + # end + + test "broadcast_append to targets" do + assert_broadcast_on @message.to_gid_param, render_props("append", targets: ["message_1"], template: render(@message)) do + @message.broadcast_append targets: ["message_1"] + end + end + + test "broadcast_remove targets" do + assert_broadcast_on @message.to_gid_param, render_props("remove", targets: ["message_1"], template: render(@message)) do + @message.broadcast_remove targets: ["message_1"] + end + end + + test "broadcast_append targets" do + assert_broadcast_on @message.to_gid_param, render_props("append", targets: ["message_1"], template: render(@message)) do + @message.broadcast_append targets: ["message_1"] + end + end + + test "broadcast_prepend targets" do + assert_broadcast_on @message.to_gid_param, render_props("prepend", targets: ["message_1"], template: render(@message)) do + @message.broadcast_prepend targets: ["message_1"] + end + end + + # test "broadcast_before_to targets" do + # assert_broadcast_on "stream", render_props("before", targets: ".message_1", template: render(@message)) do + # @message.broadcast_before_to "stream", targets: ".message_1" + # end + # end + + # test "broadcast_after_to targets" do + # assert_broadcast_on "stream", render_props("after", targets: ".message_1", template: render(@message)) do + # @message.broadcast_after_to "stream", targets: ".message_1" + # end + # end + # . //////// are we sure about the button + # test "broadcast_update_later" do + # @message.save! # Need to save the record, otherwise Active Job will not be able to retrieve it + + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", template: render(@message)) do + # perform_enqueued_jobs do + # @message.broadcast_update_later target: "unique_id" + # end + # end + # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", template: render(@message)) do + # perform_enqueued_jobs do + # @message.broadcast_update_later targets: ".message_1" + # end + # end + # end + + # test "broadcast_update_later_to" do + # @message.save! + + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", template: render(@message)) do + # perform_enqueued_jobs do + # @message.broadcast_update_later_to @message, target: "unique_id" + # end + # end + # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", template: render(@message)) do + # perform_enqueued_jobs do + # @message.broadcast_update_later_to @message, targets: ".message_1" + # end + # end + # end + + # test "broadcasting replace morph to stream now" do + # assert_broadcast_on "stream", render_props("replace", target: "message_1", method: :morph, template: render(@message)) do + # @message.broadcast_replace_to "stream", target: "message_1", attributes: {method: :morph} + # end + # end + + # test "broadcasting update morph to stream now targeting" do + # assert_broadcast_on "stream", render_props("update", target: "message_1", method: :morph, template: render(@message)) do + # @message.broadcast_update_to "stream", target: "message_1", attributes: {method: :morph} + # end + # end + + # test "broadcasting replace morph now" do + # assert_broadcast_on @message.to_gid_param, render_props("replace", target: "message_1", method: :morph, template: render(@message)) do + # @message.broadcast_replace target: "message_1", attributes: {method: :morph} + # end + # end + + # test "broadcasting update morph now" do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", method: :morph, template: render(@message)) do + # @message.broadcast_update target: "message_1", attributes: {method: :morph} + # end + # end +end + +class Superglue::BroadcastableArticleTest < ActionCable::Channel::TestCase + include Superglue::Streams::ActionHelper + include ActiveJob::TestHelper + + test "creating an article broadcasts to the overriden target with a string" do + assert_broadcast_on "overriden-stream", render_props("append", target: "overriden-target", template: "

Body

\n") do + perform_enqueued_jobs do + Article.create!(body: "Body") + end + end + end + + test "updating an article broadcasts" do + article = Article.create!(body: "Hey") + + assert_broadcast_on "ho", render_props("replace", target: "article_#{article.id}", template: "

Ho

\n") do + perform_enqueued_jobs do + article.update!(body: "Ho") + end + end + end + + test "destroying an article broadcasts" do + article = Article.create!(body: "Hey") + + assert_broadcast_on "hey", render_props("remove", target: "article_#{article.id}") do + article.destroy! + end + end +end + +class Superglue::BroadcastableCommentTest < ActionCable::Channel::TestCase + include Superglue::Streams::ActionHelper + include ActiveJob::TestHelper + + setup { @article = Article.create!(body: "Body") } + + test "creating a comment broadcasts to the overriden target with a lambda" do + stream = "#{@article.to_gid_param}:comments" + target = "article_#{@article.id}_comments" + + assert_broadcast_on stream, render_props("append", target: target, template: %(

comment

\n)) do + perform_enqueued_jobs do + @article.comments.create!(body: "comment") + end + end + end + + test "creating a second comment while using locals broadcasts the second comment" do + stream = "#{@article.to_gid_param}:comments" + target = "article_#{@article.id}_comments" + + assert_broadcast_on stream, render_props("append", target: target, template: %(

comment

\n)) do + perform_enqueued_jobs do + @article.comments.create!(body: "comment") + end + end + + assert_broadcast_on stream, render_props("append", target: target, template: %(

another comment

\n)) do + perform_enqueued_jobs do + @article.comments.create!(body: "another comment") + end + end + end + + test "updating a comment broadcasts" do + comment = @article.comments.create!(body: "random") + stream = "#{@article.to_gid_param}:comments" + target = "comment_#{comment.id}" + + assert_broadcast_on stream, render_props("replace", target: target, template: %(

precise

\n)) do + perform_enqueued_jobs do + comment.update!(body: "precise") + end + end + end + + test "destroying a comment broadcasts" do + comment = @article.comments.create!(body: "comment") + stream = "#{@article.to_gid_param}:comments" + target = "comment_#{comment.id}" + + assert_broadcast_on stream, render_props("remove", target: target) do + comment.destroy! + end + end +end + +class Superglue::BroadcastableBoardTest < ActionCable::Channel::TestCase + include Superglue::Streams::ActionHelper + include ActiveJob::TestHelper + + test "creating a board broadcasts refreshes to a channel using models plural name when creating" do + assert_broadcast_on "boards", render_props("refresh") do + perform_enqueued_jobs do + Board.create!(name: "Board") + Superglue::StreamsChannel.refresh_debouncer_for(["boards"]).wait + end + end + end + + test "updating a board broadcasts to the models channel" do + board = Board.suppressing_turbo_broadcasts do + Board.create!(name: "Hey") + end + + assert_broadcast_on board.to_gid_param, render_props("refresh") do + perform_enqueued_jobs do + board.update!(name: "Ho") + Superglue::StreamsChannel.refresh_debouncer_for(board).wait + end + end + end + + test "destroying a board broadcasts refreshes to the model channel" do + board = Board.suppressing_turbo_broadcasts do + Board.create!(name: "Hey") + end + + assert_broadcast_on board.to_gid_param, render_props("refresh") do + board.destroy! + end + end +end + +class Superglue::SuppressingBroadcastsTest < ActionCable::Channel::TestCase + include Superglue::Streams::ActionHelper + include ActiveJob::TestHelper + + setup { @message = Message.new(id: 1, content: "Hello!") } + + # test "suppressing broadcasting remove to stream now" do + # assert_no_broadcasts_when_suppressing do + # @message.broadcast_remove_to "stream" + # end + # end + + # test "suppressing broadcasting remove now" do + # assert_no_broadcasts_when_suppressing do + # @message.broadcast_remove + # end + # end + + test "suppressing broadcasting replace to stream now" do + assert_no_broadcasts_when_suppressing do + @message.broadcast_replace_to "stream" + end + end + + test "suppressing broadcasting replace to stream later" do + assert_no_broadcasts_later_when_supressing do + @message.broadcast_replace_later_to "stream" + end + end + + test "suppressing broadcasting replace now" do + assert_no_broadcasts_when_suppressing do + @message.broadcast_replace + end + end + + test "suppressing broadcasting replace later" do + assert_no_broadcasts_later_when_supressing do + @message.broadcast_replace_later + end + end + + # test "suppressing broadcasting update to stream now" do + # assert_no_broadcasts_when_suppressing do + # @message.broadcast_update_to "stream" + # end + # end + + # test "suppressing broadcasting update to stream later" do + # assert_no_broadcasts_later_when_supressing do + # @message.broadcast_update_later_to "stream" + # end + # end + + # test "suppressing broadcasting update now" do + # assert_no_broadcasts_when_suppressing do + # @message.broadcast_update + # end + # end + + # test "suppressing broadcasting update later" do + # assert_no_broadcasts_later_when_supressing do + # @message.broadcast_update_later + # end + # end + + # test "suppressing broadcasting before to stream now" do + # assert_no_broadcasts_when_suppressing do + # @message.broadcast_before_to "stream", target: "message_1" + # end + # end + + # test "suppressing broadcasting after to stream now" do + # assert_no_broadcasts_when_suppressing do + # @message.broadcast_after_to "stream", target: "message_1" + # end + # end + + test "suppressing broadcasting append to stream now" do + assert_no_broadcasts_when_suppressing do + @message.broadcast_append_to "stream" + end + end + + test "suppressing broadcasting append to stream later" do + assert_no_broadcasts_later_when_supressing do + @message.broadcast_append_later_to "stream" + end + end + + test "suppressing broadcasting append now" do + assert_no_broadcasts_when_suppressing do + @message.broadcast_append + end + end + + test "suppressing broadcasting append later" do + assert_no_broadcasts_later_when_supressing do + @message.broadcast_append_later + end + end + + test "suppressing broadcasting prepend to stream now" do + assert_no_broadcasts_when_suppressing do + @message.broadcast_prepend_to "stream" + end + end + + test "suppressing broadcasting prepend to stream later" do + assert_no_broadcasts_later_when_supressing do + @message.broadcast_prepend_later_to "stream" + end + end + + test "suppressing broadcasting refresh to stream now" do + assert_no_broadcasts_when_suppressing do + @message.broadcast_refresh_to "stream" + end + end + + test "suppressing broadcasting refresh to stream later" do + assert_no_broadcasts_later_when_supressing do + @message.broadcast_refresh_later_to "stream" + end + end + + test "suppressing broadcasting prepend now" do + assert_no_broadcasts_when_suppressing do + @message.broadcast_prepend + end + end + + test "suppressing broadcasting prepend later" do + assert_no_broadcasts_later_when_supressing do + @message.broadcast_prepend_later + end + end + + test "suppressing broadcasting action to stream now" do + assert_no_broadcasts_when_suppressing do + @message.broadcast_action_to "stream", action: "prepend" + end + end + + test "suppressing broadcasting action to stream later" do + assert_no_broadcasts_later_when_supressing do + @message.broadcast_action_later_to "stream", action: "prepend" + end + end + + test "suppressing broadcasting action now" do + assert_no_broadcasts_when_suppressing do + @message.broadcast_action "prepend" + end + end + + test "suppressing broadcasting action later" do + assert_no_broadcasts_later_when_supressing do + @message.broadcast_action_later action: "prepend" + end + end + + # test "suppressing broadcast render now" do + # assert_no_broadcasts_when_suppressing do + # @message.broadcast_render + # end + # end + + # test "suppressing broadcast render later" do + # assert_no_broadcasts_later_when_supressing do + # @message.broadcast_render_later + # end + # end + + # test "suppressing broadcast render to stream now" do + # @profile = Users::Profile.new(id: 1, name: "Ryan") + # assert_no_broadcasts_when_suppressing do + # @message.broadcast_render_to @profile + # end + # end + + # test "suppressing broadcast render to stream later" do + # @profile = Users::Profile.new(id: 1, name: "Ryan") + # assert_no_broadcasts_later_when_supressing do + # @message.broadcast_render_to @profile + # end + # end + + private + + def assert_no_broadcasts_when_suppressing + assert_no_broadcasts @message.to_gid_param do + Message.suppressing_turbo_broadcasts do + yield + end + end + end + + def assert_no_broadcasts_later_when_supressing + assert_no_broadcasts_when_suppressing do + assert_no_enqueued_jobs do + yield + end + end + end +end From 2914599d22161a384af27a548d65a546536efea8 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Wed, 14 May 2025 23:12:22 -0400 Subject: [PATCH 27/33] yayyyy --- test/dummy/app/models/message.rb | 1 + test/streams/broadcastable_test.rb | 76 +++++++++++++++--------------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/test/dummy/app/models/message.rb b/test/dummy/app/models/message.rb index ebe948c..9fe4df2 100644 --- a/test/dummy/app/models/message.rb +++ b/test/dummy/app/models/message.rb @@ -1,3 +1,4 @@ class Message < ApplicationRecord + include Superglue::Broadcastable delegate :to_s, to: :content, allow_nil: true end diff --git a/test/streams/broadcastable_test.rb b/test/streams/broadcastable_test.rb index 16e603f..84bc552 100644 --- a/test/streams/broadcastable_test.rb +++ b/test/streams/broadcastable_test.rb @@ -55,19 +55,19 @@ def to_partial_path # end test "broadcasting replace to stream now" do - assert_broadcast_on "stream", render_props("replace", target: "message_1", template: render(@message)) do + assert_broadcast_on "stream", render_props("replace", target: "message_1", partial: @message.to_partial_path) do @message.broadcast_replace_to "stream" end end test "broadcasting replace now" do - assert_broadcast_on @message.to_gid_param, render_props("replace", target: "message_1", template: render(@message)) do + assert_broadcast_on @message.to_gid_param, render_props("replace", target: "message_1", partial: @message.to_partial_path) do @message.broadcast_replace end end # test "broadcasting update to stream now" do - # assert_broadcast_on "stream", render_props("update", target: "message_1", template: render(@message)) do + # assert_broadcast_on "stream", render_props("update", target: "message_1", partial: @message.to_partial_path) do # @message.broadcast_update_to "stream" # end # end @@ -79,55 +79,55 @@ def to_partial_path # end # test "broadcasting update now" do - # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", partial: @message.to_partial_path) do # @message.broadcast_update # end # end # test "broadcasting before to stream now" do - # assert_broadcast_on "stream", render_props("before", target: "message_1", template: render(@message)) do + # assert_broadcast_on "stream", render_props("before", target: "message_1", partial: @message.to_partial_path) do # @message.broadcast_before_to "stream", target: "message_1" # end # end # test "broadcasting after to stream now" do - # assert_broadcast_on "stream", render_props("after", target: "message_1", template: render(@message)) do + # assert_broadcast_on "stream", render_props("after", target: "message_1", partial: @message.to_partial_path) do # @message.broadcast_after_to "stream", target: "message_1" # end # end test "broadcasting append to stream now" do - assert_broadcast_on "stream", render_props("append", target: "messages", template: render(@message)) do + assert_broadcast_on "stream", render_props("append", target: "messages", partial: @message.to_partial_path) do @message.broadcast_append_to "stream" end end test "broadcasting append to stream with custom target now" do - assert_broadcast_on "stream", render_props("append", target: "board_messages", template: render(@message)) do + assert_broadcast_on "stream", render_props("append", target: "board_messages", partial: @message.to_partial_path) do @message.broadcast_append_to "stream", target: "board_messages" end end test "broadcasting append now" do - assert_broadcast_on @message.to_gid_param, render_props("append", target: "messages", template: render(@message)) do + assert_broadcast_on @message.to_gid_param, render_props("append", target: "messages", partial: @message.to_partial_path) do @message.broadcast_append end end test "broadcasting prepend to stream now" do - assert_broadcast_on "stream", render_props("prepend", target: "messages", template: render(@message)) do + assert_broadcast_on "stream", render_props("prepend", target: "messages", partial: @message.to_partial_path) do @message.broadcast_prepend_to "stream" end end test "broadcasting prepend to stream with custom target now" do - assert_broadcast_on "stream", render_props("prepend", target: "board_messages", template: render(@message)) do + assert_broadcast_on "stream", render_props("prepend", target: "board_messages", partial: @message.to_partial_path) do @message.broadcast_prepend_to "stream", target: "board_messages" end end test "broadcasting prepend now" do - assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: render(@message)) do + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", partial: @message.to_partial_path) do @message.broadcast_prepend end end @@ -167,13 +167,13 @@ def to_partial_path end test "broadcasting action to stream now" do - assert_broadcast_on "stream", render_props("prepend", target: "messages", template: render(@message)) do + assert_broadcast_on "stream", render_props("prepend", target: "messages", partial: @message.to_partial_path) do @message.broadcast_action_to "stream", action: "prepend" end end test "broadcasting action now" do - assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: render(@message)) do + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", partial: @message.to_partial_path) do @message.broadcast_action "prepend" end end @@ -251,7 +251,7 @@ def to_partial_path test "local variables don't get overwritten if they collide with the template name" do @profile = Users::Profile.new(id: 1, name: "Ryan") - assert_broadcast_on @profile.to_param, render_props("replace", target: "users_profile_1", template: render(@message)) do + assert_broadcast_on @profile.to_param, render_props("replace", target: "users_profile_1", partial: @message.to_partial_path) do @profile.broadcast_replace partial: "messages/message", locals: {message: @message} end end @@ -270,73 +270,73 @@ def to_partial_path # end # test "broadcast_update to target string" do - # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", partial: @message.to_partial_path) do # @message.broadcast_update target: "unique_id" # end # end # test "broadcast_update to target object" do - # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", partial: @message.to_partial_path) do # @message.broadcast_update target: @message # end # end # test "broadcast_update to targets" do - # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", partial: @message.to_partial_path) do # @message.broadcast_update targets: ".message_1" # end # end # test "broadcast_update_to to target string" do - # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", partial: @message.to_partial_path) do # @message.broadcast_update_to @message, target: "unique_id" # end # end # test "broadcast_update_to to target object" do - # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", partial: @message.to_partial_path) do # @message.broadcast_update_to @message, target: @message # end # end # test "broadcast_update_to to targets" do - # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", partial: @message.to_partial_path) do # @message.broadcast_update_to @message, targets: ".message_1" # end # end test "broadcast_append to targets" do - assert_broadcast_on @message.to_gid_param, render_props("append", targets: ["message_1"], template: render(@message)) do + assert_broadcast_on @message.to_gid_param, render_props("append", targets: ["message_1"], partial: @message.to_partial_path) do @message.broadcast_append targets: ["message_1"] end end test "broadcast_remove targets" do - assert_broadcast_on @message.to_gid_param, render_props("remove", targets: ["message_1"], template: render(@message)) do + assert_broadcast_on @message.to_gid_param, render_props("remove", targets: ["message_1"], partial: @message.to_partial_path) do @message.broadcast_remove targets: ["message_1"] end end test "broadcast_append targets" do - assert_broadcast_on @message.to_gid_param, render_props("append", targets: ["message_1"], template: render(@message)) do + assert_broadcast_on @message.to_gid_param, render_props("append", targets: ["message_1"], partial: @message.to_partial_path) do @message.broadcast_append targets: ["message_1"] end end test "broadcast_prepend targets" do - assert_broadcast_on @message.to_gid_param, render_props("prepend", targets: ["message_1"], template: render(@message)) do + assert_broadcast_on @message.to_gid_param, render_props("prepend", targets: ["message_1"], partial: @message.to_partial_path) do @message.broadcast_prepend targets: ["message_1"] end end # test "broadcast_before_to targets" do - # assert_broadcast_on "stream", render_props("before", targets: ".message_1", template: render(@message)) do + # assert_broadcast_on "stream", render_props("before", targets: ".message_1", partial: @message.to_partial_path) do # @message.broadcast_before_to "stream", targets: ".message_1" # end # end # test "broadcast_after_to targets" do - # assert_broadcast_on "stream", render_props("after", targets: ".message_1", template: render(@message)) do + # assert_broadcast_on "stream", render_props("after", targets: ".message_1", partial: @message.to_partial_path) do # @message.broadcast_after_to "stream", targets: ".message_1" # end # end @@ -344,12 +344,12 @@ def to_partial_path # test "broadcast_update_later" do # @message.save! # Need to save the record, otherwise Active Job will not be able to retrieve it - # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", partial: @message.to_partial_path) do # perform_enqueued_jobs do # @message.broadcast_update_later target: "unique_id" # end # end - # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", partial: @message.to_partial_path) do # perform_enqueued_jobs do # @message.broadcast_update_later targets: ".message_1" # end @@ -359,12 +359,12 @@ def to_partial_path # test "broadcast_update_later_to" do # @message.save! - # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "unique_id", partial: @message.to_partial_path) do # perform_enqueued_jobs do # @message.broadcast_update_later_to @message, target: "unique_id" # end # end - # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", targets: ".message_1", partial: @message.to_partial_path) do # perform_enqueued_jobs do # @message.broadcast_update_later_to @message, targets: ".message_1" # end @@ -372,25 +372,25 @@ def to_partial_path # end # test "broadcasting replace morph to stream now" do - # assert_broadcast_on "stream", render_props("replace", target: "message_1", method: :morph, template: render(@message)) do + # assert_broadcast_on "stream", render_props("replace", target: "message_1", method: :morph, partial: @message.to_partial_path) do # @message.broadcast_replace_to "stream", target: "message_1", attributes: {method: :morph} # end # end # test "broadcasting update morph to stream now targeting" do - # assert_broadcast_on "stream", render_props("update", target: "message_1", method: :morph, template: render(@message)) do + # assert_broadcast_on "stream", render_props("update", target: "message_1", method: :morph, partial: @message.to_partial_path) do # @message.broadcast_update_to "stream", target: "message_1", attributes: {method: :morph} # end # end # test "broadcasting replace morph now" do - # assert_broadcast_on @message.to_gid_param, render_props("replace", target: "message_1", method: :morph, template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("replace", target: "message_1", method: :morph, partial: @message.to_partial_path) do # @message.broadcast_replace target: "message_1", attributes: {method: :morph} # end # end # test "broadcasting update morph now" do - # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", method: :morph, template: render(@message)) do + # assert_broadcast_on @message.to_gid_param, render_props("update", target: "message_1", method: :morph, partial: @message.to_partial_path) do # @message.broadcast_update target: "message_1", attributes: {method: :morph} # end # end @@ -498,7 +498,7 @@ class Superglue::BroadcastableBoardTest < ActionCable::Channel::TestCase end test "updating a board broadcasts to the models channel" do - board = Board.suppressing_turbo_broadcasts do + board = Board.suppressing_superglue_broadcasts do Board.create!(name: "Hey") end @@ -511,7 +511,7 @@ class Superglue::BroadcastableBoardTest < ActionCable::Channel::TestCase end test "destroying a board broadcasts refreshes to the model channel" do - board = Board.suppressing_turbo_broadcasts do + board = Board.suppressing_superglue_broadcasts do Board.create!(name: "Hey") end @@ -713,7 +713,7 @@ class Superglue::SuppressingBroadcastsTest < ActionCable::Channel::TestCase def assert_no_broadcasts_when_suppressing assert_no_broadcasts @message.to_gid_param do - Message.suppressing_turbo_broadcasts do + Message.suppressing_superglue_broadcasts do yield end end From a401a029f588ad19d8d0e47b788911478ff43298 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Thu, 15 May 2025 17:24:51 -0400 Subject: [PATCH 28/33] wip --- test/dummy/app/models/article.rb | 17 +++++++++++++ test/dummy/app/models/board.rb | 4 +++ test/dummy/app/models/comment.rb | 11 ++++++++ .../migrate/20250515210853_create_boards.rb | 9 +++++++ .../migrate/20250515210952_create_articles.rb | 9 +++++++ .../migrate/20250515211239_create_comments.rb | 10 ++++++++ test/dummy/db/schema.rb | 24 +++++++++++++++++- test/dummy/storage/test.sqlite3 | Bin 28672 -> 45056 bytes test/dummy/storage/test.sqlite3-shm | Bin 0 -> 32768 bytes test/dummy/storage/test.sqlite3-wal | 0 test/streams/broadcastable_test.rb | 16 ++++++------ 11 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 test/dummy/app/models/article.rb create mode 100644 test/dummy/app/models/board.rb create mode 100644 test/dummy/app/models/comment.rb create mode 100644 test/dummy/db/migrate/20250515210853_create_boards.rb create mode 100644 test/dummy/db/migrate/20250515210952_create_articles.rb create mode 100644 test/dummy/db/migrate/20250515211239_create_comments.rb create mode 100644 test/dummy/storage/test.sqlite3-shm create mode 100644 test/dummy/storage/test.sqlite3-wal diff --git a/test/dummy/app/models/article.rb b/test/dummy/app/models/article.rb new file mode 100644 index 0000000..586b442 --- /dev/null +++ b/test/dummy/app/models/article.rb @@ -0,0 +1,17 @@ +class Article < ApplicationRecord + include Superglue::Broadcastable + + has_many :comments + + validates :body, presence: true + + broadcasts "overriden-stream", target: "overriden-target" + + def to_gid_param + to_param + end + + def to_param + body.parameterize + end +end diff --git a/test/dummy/app/models/board.rb b/test/dummy/app/models/board.rb new file mode 100644 index 0000000..f06aaea --- /dev/null +++ b/test/dummy/app/models/board.rb @@ -0,0 +1,4 @@ +class Board < ApplicationRecord + include Superglue::Broadcastable + broadcasts_refreshes +end diff --git a/test/dummy/app/models/comment.rb b/test/dummy/app/models/comment.rb new file mode 100644 index 0000000..fc65a83 --- /dev/null +++ b/test/dummy/app/models/comment.rb @@ -0,0 +1,11 @@ +class Comment < ApplicationRecord + include Superglue::Broadcastable + belongs_to :article + + validates :body, presence: true + + broadcasts_to ->(comment) { [comment.article, :comments] }, + target: ->(comment) { "article_#{comment.article_id}_comments" }, + partial: "comments/different_comment", + locals: {highlight: true} +end diff --git a/test/dummy/db/migrate/20250515210853_create_boards.rb b/test/dummy/db/migrate/20250515210853_create_boards.rb new file mode 100644 index 0000000..481eb31 --- /dev/null +++ b/test/dummy/db/migrate/20250515210853_create_boards.rb @@ -0,0 +1,9 @@ +class CreateBoards < ActiveRecord::Migration[7.2] + def change + create_table :boards do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/test/dummy/db/migrate/20250515210952_create_articles.rb b/test/dummy/db/migrate/20250515210952_create_articles.rb new file mode 100644 index 0000000..4666be9 --- /dev/null +++ b/test/dummy/db/migrate/20250515210952_create_articles.rb @@ -0,0 +1,9 @@ +class CreateArticles < ActiveRecord::Migration[7.2] + def change + create_table :articles do |t| + t.text :body, null: false + + t.timestamps + end + end +end diff --git a/test/dummy/db/migrate/20250515211239_create_comments.rb b/test/dummy/db/migrate/20250515211239_create_comments.rb new file mode 100644 index 0000000..9266c42 --- /dev/null +++ b/test/dummy/db/migrate/20250515211239_create_comments.rb @@ -0,0 +1,10 @@ +class CreateComments < ActiveRecord::Migration[7.2] + def change + create_table :comments do |t| + t.text :body, null: false + t.references :article, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 724b460..c4c3542 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -10,10 +10,32 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2025_05_12_032735) do +ActiveRecord::Schema[7.2].define(version: 2025_05_15_211239) do + create_table "articles", force: :cascade do |t| + t.text "body", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "boards", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "comments", force: :cascade do |t| + t.text "body", null: false + t.integer "article_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["article_id"], name: "index_comments_on_article_id" + end + create_table "messages", force: :cascade do |t| t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false end + + add_foreign_key "comments", "articles" end diff --git a/test/dummy/storage/test.sqlite3 b/test/dummy/storage/test.sqlite3 index d3cc4f1aa75a6dfba421405a09b4d8126436ca2a..a02331e0ceaec5a37409b258e7e9392760710f69 100644 GIT binary patch delta 1035 zcmaJ;&rj1}7+%-1AMM(&;DHb``O=e%gS21QZAD`Q>sBZp9*)Pc&AFygTCu8@Z5zv$?dPy# zfVV|g__eyj=IGtCET48cl)gV`Xt~ZRzimi*Ka#}FbyeV>IE?-b) zXH+<+Ji4Hmgs`CGl!Bs8E5+V2yJJkn^ z;zg7ZB^K`?5)$~VB(lLiM3xYXoks*D%AJ)2*b9I?@D=<7$1eMOFgJuJuzq&p9Mq-9 zFF-DTa1c*KyMu05XTk2mWAHR>S{5%0$PzNyl9@6}hHREiBrK&ef@t!xR7R;(*-Ujt p$9QQ>kRTE!5I-Si#s#=6lY-#>>UP@5uL_Pm6B}FBk8_&4L1xc$gbQSSRo0 zG+|_${E2e`ketlr#>`&9Joyfn9wWbw2mk "messages", :template => render(@message), "data-foo" => "bar") do - @message.broadcast_action "prepend", target: "messages", attributes: {"data-foo" => "bar"} + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", partial: @message.to_partial_path, options: {"data-foo" => "bar"}) do + @message.broadcast_action "prepend", target: "messages", options: {"data-foo" => "bar"} end end @@ -191,8 +191,8 @@ def to_partial_path end test "broadcasting action to with attributes" do - assert_broadcast_on "stream", render_props("prepend", :target => "messages", :template => render(@message), "data-foo" => "bar") do - @message.broadcast_action_to "stream", action: "prepend", attributes: {"data-foo" => "bar"} + assert_broadcast_on "stream", render_props("prepend", target: "messages", partial: @message.to_partial_path, options: {"data-foo" => "bar"}) do + @message.broadcast_action_to "stream", action: "prepend", options: {"data-foo" => "bar"} end end @@ -205,9 +205,9 @@ def to_partial_path test "broadcasting action later to with attributes" do @message.save! - assert_broadcast_on @message.to_gid_param, render_props("prepend", :target => "messages", :template => render(@message), "data-foo" => "bar") do + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", partial: @message.to_partial_path, options: {"data-foo" => "bar"}) do perform_enqueued_jobs do - @message.broadcast_action_later_to @message, action: "prepend", target: "messages", attributes: {"data-foo" => "bar"} + @message.broadcast_action_later_to @message, action: "prepend", target: "messages", options: {"data-foo" => "bar"} end end end @@ -225,9 +225,9 @@ def to_partial_path test "broadcasting action later with attributes" do @message.save! - assert_broadcast_on @message.to_gid_param, render_props("prepend", :target => "messages", :template => render(@message), "data-foo" => "bar") do + assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", partial: @message.to_partial_path, options: {"data-foo" => "bar"}) do perform_enqueued_jobs do - @message.broadcast_action_later action: "prepend", target: "messages", attributes: {"data-foo" => "bar"} + @message.broadcast_action_later action: "prepend", target: "messages", options: {"data-foo" => "bar"} end end end From 76368768df24aaa3bea1af8172426930e9dbb5c0 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Thu, 15 May 2025 19:44:39 -0400 Subject: [PATCH 29/33] wip --- test/dummy/app/models/users/profile.rb | 16 ++++++++++++++++ .../dummy/app/views/articles/_article.json.props | 1 + test/streams/broadcastable_test.rb | 16 ++++++++-------- 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 test/dummy/app/models/users/profile.rb create mode 100644 test/dummy/app/views/articles/_article.json.props diff --git a/test/dummy/app/models/users/profile.rb b/test/dummy/app/models/users/profile.rb new file mode 100644 index 0000000..8837ee2 --- /dev/null +++ b/test/dummy/app/models/users/profile.rb @@ -0,0 +1,16 @@ +module Users + class Profile + include ActiveModel::Model + include Superglue::Broadcastable + + attr_accessor :id, :name + + def to_param + "users:profile:#{id}" + end + + def to_partial_path + "users/profiles/profile" + end + end +end diff --git a/test/dummy/app/views/articles/_article.json.props b/test/dummy/app/views/articles/_article.json.props new file mode 100644 index 0000000..007efda --- /dev/null +++ b/test/dummy/app/views/articles/_article.json.props @@ -0,0 +1 @@ +json.body article.body diff --git a/test/streams/broadcastable_test.rb b/test/streams/broadcastable_test.rb index 9f161a8..df480d3 100644 --- a/test/streams/broadcastable_test.rb +++ b/test/streams/broadcastable_test.rb @@ -401,7 +401,7 @@ class Superglue::BroadcastableArticleTest < ActionCable::Channel::TestCase include ActiveJob::TestHelper test "creating an article broadcasts to the overriden target with a string" do - assert_broadcast_on "overriden-stream", render_props("append", target: "overriden-target", template: "

Body

\n") do + assert_broadcast_on "overriden-stream", render_props("append", target: "overriden-target", partial: "articles/article", locals: {article: Article.new(body: "Body")}) do perform_enqueued_jobs do Article.create!(body: "Body") end @@ -411,20 +411,20 @@ class Superglue::BroadcastableArticleTest < ActionCable::Channel::TestCase test "updating an article broadcasts" do article = Article.create!(body: "Hey") - assert_broadcast_on "ho", render_props("replace", target: "article_#{article.id}", template: "

Ho

\n") do + assert_broadcast_on "ho", render_props("replace", target: "article_#{article.id}", partial: "articles/article", locals: {article: article}) do perform_enqueued_jobs do article.update!(body: "Ho") end end end - test "destroying an article broadcasts" do - article = Article.create!(body: "Hey") + # test "destroying an article broadcasts" do + # article = Article.create!(body: "Hey") - assert_broadcast_on "hey", render_props("remove", target: "article_#{article.id}") do - article.destroy! - end - end + # assert_broadcast_on "hey", render_props("remove", target: "article_#{article.id}") do + # article.destroy! + # end + # end end class Superglue::BroadcastableCommentTest < ActionCable::Channel::TestCase From f7396e6b9885f762517597eeb78744c7531e6d56 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Thu, 15 May 2025 22:41:15 -0400 Subject: [PATCH 30/33] wip --- app/channels/superglue/streams/broadcasts.rb | 15 +++-- app/views/superglue/body.json.props | 8 +++ test/streams/broadcastable_test.rb | 64 ++++++++++---------- 3 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 app/views/superglue/body.json.props diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 0f90d3a..1eeb9fd 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -150,14 +150,21 @@ def render_format(format, **rendering) end def render_broadcast_action(rendering) - content = rendering.delete(:content) # i should remove content - html = rendering.delete(:html) # i should add json and stringify it + # content = rendering.delete(:content) # i should remove content + # html = rendering.delete(:html) # i should add json and stringify it render = rendering.delete(:render) + json = rendering.delete(:json) if render == false nil - else - content || html || (render_format(:json, **rendering) if rendering.present?) + elsif rendering.present? + if json + rendering[:partial] = "superglue/body" + rendering[:locals] ||= {} + rendering[:locals][:broadcast_json] = json + end + + render_format(:json, **rendering) end end end diff --git a/app/views/superglue/body.json.props b/app/views/superglue/body.json.props new file mode 100644 index 0000000..c1e5fb9 --- /dev/null +++ b/app/views/superglue/body.json.props @@ -0,0 +1,8 @@ +# json.disable_deferments! +json.data broadcast_json + +json.fragments json.fragments! +json.type "message" +json.action broadcast_action +json.targets broadcast_targets +json.options broadcast_options diff --git a/test/streams/broadcastable_test.rb b/test/streams/broadcastable_test.rb index df480d3..03355e7 100644 --- a/test/streams/broadcastable_test.rb +++ b/test/streams/broadcastable_test.rb @@ -184,11 +184,11 @@ def to_partial_path end end - test "broadcasting action with no rendering" do - assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: nil) do - @message.broadcast_action "prepend", target: "messages", render: false - end - end + # test "broadcasting action with no rendering" do + # assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: nil) do + # @message.broadcast_action "prepend", target: "messages", render: false + # end + # end test "broadcasting action to with attributes" do assert_broadcast_on "stream", render_props("prepend", target: "messages", partial: @message.to_partial_path, options: {"data-foo" => "bar"}) do @@ -196,11 +196,11 @@ def to_partial_path end end - test "broadcasting action to with no rendering" do - assert_broadcast_on "stream", render_props("prepend", target: "messages", template: nil) do - @message.broadcast_action_to "stream", action: "prepend", render: false - end - end + # test "broadcasting action to with no rendering" do + # assert_broadcast_on "stream", render_props("prepend", target: "messages", template: nil) do + # @message.broadcast_action_to "stream", action: "prepend", render: false + # end + # end test "broadcasting action later to with attributes" do @message.save! @@ -212,15 +212,15 @@ def to_partial_path end end - test "broadcasting action later to with no rendering" do - @message.save! + # test "broadcasting action later to with no rendering" do + # @message.save! - assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: nil) do - perform_enqueued_jobs do - @message.broadcast_action_later_to @message, action: "prepend", target: "messages", render: false - end - end - end + # assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: nil) do + # perform_enqueued_jobs do + # @message.broadcast_action_later_to @message, action: "prepend", target: "messages", render: false + # end + # end + # end test "broadcasting action later with attributes" do @message.save! @@ -232,15 +232,15 @@ def to_partial_path end end - test "broadcasting action later with no rendering" do - @message.save! + # test "broadcasting action later with no rendering" do + # @message.save! - assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: nil) do - perform_enqueued_jobs do - @message.broadcast_action_later action: "prepend", target: "messages", render: false - end - end - end + # assert_broadcast_on @message.to_gid_param, render_props("prepend", target: "messages", template: nil) do + # perform_enqueued_jobs do + # @message.broadcast_action_later action: "prepend", target: "messages", render: false + # end + # end + # end test "render correct local name in partial for namespaced models" do @profile = Users::Profile.new(id: 1, name: "Ryan") @@ -311,11 +311,11 @@ def to_partial_path end end - test "broadcast_remove targets" do - assert_broadcast_on @message.to_gid_param, render_props("remove", targets: ["message_1"], partial: @message.to_partial_path) do - @message.broadcast_remove targets: ["message_1"] - end - end + # test "broadcast_remove targets" do + # assert_broadcast_on @message.to_gid_param, render_props("remove", targets: ["message_1"], partial: @message.to_partial_path) do + # @message.broadcast_remove targets: ["message_1"] + # end + # end test "broadcast_append targets" do assert_broadcast_on @message.to_gid_param, render_props("append", targets: ["message_1"], partial: @message.to_partial_path) do @@ -411,7 +411,7 @@ class Superglue::BroadcastableArticleTest < ActionCable::Channel::TestCase test "updating an article broadcasts" do article = Article.create!(body: "Hey") - assert_broadcast_on "ho", render_props("replace", target: "article_#{article.id}", partial: "articles/article", locals: {article: article}) do + assert_broadcast_on "ho", render_props("replace", target: "article_#{article.id}", partial: "articles/article", locals: {article: Article.new(body: "Ho")}) do perform_enqueued_jobs do article.update!(body: "Ho") end From 239594f286a4c43e830be724bc779f6206be3e14 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Thu, 15 May 2025 23:20:10 -0400 Subject: [PATCH 31/33] almost --- app/channels/superglue/streams/broadcasts.rb | 14 +++++++++---- .../concerns/superglue/broadcastable.rb | 2 +- .../views/users/profiles/_profile.json.props | 1 + test/streams/broadcastable_test.rb | 21 +++++++++++++------ 4 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 test/dummy/app/views/users/profiles/_profile.json.props diff --git a/app/channels/superglue/streams/broadcasts.rb b/app/channels/superglue/streams/broadcasts.rb index 1eeb9fd..6321f40 100644 --- a/app/channels/superglue/streams/broadcasts.rb +++ b/app/channels/superglue/streams/broadcasts.rb @@ -33,10 +33,16 @@ def broadcast_prepend_to(*streamables, **opts) broadcast_action_to(*streamables, action: :prepend, **opts) end - # i think we need to keep this? - # def broadcast_refresh_to(*streamables, **opts) - # broadcast_stream_to(*streamables, content: superglue_stream_refresh_tag) - # end + def broadcast_refresh_to(*streamables, **opts) + request_id = Superglue.current_request_id + content = JSON.generate({ + type: "message", + action: "refresh", + requestId: request_id, + options: opts + }) + broadcast_stream_to(*streamables, content: content) + end def broadcast_action_to(*streamables, action:, target: nil, targets: nil, options: {}, **rendering) locals = rendering[:locals] || {} diff --git a/app/models/concerns/superglue/broadcastable.rb b/app/models/concerns/superglue/broadcastable.rb index 802e36f..c66f567 100644 --- a/app/models/concerns/superglue/broadcastable.rb +++ b/app/models/concerns/superglue/broadcastable.rb @@ -146,7 +146,7 @@ def broadcast_prepend_later(target: broadcast_target_default, **rendering) end def broadcast_refresh_later_to(*streamables) - Superglue::StreamsChannel.broadcast_refresh_later_to(*streamables, request_id: superglue.current_request_id) unless suppressed_superglue_broadcasts? + Superglue::StreamsChannel.broadcast_refresh_later_to(*streamables, request_id: Superglue.current_request_id) unless suppressed_superglue_broadcasts? end def broadcast_refresh_later diff --git a/test/dummy/app/views/users/profiles/_profile.json.props b/test/dummy/app/views/users/profiles/_profile.json.props new file mode 100644 index 0000000..ebd0aba --- /dev/null +++ b/test/dummy/app/views/users/profiles/_profile.json.props @@ -0,0 +1 @@ +json.name profile.name diff --git a/test/streams/broadcastable_test.rb b/test/streams/broadcastable_test.rb index 03355e7..807500d 100644 --- a/test/streams/broadcastable_test.rb +++ b/test/streams/broadcastable_test.rb @@ -2,6 +2,15 @@ require "action_cable" require "minitest/mock" +def render_refresh(request_id = nil) + JSON.generate({ + type: "message", + action: "refresh", + requestId: request_id, + options: {} + }) +end + class Superglue::BroadcastableTest < ActionCable::Channel::TestCase # include Superglue::Streams::ActionHelper include ActiveJob::TestHelper @@ -133,13 +142,13 @@ def to_partial_path end test "broadcasting refresh to stream now" do - assert_broadcast_on "stream", turbo_stream_refresh_tag do + assert_broadcast_on "stream", render_refresh do @message.broadcast_refresh_to "stream" end end test "broadcasting refresh now" do - assert_broadcast_on @message.to_gid_param, turbo_stream_refresh_tag do + assert_broadcast_on @message.to_gid_param, render_refresh do @message.broadcast_refresh end end @@ -147,13 +156,13 @@ def to_partial_path test "broadcasting refresh does not render contents" do message = MessageThatRendersError.new(id: 1) - assert_broadcast_on message.to_gid_param, render_props("refresh") do + assert_broadcast_on message.to_gid_param, render_refresh do message.broadcast_refresh end end test "broadcasting refresh later is debounced" do - assert_broadcast_on @message.to_gid_param, turbo_stream_refresh_tag do + assert_broadcast_on @message.to_gid_param, render_refresh do assert_broadcasts(@message.to_gid_param, 1) do perform_enqueued_jobs do assert_no_changes -> { Thread.current.keys.size } do @@ -244,7 +253,7 @@ def to_partial_path test "render correct local name in partial for namespaced models" do @profile = Users::Profile.new(id: 1, name: "Ryan") - assert_broadcast_on @profile.to_param, render_props("replace", target: "users_profile_1", template: "

Ryan

\n") do + assert_broadcast_on @profile.to_param, render_props("replace", target: "users_profile_1", partial: @profile.to_partial_path, locals: {profile: @profile}) do @profile.broadcast_replace end end @@ -489,7 +498,7 @@ class Superglue::BroadcastableBoardTest < ActionCable::Channel::TestCase include ActiveJob::TestHelper test "creating a board broadcasts refreshes to a channel using models plural name when creating" do - assert_broadcast_on "boards", render_props("refresh") do + assert_broadcast_on "boards", render_refresh do perform_enqueued_jobs do Board.create!(name: "Board") Superglue::StreamsChannel.refresh_debouncer_for(["boards"]).wait From c92ae1443e05cadd320ccff28583422a000cf1e1 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Thu, 15 May 2025 23:23:25 -0400 Subject: [PATCH 32/33] almost --- test/streams/broadcastable_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/streams/broadcastable_test.rb b/test/streams/broadcastable_test.rb index 807500d..eacc81d 100644 --- a/test/streams/broadcastable_test.rb +++ b/test/streams/broadcastable_test.rb @@ -511,7 +511,7 @@ class Superglue::BroadcastableBoardTest < ActionCable::Channel::TestCase Board.create!(name: "Hey") end - assert_broadcast_on board.to_gid_param, render_props("refresh") do + assert_broadcast_on board.to_gid_param, render_refresh do perform_enqueued_jobs do board.update!(name: "Ho") Superglue::StreamsChannel.refresh_debouncer_for(board).wait @@ -524,7 +524,7 @@ class Superglue::BroadcastableBoardTest < ActionCable::Channel::TestCase Board.create!(name: "Hey") end - assert_broadcast_on board.to_gid_param, render_props("refresh") do + assert_broadcast_on board.to_gid_param, render_refresh do board.destroy! end end From 1d8d3bf6c293348af63aef0790bca83db3610f8e Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Thu, 15 May 2025 23:37:18 -0400 Subject: [PATCH 33/33] donneeee --- .../app/views/comments/_comment.json.props | 1 + .../comments/_different_comment.json.props | 1 + test/streams/broadcastable_test.rb | 24 +++++++++---------- 3 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 test/dummy/app/views/comments/_comment.json.props create mode 100644 test/dummy/app/views/comments/_different_comment.json.props diff --git a/test/dummy/app/views/comments/_comment.json.props b/test/dummy/app/views/comments/_comment.json.props new file mode 100644 index 0000000..347b91a --- /dev/null +++ b/test/dummy/app/views/comments/_comment.json.props @@ -0,0 +1 @@ +json.body comment.body diff --git a/test/dummy/app/views/comments/_different_comment.json.props b/test/dummy/app/views/comments/_different_comment.json.props new file mode 100644 index 0000000..4b926dc --- /dev/null +++ b/test/dummy/app/views/comments/_different_comment.json.props @@ -0,0 +1 @@ +json.different comment.body diff --git a/test/streams/broadcastable_test.rb b/test/streams/broadcastable_test.rb index eacc81d..baa5b5d 100644 --- a/test/streams/broadcastable_test.rb +++ b/test/streams/broadcastable_test.rb @@ -446,7 +446,7 @@ class Superglue::BroadcastableCommentTest < ActionCable::Channel::TestCase stream = "#{@article.to_gid_param}:comments" target = "article_#{@article.id}_comments" - assert_broadcast_on stream, render_props("append", target: target, template: %(

comment

\n)) do + assert_broadcast_on stream, render_props("append", target: target, partial: "comments/different_comment", locals: {comment: Comment.new(body: "comment")}) do perform_enqueued_jobs do @article.comments.create!(body: "comment") end @@ -457,13 +457,13 @@ class Superglue::BroadcastableCommentTest < ActionCable::Channel::TestCase stream = "#{@article.to_gid_param}:comments" target = "article_#{@article.id}_comments" - assert_broadcast_on stream, render_props("append", target: target, template: %(

comment

\n)) do + assert_broadcast_on stream, render_props("append", target: target, partial: "comments/different_comment", locals: {comment: Comment.new(body: "comment")}) do perform_enqueued_jobs do @article.comments.create!(body: "comment") end end - assert_broadcast_on stream, render_props("append", target: target, template: %(

another comment

\n)) do + assert_broadcast_on stream, render_props("append", target: target, partial: "comments/different_comment", locals: {comment: Comment.new(body: "another comment")}) do perform_enqueued_jobs do @article.comments.create!(body: "another comment") end @@ -475,22 +475,22 @@ class Superglue::BroadcastableCommentTest < ActionCable::Channel::TestCase stream = "#{@article.to_gid_param}:comments" target = "comment_#{comment.id}" - assert_broadcast_on stream, render_props("replace", target: target, template: %(

precise

\n)) do + assert_broadcast_on stream, render_props("replace", target: target, partial: "comments/different_comment", locals: {comment: Comment.new(body: "precise")}) do perform_enqueued_jobs do comment.update!(body: "precise") end end end - test "destroying a comment broadcasts" do - comment = @article.comments.create!(body: "comment") - stream = "#{@article.to_gid_param}:comments" - target = "comment_#{comment.id}" + # test "destroying a comment broadcasts" do + # comment = @article.comments.create!(body: "comment") + # stream = "#{@article.to_gid_param}:comments" + # target = "comment_#{comment.id}" - assert_broadcast_on stream, render_props("remove", target: target) do - comment.destroy! - end - end + # assert_broadcast_on stream, render_props("remove", target: target) do + # comment.destroy! + # end + # end end class Superglue::BroadcastableBoardTest < ActionCable::Channel::TestCase