Skip to content

Commit fd7e0c4

Browse files
committed
fix: allow applications to override scaffold templates
Note that developers can override by placing templates in either: - lib/templates/tailwindcss/scaffold - lib/templates/erb/scaffold
1 parent 2a89155 commit fd7e0c4

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

Diff for: CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
## next / unreleased
22

33
* Update to [Tailwind CSS v3.4.1](https://github.com/tailwindlabs/tailwindcss/releases/tag/v3.4.1) from v3.4.0 by @flavorjones
4-
* Fix `password` form field styling in generated scaffold forms. (#304, #307) @flavorjones
5-
* Fix namespaced mailer generation. (#272, #308) @flavorjones
4+
* Fix `password` form field styling in generated scaffold forms. (#304) @flavorjones
5+
* Fix namespaced mailer generation. (#272) @flavorjones
6+
* Allow overriding the scaffold generator templates by placing application templates in either `lib/templates/tailwindcss/scaffold` or `lib/templates/erb/scaffold`. (#164) @flavorjones
67

78

89
## v2.2.0 / 2023-01-04

Diff for: lib/generators/tailwindcss/scaffold/scaffold_generator.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
77
include Rails::Generators::ResourceHelpers
88

99
source_root File.expand_path("templates", __dir__)
10+
source_paths << "lib/templates/erb/scaffold"
1011

1112
argument :attributes, type: :array, default: [], banner: "field:type field:type"
1213

Diff for: test/lib/generators/tailwindcss/scaffold_generator_test.rb

+24
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,28 @@ class Tailwindcss::Generators::ScaffoldGeneratorTest < Rails::Generators::TestCa
2222
assert_file "app/views/admin/roles/#{view}.html.erb"
2323
end
2424
end
25+
26+
[
27+
"lib/templates/tailwindcss/scaffold",
28+
"lib/templates/erb/scaffold",
29+
].each do |templates_path|
30+
test "overriding generator templates in #{templates_path}" do
31+
override_dir = File.join(destination_root, templates_path)
32+
FileUtils.mkdir_p override_dir
33+
File.open(File.join(override_dir, "index.html.erb"), "w") { |f| f.puts "This is a custom template" }
34+
35+
# change directory because the generator adds a relative path to source_paths
36+
Dir.chdir(destination_root) do
37+
run_generator
38+
end
39+
40+
%w(edit new show _form _message).each do |view|
41+
assert_file "app/views/messages/#{view}.html.erb"
42+
end
43+
44+
assert_file "app/views/messages/index.html.erb" do |body|
45+
assert_match("This is a custom template", body, "index custom template should be used")
46+
end
47+
end
48+
end
2549
end

0 commit comments

Comments
 (0)