Skip to content

Overriding scaffold erb templates #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
craic opened this issue Apr 20, 2022 · 5 comments
Closed

Overriding scaffold erb templates #164

craic opened this issue Apr 20, 2022 · 5 comments

Comments

@craic
Copy link

craic commented Apr 20, 2022

Rails 7.0.2.3 ruby 3.0.0p0 ubuntu

I want to modify the erb templates used in scaffolding but the tailwindcss template engine doesn't pick up custom templates from lib/templates/erb/scaffold

Does it use a different location ? I don't see an option in 'rails g scaffold --help' where I can specify that explicitly

Thanks !

@dhh
Copy link
Member

dhh commented Apr 30, 2022

You can configure which files are watched in tailwind.config.js.

@dhh dhh closed this as completed Apr 30, 2022
@mabras
Copy link

mabras commented Apr 30, 2022

You can configure which files are watched in tailwind.config.js.

Please, can you double-check your answer? It sounds to me like it has nothing to do with the issue.

We are trying to override the erb templates generated by rails g scaffold [resource].
Usually, we put the erb files in locations like:

lib/templates/erb/scaffold/*.html.erb

Also, I have tried this path:

lib/generators/tailwindcss/scaffold/templates/*.html.erb

Both are not working.

Thanks @dhh

@dhh
Copy link
Member

dhh commented May 1, 2022

Ah I read it as a watching question. This gem is already overriding the scaffold templates, so that's probably why. Would be happy to look at a PR if you find a way to make this compatible with overwritten templates.

@craic
Copy link
Author

craic commented May 3, 2022

This source_root line here is overriding the default behaviour

require 'rails/generators/erb/scaffold/scaffold_generator'
require "rails/generators/resource_helpers"
module Tailwindcss
module Generators
class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
include Rails::Generators::ResourceHelpers
source_root File.expand_path("../templates", __FILE__)

If I comment that out then I can pick up from
lib/templates/erb/scaffold/*.html.erb

Maybe an if / else clause that looks to see if the app has defined custom templates then use those, if not then use the ones in tailwindcss

Unfortunately I don't see how where the custom template path is defined in the regular generator.
If you can give me a pointer then I will try it out

@luiskhernandez
Copy link

luiskhernandez commented Aug 26, 2022

@craic Not sure if this could be merged into the gem, but overriding the class method source_root did the trick.

I added this file lib/generators/tailwindcss/scaffold/scaffold_generator.rb

require 'rails/generators/erb/scaffold/scaffold_generator'
require "rails/generators/resource_helpers"

module Tailwindcss
  module Generators
    class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator

      def self.source_root
        default_path = Rails.root.join("lib", "templates", "erb", "scaffold")
        return default_path if Dir.exists?(default_path)

        File.expand_path("../templates", __FILE__)
      end
    end
  end
end

mojodojo101 added a commit to mojodojo101/tailwindcss-rails that referenced this issue Feb 11, 2023
Current behavior:
Custom templates are ignored by the tailwindcss-rails generator 

Expected behavior:
View templates under "#{RAILS_ROOT}/lib/templates/erb/scaffold" should be prioritized by the generator.

rails#164
mojodojo101 added a commit to mojodojo101/tailwindcss-rails that referenced this issue Feb 11, 2023
Current behavior:
Custom templates are ignored by the tailwindcss-rails generator

Expected behavior:
View templates under "#{RAILS_ROOT}/lib/templates/erb/scaffold" should be prioritized by the generator.

rails#164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants