From 9c51115bc69855ee13fd67dbb718bd22e6e77e77 Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Wed, 10 Jul 2024 13:32:12 +0930 Subject: [PATCH] Allow passing of script attributes to tinymce_assets helper --- lib/tinymce/rails/engine.rb | 3 +++ lib/tinymce/rails/helper.rb | 4 ++-- sandbox/app/views/editor/helpers.html.erb | 2 +- spec/helpers/helper_spec.rb | 8 +++++++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/tinymce/rails/engine.rb b/lib/tinymce/rails/engine.rb index a9e8e3fc..8c64d6dc 100644 --- a/lib/tinymce/rails/engine.rb +++ b/lib/tinymce/rails/engine.rb @@ -13,6 +13,9 @@ class Engine < ::Rails::Engine # :copy - copies across the TinyMCE assets statically config.tinymce.install = :compile + # Set default attributes for script source tags (defaults to data-turbolinks-track="reload" for backwards compatibility) + config.tinymce.default_script_attributes = { "data-turbolinks-track" => "reload" } + initializer "precompile", :group => :all do |app| if config.tinymce.install == :compile app.config.assets.precompile << "tinymce-rails.manifest.js" # Sprockets 4 manifest diff --git a/lib/tinymce/rails/helper.rb b/lib/tinymce/rails/helper.rb index 534f60a2..ec73896e 100644 --- a/lib/tinymce/rails/helper.rb +++ b/lib/tinymce/rails/helper.rb @@ -63,8 +63,8 @@ def tinymce_configuration(config=:default, options={}) end # Includes TinyMCE javascript assets via a script tag. - def tinymce_assets - javascript_include_tag "tinymce", "data-turbolinks-track" => "reload" + def tinymce_assets(options=Rails.application.config.tinymce.default_script_attributes) + javascript_include_tag("tinymce", options) end # Allow methods to be called as module functions: diff --git a/sandbox/app/views/editor/helpers.html.erb b/sandbox/app/views/editor/helpers.html.erb index 41c88b17..0c9b9261 100644 --- a/sandbox/app/views/editor/helpers.html.erb +++ b/sandbox/app/views/editor/helpers.html.erb @@ -1,5 +1,5 @@ <% content_for :javascript do %> - <%= tinymce_assets %> + <%= tinymce_assets "data-turbolinks-track": :reload %> <% end %>

TinyMCE Helpers

diff --git a/spec/helpers/helper_spec.rb b/spec/helpers/helper_spec.rb index f9536bf2..81e47865 100644 --- a/spec/helpers/helper_spec.rb +++ b/spec/helpers/helper_spec.rb @@ -23,7 +23,13 @@ module TinyMCE::Rails describe "#tinymce_assets" do it "returns a bundled TinyMCE javascript tag" do - expect(tinymce_assets).to have_selector("script[src='#{asset_path("tinymce.js")}']", visible: false) + script = tinymce_assets + expect(script).to have_selector("script[src='#{asset_path("tinymce.js")}'][data-turbolinks-track='reload']", visible: false) + end + + it "allows custom attributes to be set on the script tag" do + script = tinymce_assets(defer: true, data: { turbo_track: "reload" }) + expect(script).to have_selector("script[src='#{asset_path("tinymce.js")}'][defer][data-turbo-track='reload']", visible: false) end end