Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jhirn
Copy link

@jhirn jhirn commented Feb 3, 2025

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 uses config.generators instead of an initializer to set js_root. A small drawback is the manifest tasks load :environment to access configuration from application.rb, but it plays nicely with generator help message and doesn't pollute the Stimulus namespace. Here's the usage:

module Rails4Lyfe
  class Application < Rails::Application
    ...

    config.generators do |g|
      ...
      g.stimulus          :stimulus, skip_manifest: true, js_root: 'app/frontend'
      ...
    end
  end
end

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 the stimulus:install tasks, but didn't want to make changes bigger without a green light.

Thank you for considering this PR.

@@ -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"
Copy link
Member

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.

Copy link
Author

@jhirn jhirn Feb 4, 2025

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.

Copy link
Author

@jhirn jhirn Feb 4, 2025

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"
Copy link
Member

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.

@jhirn jhirn force-pushed the stimulus-generator-path-option branch from 484cc45 to 66187f7 Compare February 4, 2025 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants