-
Notifications
You must be signed in to change notification settings - Fork 93
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
Support js_root generator option for controller and manifest tasks. #152
base: main
Are you sure you want to change the base?
Conversation
lib/tasks/stimulus_tasks.rake
Outdated
@@ -7,6 +7,11 @@ module Stimulus | |||
system RbConfig.ruby, "./bin/rails", "app:template", "LOCATION=#{File.expand_path("../install/#{path}.rb", __dir__)}" | |||
end | |||
|
|||
def controllers_path | |||
js_root = Rails.application.config.generators.options.dig(:stimulus, :js_root) || "app/javascript" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't js_root always be set, given that you have a default for the class_option? If so, I don't think we need to repeat the default here. And in fact, I'd just inline the entire call in the Rails.root.join.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love the idea of using the class_option
, however the value in Rails.application.config.generators
is not set on the class without instantiating an instance with the options. I actually quite like this approach as the default is only in one place:
def controllers_path
Rails.root.join(StimulusGenerator.new(['stimulus'], generator_options, {}).options.js_root, "controllers")
end
def generator_options
Rails.application.config.generators.options.dig(:stimulus)
end
I also had to add the following require statement to load StimulusGenerator.
# stimulus_tasks.rake
require "generators/stimulus/stimulus_generator"
# This worked in the rake task, but perhaps more appropriate in stimulus_generator.rb
# Let me know if its better to have them in the rake task instead of the generator.
require 'rails/generators'
require 'rails/generators/actions'
require "rails/generators/named_base" # already present
This also eliminates needing to add :environment
to the rake tasks for a reason I do not fully grok, but I'm totally ok with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed up this change. LMK what you think.
However this pans out, thanks for the code review 👍
@@ -4,10 +4,11 @@ class StimulusGenerator < Rails::Generators::NamedBase # :nodoc: | |||
source_root File.expand_path("templates", __dir__) | |||
|
|||
class_option :skip_manifest, type: :boolean, default: false, desc: "Don't update the stimulus manifest" | |||
class_option :js_root, type: :string, default: 'app/javascript', desc: "Root path for javascript files" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always use " instead of ' for strings.
484cc45
to
66187f7
Compare
This PR aims to support paths other than
app/javascript
for the root path in generator and manifest tasks. Similar attempts have been made/requested, but this PR usesconfig.generators
instead of an initializer to setjs_root
. A small drawback is the manifest tasks load:environment
to access configuration fromapplication.rb
, but it plays nicely with generator help message and doesn't pollute theStimulus
namespace. Here's the usage:I like using the generator because it keeps the controller consistent with the
// Connects to data-controller=..
comment and naming conventions which get tricky with camelCase, under_score and dash-erized in play. Because the path is hard coded in the generator, the only option is to monkey patch it. Monkey patching the generator in my project made it difficult to reference the template from inside the gem requiring me to copy template as well.If there's support for this option I'd be happy to proceed with updating docs and tests, although I had a pretty tough time getting the suite to run locally. Also open to suggestions for the name
js_root
and if we should respect this configuration for thestimulus:install
tasks, but didn't want to make changes bigger without a green light.Thank you for considering this PR.