Skip to content

Commit

Permalink
fix: allow applications to override templates
Browse files Browse the repository at this point in the history
Note that developers can override by placing templates in either:

- lib/templates/tailwindcss/{scaffold,mailer,controller}
- lib/templates/erb/{scaffold,mailer,controller}
  • Loading branch information
flavorjones committed Jan 8, 2024
1 parent 8f0cd50 commit 85c2c4a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Update to [Tailwind CSS v3.4.1](https://github.com/tailwindlabs/tailwindcss/releases/tag/v3.4.1) from v3.4.0 by @flavorjones
* Fix `password` form field styling in generated scaffold forms. (#304, #307) @flavorjones
* Fix namespaced mailer generation. (#272, #308) @flavorjones
* Allow overriding the generator templates by placing application templates in either `lib/templates/tailwindcss/{scaffold,mailer,controller}` or `lib/templates/erb/{scaffold,mailer,controller}`. (#164, #314) @flavorjones


## v2.2.0 / 2023-01-04
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Tailwindcss
module Generators
class ControllerGenerator < Erb::Generators::ControllerGenerator
source_root File.expand_path("templates", __dir__)
source_paths << "lib/templates/erb/controller"
end
end
end
1 change: 1 addition & 0 deletions lib/generators/tailwindcss/mailer/mailer_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Tailwindcss
module Generators
class MailerGenerator < Erb::Generators::MailerGenerator
source_root File.expand_path("templates", __dir__)
source_paths << "lib/templates/erb/mailer"
end
end
end
1 change: 1 addition & 0 deletions lib/generators/tailwindcss/scaffold/scaffold_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
include Rails::Generators::ResourceHelpers

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

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

Expand Down
37 changes: 34 additions & 3 deletions test/lib/generators/tailwindcss/controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,39 @@ class Tailwindcss::Generators::ControllerGeneratorTest < Rails::Generators::Test

test "generates correct view templates" do
run_generator
assert_file "app/views/messages/index.html.erb"
assert_file "app/views/messages/show.html.erb"

["index", "show"].each do |view|
assert_file "app/views/messages/#{view}.html.erb"
end
end

test "generates correct view templates when namespaced" do
run_generator ["admin/messages", "index", "show"]

["index", "show"].each do |view|
assert_file "app/views/admin/messages/#{view}.html.erb"
end
end
end

[
"lib/templates/erb/controller",
"lib/templates/tailwindcss/controller",
].each do |templates_path|
test "overriding generator templates in #{templates_path}" do
override_dir = File.join(destination_root, templates_path)
FileUtils.mkdir_p override_dir
File.open(File.join(override_dir, "view.html.erb"), "w") { |f| f.puts "This is a custom template" }

# change directory because the generator adds a relative path to source_paths
Dir.chdir(destination_root) do
run_generator
end

["index", "show"].each do |view|
assert_file "app/views/messages/#{view}.html.erb" do |body|
assert_match("This is a custom template", body, "index custom template should be used")
end
end
end
end
end
26 changes: 25 additions & 1 deletion test/lib/generators/tailwindcss/mailer_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,29 @@ class Tailwindcss::Generators::MailerGeneratorTest < Rails::Generators::TestCase
assert_match("<%= yield %>", view)
end
end
end

[
"lib/templates/erb/mailer",
"lib/templates/tailwindcss/mailer",
].each do |templates_path|
test "overriding generator templates in #{templates_path}" do
override_dir = File.join(destination_root, templates_path)
FileUtils.mkdir_p override_dir
File.open(File.join(override_dir, "view.html.erb"), "w") { |f| f.puts "This is a custom template" }
File.open(File.join(override_dir, "layout.html.erb"), "w") { |f| f.puts "This is a custom layout" }

# change directory because the generator adds a relative path to source_paths
Dir.chdir(destination_root) do
run_generator
end

assert_file "app/views/notifications_mailer/invoice.html.erb" do |view|
assert_match("This is a custom template", view)
end

assert_file "app/views/layouts/mailer.html.erb" do |view|
assert_match("This is a custom layout", view)
end
end
end
end
24 changes: 24 additions & 0 deletions test/lib/generators/tailwindcss/scaffold_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,28 @@ class Tailwindcss::Generators::ScaffoldGeneratorTest < Rails::Generators::TestCa
assert_file "app/views/admin/roles/#{view}.html.erb"
end
end

[
"lib/templates/tailwindcss/scaffold",
"lib/templates/erb/scaffold",
].each do |templates_path|
test "overriding generator templates in #{templates_path}" do
override_dir = File.join(destination_root, templates_path)
FileUtils.mkdir_p override_dir
File.open(File.join(override_dir, "index.html.erb"), "w") { |f| f.puts "This is a custom template" }

# change directory because the generator adds a relative path to source_paths
Dir.chdir(destination_root) do
run_generator
end

%w(edit new show _form _message).each do |view|
assert_file "app/views/messages/#{view}.html.erb"
end

assert_file "app/views/messages/index.html.erb" do |body|
assert_match("This is a custom template", body, "index custom template should be used")
end
end
end
end

0 comments on commit 85c2c4a

Please sign in to comment.