hotwired / stimulus-rails

Use Stimulus in your Ruby on Rails app
https://stimulus.hotwired.dev
MIT License
638 stars 92 forks source link

Added config for controllers path used by generator #119

Closed wJoenn closed 1 year ago

wJoenn commented 1 year ago

I am using vite_rails in my project and because of that I don't have a app/javascript folder but a app/frontend/javascript folder. Because the path in the Stimulus generator is hardcoded every time I try to use it to generate a new controller it would recreate the app/javascript folder and add the controller there instead of inside my existing folder.

Here's a fix that would make any file structure compatible by using Dir.glob("**/javascript/controllers").first instead of "app/javascript/controllers"

I tested it locally and it's working as expected as far as I can see

wJoenn commented 1 year ago

Scrap this, it was raised to my attention that this could have conflicts if any other javascript/controllers directory exist. I'll work on a different way that would use the config file of Rails. Something like

config.generators do |generate|
  generate.stimulus_path "path/to/controllers"
end
wJoenn commented 1 year ago

In the end I created a stimulus initializer file in which I can configurate the path by doind

Stimulus.configure do |config|
  config.controllers_path = "path/to/controllers"
end

The value of Stimulus.controllers_path is then read in the generator and task to respect the user's preference

wJoenn commented 1 year ago

@dhh May I ask why ?