Skip to content

Commit

Permalink
Load ActionText assets statically to enable full-featured setup
Browse files Browse the repository at this point in the history
Fixes #3251, Fixes #3386
  • Loading branch information
mshibuya committed Sep 18, 2022
1 parent 3cffc00 commit 458d0fb
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@
//= require 'rails_admin/sidescroll'
//= require 'rails_admin/ui'
//= require 'rails_admin/custom/ui'

<% if defined?(ActiveStorage) %>
//= require activestorage
<% end %>
<% if defined?(ActionText) && Rails.gem_version >= Gem::Version.new('7.0') %>
//= require trix
//= require actiontext
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@

@import "rails_admin/styles/base/theming";
@import "rails_admin/custom/theming";

<% if defined?(ActionText) && Rails.gem_version >= Gem::Version.new('7.0') %>
@import "trix";
<% end %>
9 changes: 9 additions & 0 deletions app/helpers/rails_admin/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ def handle_asset_dependency_error
raise e
end

# Workaround for https://github.com/rails/rails/issues/31325
def image_tag(source, options = {})
if %w[ActiveStorage::Variant ActiveStorage::VariantWithRecord ActiveStorage::Preview].include? source.class.to_s
super main_app.route_for(ActiveStorage.resolve_model_to_route, source), options
else
super
end
end

private

def edit_user_link_label
Expand Down
3 changes: 2 additions & 1 deletion app/views/rails_admin/main/_form_action_text.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<%
js_data = {
csspath: field.css_location,
jspath: field.js_location
jspath: field.js_location,
warn_dynamic_load: field.warn_dynamic_load
}
%>
<%= form.rich_text_area field.method_name, field.html_attributes.reverse_merge(data: { options: js_data.to_json }) %>
4 changes: 4 additions & 0 deletions lib/rails_admin/config/fields/types/action_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class ActionText < Text
"https://cdnjs.cloudflare.com/ajax/libs/trix/#{version}/trix.js"
end

register_instance_option :warn_dynamic_load do
true
end

register_instance_option :partial do
:form_action_text
end
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions spec/dummy_app/app/assets/stylesheets/rails_admin.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
$fa-font-path: ".";
@import "rails_admin/src/rails_admin/styles/base.scss";
@import "trix/dist/trix";
2 changes: 2 additions & 0 deletions spec/dummy_app/app/javascript/rails_admin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import "rails_admin/src/rails_admin/base";
import "flatpickr/dist/l10n/fr.js";
import "trix";
import "@rails/actiontext";
import * as ActiveStorage from "@rails/activestorage";
ActiveStorage.start();
6 changes: 4 additions & 2 deletions spec/dummy_app/config/importmap.rails_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
pin '@hotwired/turbo', to: 'https://ga.jspm.io/npm:@hotwired/turbo@7.1.0/dist/turbo.es2017-esm.js'
pin '@hotwired/turbo-rails', to: 'https://ga.jspm.io/npm:@hotwired/turbo-rails@7.1.3/app/javascript/turbo/index.js'
pin '@popperjs/core', to: 'https://ga.jspm.io/npm:@popperjs/core@2.11.5/dist/esm/popper.js'
pin '@rails/actioncable/src', to: 'https://ga.jspm.io/npm:@rails/actioncable@7.0.2/src/index.js'
pin '@rails/actioncable/src', to: 'https://ga.jspm.io/npm:@rails/actioncable@7.0.3-1/src/index.js'
pin '@rails/actiontext', to: 'https://ga.jspm.io/npm:@rails/actiontext@7.0.3-1/app/javascript/actiontext/index.js'
pin '@rails/activestorage', to: 'https://ga.jspm.io/npm:@rails/activestorage@7.0.3-1/app/assets/javascripts/activestorage.esm.js'
pin '@rails/ujs', to: 'https://ga.jspm.io/npm:@rails/ujs@6.1.4/lib/assets/compiled/rails-ujs.js'
pin '@rails/ujs', to: 'https://ga.jspm.io/npm:@rails/ujs@7.0.3-1/lib/assets/compiled/rails-ujs.js'
pin 'bootstrap', to: 'https://ga.jspm.io/npm:bootstrap@5.1.3/dist/js/bootstrap.esm.js'
pin 'flatpickr', to: 'https://ga.jspm.io/npm:flatpickr@4.6.13/dist/flatpickr.js'
pin 'flatpickr/', to: 'https://ga.jspm.io/npm:flatpickr@4.6.13/'
pin 'jquery', to: 'https://ga.jspm.io/npm:jquery@3.6.0/dist/jquery.js'
pin 'jquery-ui/', to: 'https://ga.jspm.io/npm:jquery-ui@1.13.1/'
pin 'trix', to: 'https://ga.jspm.io/npm:trix@2.0.0-beta.0/dist/trix.js'
4 changes: 3 additions & 1 deletion spec/dummy_app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"private": true,
"version": "0.1.0",
"dependencies": {
"@rails/webpacker": "5.4.3",
"@rails/actiontext": "^7.0.3-1",
"@rails/activestorage": "^7.0.3-1",
"@rails/webpacker": "5.4.3",
"rails_admin": "file:../../",
"trix": "^2.0.0-beta.0",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12"
},
Expand Down
29 changes: 27 additions & 2 deletions spec/integration/fields/action_text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,42 @@
require 'spec_helper'

if defined?(ActionText)
RSpec.describe 'ActionText field', type: :request do
RSpec.describe 'ActionText field', type: :request, js: true do
subject { page }

it 'works without error', js: true do
before do
RailsAdmin.config FieldTest do
edit do
field :action_text_field
end
end
end

it 'works without error' do
allow(ConsoleLogger).to receive(:warn).with(/ActionText assets should be loaded statically/)
expect { visit new_path(model_name: 'field_test') }.not_to raise_error
is_expected.to have_selector('trix-toolbar')
end

if RailsAdmin.config.asset_source == :sprockets && Rails.gem_version < Gem::Version.new('7.0')
it 'shows a warning if ActionText assets are loaded dynamically' do
expect(ConsoleLogger).to receive(:warn).with(/ActionText assets should be loaded statically/)
visit new_path(model_name: 'field_test')
is_expected.to have_selector('trix-toolbar')
end

it 'allows suppressing the warning' do
RailsAdmin.config FieldTest do
edit do
field :action_text_field do
warn_dynamic_load false
end
end
end
expect(ConsoleLogger).not_to receive(:warn).with(/ActionText assets should be loaded statically/)
visit new_path(model_name: 'field_test')
is_expected.to have_selector('trix-toolbar')
end
end
end
end
5 changes: 5 additions & 0 deletions src/rails_admin/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@ import I18n from "./i18n";
return content.find("trix-editor").each(function () {
if (!window.Trix) {
options = $(this).data("options");
if (options.warn_dynamic_load) {
console.warn(
"ActionText assets should be loaded statically. Follow instructions in https://github.com/railsadminteam/rails_admin/wiki/ActionText"
);
}
$("head").append(
'<link href="' +
options["csspath"] +
Expand Down

0 comments on commit 458d0fb

Please sign in to comment.