Skip to content

Commit 78b4a77

Browse files
Import actiontext.css when actiontext is installed
When a Rails app is generated with the --css option and the action_text:install task is run, the Trix editor will not work as expected. This occurs because using cssbundling-rails does not create an application.css file, which would normally automatically require actiontext.css. Adding a step to append an import statement to the base CSS or SCSS file, when one can be detected, solves the issue. In the case a base CSS or SCSS file can't be detected, we output a warning to import the necessary CSS file.
1 parent f2be2b0 commit 78b4a77

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

actiontext/lib/generators/action_text/install/install_generator.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,20 @@ def append_javascript_dependencies
3232
end
3333

3434
def create_actiontext_files
35+
destination = Pathname(destination_root)
36+
3537
template "actiontext.css", "app/assets/stylesheets/actiontext.css"
3638

39+
unless destination.join("app/assets/application.css").exist?
40+
if (stylesheets = Dir.glob "#{destination_root}/app/assets/stylesheets/application.*.{scss,css}").length > 0
41+
insert_into_file stylesheets.first.to_s, %(@import 'actiontext.css';)
42+
else
43+
say <<~INSTRUCTIONS, :green
44+
To use the Trix editor, you must require 'app/assets/stylesheets/actiontext.css' in your base stylesheet.
45+
INSTRUCTIONS
46+
end
47+
end
48+
3749
gem_root = "#{__dir__}/../../../.."
3850

3951
copy_file "#{gem_root}/app/views/active_storage/blobs/_blob.html.erb",

railties/test/generators/action_text_install_generator_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class ActionText::Generators::InstallGeneratorTest < Rails::Generators::TestCase
1313
FileUtils.mkdir_p("#{destination_root}/app/javascript")
1414
FileUtils.touch("#{destination_root}/app/javascript/application.js")
1515

16+
FileUtils.mkdir_p("#{destination_root}/app/assets/stylesheets")
17+
1618
FileUtils.mkdir_p("#{destination_root}/config")
1719
FileUtils.touch("#{destination_root}/config/importmap.rb")
1820
end
@@ -56,6 +58,32 @@ class ActionText::Generators::InstallGeneratorTest < Rails::Generators::TestCase
5658
assert_file "app/assets/stylesheets/actiontext.css"
5759
end
5860

61+
test "appends @import 'actiontext.css' to base scss file" do
62+
FileUtils.touch("#{destination_root}/app/assets/stylesheets/application.bootstrap.scss")
63+
64+
run_generator_instance
65+
66+
assert_file "app/assets/stylesheets/application.bootstrap.scss" do |content|
67+
assert_match "@import 'actiontext.css';", content
68+
end
69+
end
70+
71+
72+
test "appends @import 'actiontext.css'; to base css file" do
73+
FileUtils.touch("#{destination_root}/app/assets/stylesheets/application.postcss.css")
74+
75+
run_generator_instance
76+
77+
assert_file "app/assets/stylesheets/application.postcss.css" do |content|
78+
assert_match "@import 'actiontext.css';", content
79+
end
80+
end
81+
82+
test "throws a warning for missing base (s)css file" do
83+
assert_match "To use the Trix editor, you must require 'app/assets/stylesheets/actiontext.css' in your base stylesheet.",
84+
run_generator_instance
85+
end
86+
5987
test "creates Active Storage view partial" do
6088
run_generator_instance
6189
assert_file "app/views/active_storage/blobs/_blob.html.erb"

0 commit comments

Comments
 (0)