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

Set ActiveStorage::Blob.service when ActiveStorage::Blob is loaded #30118

Merged
merged 1 commit into from
Aug 8, 2017
Merged

Set ActiveStorage::Blob.service when ActiveStorage::Blob is loaded #30118

merged 1 commit into from
Aug 8, 2017

Conversation

georgeclaghorn
Copy link
Contributor

@georgeclaghorn georgeclaghorn commented Aug 7, 2017

Fixes that ActiveStorage::Blob.service is unset when ActiveStorage::Blob is reloaded.

@rails-bot
Copy link

r? @eileencodes

(@rails-bot has picked a reviewer for you, use r? to override)

config.after_initialize do |app|
if config_choice = app.config.active_storage.service
config.to_prepare do
if config_choice = Rails.configuration.active_storage.service
config_file = Pathname.new(Rails.root.join("config/storage.yml"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use config_for for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that, but the YAML file doesn’t have top-level environment keys, so we can’t.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Maybe we could extract the file read behavior from config_for to a private API method and reuse here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m in favor of extracting a reusable method, but I’d like to do it in a separate PR. There are a few other places where we do roughly the same thing but in slightly different ways, and I’d like to consolidate them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

config.after_initialize do |app|
if config_choice = app.config.active_storage.service
config.to_prepare do
if config_choice = Rails.configuration.active_storage.service
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reading the YAML every request in development what do you think to store the configuration in the active_storage configuration object? Something like:

initializer "active_storage.services" do
  config.active_storage.service_configuration = application.config_for("config/storage.yml")
  
  config.to_prepare do
    if config_choice = Rails.configuration.active_storage.service
      ActiveStorage::Blob.service =
             begin
               ActiveStorage::Service.configure config_choice, Rails.configuration.active_storage.service_configuration
             rescue => e
               raise e, "Cannot load `Rails.config.active_storage.service`:\n#{e.message}", e.backtrace
             end
     endddd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memoized the config in caf7e62.

@rafaelfranca rafaelfranca added this to the 5.2.0 milestone Aug 8, 2017
Fixes that ActiveStorage::Blob.service is unset when ActiveStorage::Blob
is reloaded.
end
config.to_prepare do
if config_choice = Rails.configuration.active_storage.service
configs = Rails.configuration.active_storage.service_configurations ||= begin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is better to avoid the memoization and conditionally do everything, even the to_prepare hook? We could take advantage of the scope of the blocks to even avoiding have to memoize things or create a new config.

    initializer "active_storage.services" do
      if config_choice = app.config.active_storage.service
        config_file = Pathname.new(Rails.root.join("config/storage.yml"))
        raise("Couldn't find Active Storage configuration in #{config_file}") unless config_file.exist?

        require "yaml"
        require "erb"

        configs =
          begin
            YAML.load(ERB.new(config_file.read).result) || {}
          rescue Psych::SyntaxError => e
            raise "YAML syntax error occurred while parsing #{config_file}. " \
              "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
              "Error: #{e.message}"
          end

        config.to_prepare do |app|
          ActiveStorage::Blob.service =
            begin
              ActiveStorage::Service.configure config_choice, configs
            rescue => e
              raise e, "Cannot load `Rails.config.active_storage.service`:\n#{e.message}", e.backtrace
            end
        end
      end
    end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make it impossible to set Rails.configuration.active_storage.service in an initializer, no? Maybe that’s not a big deal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, good point. I think we should keep the possibility to set that in a initializer, so let's keep your implementation for now. I'll change if I think in a way to solve that problem.

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

Successfully merging this pull request may close these issues.

None yet

4 participants