Skip to content

Commit

Permalink
Allow passing of script attributes to tinymce_assets helper
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed Jul 10, 2024
1 parent eecd1c6 commit 9c51115
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/tinymce/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/tinymce/rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion sandbox/app/views/editor/helpers.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% content_for :javascript do %>
<%= tinymce_assets %>
<%= tinymce_assets "data-turbolinks-track": :reload %>
<% end %>

<h1>TinyMCE Helpers</h1>
Expand Down
8 changes: 7 additions & 1 deletion spec/helpers/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9c51115

Please sign in to comment.