Skip to content

Put configuration in an initializer #345

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
dv opened this issue Aug 28, 2015 · 3 comments
Closed

Put configuration in an initializer #345

dv opened this issue Aug 28, 2015 · 3 comments

Comments

@dv
Copy link
Contributor

dv commented Aug 28, 2015

I like to keep my configuration as separated into relevant files as possible, that's why I would love to be able to have an /config/initializers/react.rb file containing the react-rails configuration options.

However, due to the asset_paths being added in the config.before_initialize hook of the railtie the addon option is ignored if it's set in an initializer instead of in application.rb.

Is it possible to move this asset path generation to the after_initialize hook, like the other stuff? I'm assuming it's not since you made a deliberate split of what goes into before and after, but I figured I'd make sure there is a reason before I start spending time maybe doing a PR to fix it :-)

@rmosolgo
Copy link
Member

Hey, great question, thanks for checking in. We used before_initialize because after_initialize failed. Here's the comment thread: #254 (comment)

I think that's the only reason it's in before_initialize, though, so if you can find a better solution that doesn't go 💥, that would be great!

I definitely agree that supporting initializer files would be a big improvement!

@dv
Copy link
Contributor Author

dv commented Aug 31, 2015

Hey @rmosolgo

Thanks for the reference! This helps me a great deal. It doesn't strike me as problematic that Sprockets freezes its environment in after_initialize, particularly because a default Rails 4 app has an initializer called assets.rb which has this example line in, commented out:

# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path

So it should be possible to edit the asset_path in an initializer still. I went looking at all the different lifecycle events and discovered that between the before_initialize and after_initialize of a railtie (aka bootstrap_hook and finisher_hook) are a bunch of other events we can hook into as well, that are run before or after the initializer files are run.

I think three options look good for us:

  • An after-hook for load_config_initializers: Loads all Ruby files from config/initializers in the application, railties and engines.
  • A hook for engines_blank_point: Provides a point-in-initialization to hook into if you wish to do anything before engines are loaded. After this point, all railtie and engine initializers are run.
  • A before-hook for finisher_hook.

That last one would be the absolute last point we could add the asset paths, after which Sprocket's after_initialize is run and the array is frozen.

I'll work on some code and get back to you :)

@dv
Copy link
Contributor Author

dv commented Aug 31, 2015

Turns out that a before-hook for finisher_hook already was too late. I went with an after-hook for engines_blank_point, which I thought was the most appropriate.

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

2 participants