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

Move custom ActiveStorage service to $LOAD_PATH #5492

Merged
merged 2 commits into from
Apr 17, 2024

Conversation

javierm
Copy link
Member

@javierm javierm commented Apr 15, 2024

References

Objectives

  • Make it easier to upgrade to Rails 7.1

@javierm javierm self-assigned this Apr 15, 2024
@javierm javierm added this to Reviewing in Consul Democracy Apr 15, 2024
@taitus taitus self-assigned this Apr 16, 2024
@javierm javierm force-pushed the move_tenant_service_to_load_path branch 2 times, most recently from a81b61c to 5badeb0 Compare April 16, 2024 17:54
This way we simplify the code a little bit and we create a method unique
to the `TenantDiskService` class, which can be used to check whether
we're using this class without using `is_a?` or similar.
@javierm javierm force-pushed the move_tenant_service_to_load_path branch 3 times, most recently from cdd417c to 0ca5565 Compare April 16, 2024 21:40
We moved this file to `app/lib/` in commit cb47714 so it would be in
the autoload_paths. However, this class is loaded by ActiveStorage, with
the following method:

```
def resolve(class_name)
  require "active_storage/service/#{class_name.to_s.underscore}_service"
  ActiveStorage::Service.const_get(:"#{class_name.camelize}Service")
rescue LoadError
  raise "Missing service adapter for #{class_name.inspect}"
end
``

So this file needs to be in the $LOAD_PATH, or else ActiveStorage won't
be able to load it when we disable the `add_autoload_paths_to_load_path`
option, which is the default in Rails 7.1 [1].

Moving it to the `lib` folder solves the issue; as mentioned in the
guide to upgrade to Rails 7.1 [2]:

> The lib directory is not affected by this flag, it is added to
> $LOAD_PATH always.

However, we were also referencing this class in the `Tenant` model,
meaning we needed to autoload it as well somehow. So, instead of
directly referencing this class, we're using `respond_to?` in the Tenant
model.

We're changing the test so it fails when the code calls
`is_a?(ActiveStorage::Service::TenantDiskService)`. We need to change
the active storage configurations in the test because, otherwise, the
moment `ActiveStorage::Blob` is loaded, the `TenantDiskService` class is
also loaded, meaning the test will pass when using `is_a?`.

Note that, since this class isn't in the autoload paths anymore, we need
to add a `require` in the tests. We could add an initializer to require
it; we're not doing it in order to be consistent with what ActiveStorage
does: it only loads the service that's going to be used in the current
Rails environment. If somebody changed their production environment in
order to use (for example), S3, and we added an initializer to require
the TenantDiskService, we would still load the TenantDiskService even if
it isn't going to be used.

[1] https://guides.rubyonrails.org/v7.1/configuring.html#config-add-autoload-paths-to-load-path
[2] https://guides.rubyonrails.org/v7.1/upgrading_ruby_on_rails.html#autoloaded-paths-are-no-longer-in-$load-path
@javierm javierm force-pushed the move_tenant_service_to_load_path branch from 0ca5565 to 118c2bf Compare April 17, 2024 13:21
Consul Democracy automation moved this from Reviewing to Testing Apr 17, 2024
@javierm javierm merged commit 09321f4 into master Apr 17, 2024
13 checks passed
Consul Democracy automation moved this from Testing to Release 2.2.0 Apr 17, 2024
@javierm javierm deleted the move_tenant_service_to_load_path branch April 17, 2024 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Consul Democracy
  
Release 2.2.0
Development

Successfully merging this pull request may close these issues.

None yet

2 participants