Skip to content

Instantly share code, notes, and snippets.

@mostlyobvious
Created July 23, 2025 11:02
Show Gist options
  • Select an option

  • Save mostlyobvious/c320220a5aa5927575f6277f2026b6b8 to your computer and use it in GitHub Desktop.

Select an option

Save mostlyobvious/c320220a5aa5927575f6277f2026b6b8 to your computer and use it in GitHub Desktop.
commit 01e41f9dbada0981ad6cac44c000ae56cc0508a8
Author: Paweł Pacana <pawel.pacana@gmail.com>
Date: Wed Jul 23 12:49:56 2025 +0200
Respect Shrine design decisions
In https://shrinerb.com/docs/design we read that:
> Each Shrine subclass has its own copy of the core classes,
> storages and options, which makes it possible to customize
> attachment logic per uploader.
This feature also means that, when we eager load the code:
* `app/uploaders/*.rb` loaded eagerly set their storage
based on `config/initializers/shrine.rb`
* changing `Shrine.storages` after the code loaded will not influence
existing uploaders
To accommodate Shrine design with eagerly loaded test code we may
either:
* conditionally load storages in initializers based on the environment
(still before loading `app/uploaders` code)
* split setting storages into respective `config/environment/*.rb`
files (still before loading `app/uploaders` code)
* modify loaded uploaders, that by design have copies of Shrine parent
and changing Shrine.storages does not influence them
The latter was chosen here.
diff --git spec/support/shrine.rb spec/support/shrine.rb
index 212146432..3963893b4 100644
--- spec/support/shrine.rb
+++ spec/support/shrine.rb
@@ -2,7 +2,12 @@
require 'shrine/storage/memory'
-Shrine.storages = {
- cache: Shrine::Storage::Memory.new,
- store: Shrine::Storage::Memory.new,
-}
+storages =
+ {
+ cache: Shrine::Storage::Memory.new,
+ store: Shrine::Storage::Memory.new,
+ }
+
+[Shrine, *Shrine.subclasses].each do |uploader|
+ uploader.storages = storages
+end
commit 9fee56e7f63ccbeda910561bf355602f587bb6b1
Author: Paweł Pacana <pawel.pacana@gmail.com>
Date: Wed Jul 23 12:39:57 2025 +0200
Enable eager code loading on CI to illustrate problem with Shrine setup
diff --git config/environments/test.rb config/environments/test.rb
index 8f61890aa..fc0fbf254 100644
--- config/environments/test.rb
+++ config/environments/test.rb
@@ -10,7 +10,7 @@ require 'active_support/core_ext/integer/time'
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
- config.cache_classes = false
+ config.cache_classes = ENV['CI'].present?
config.action_view.cache_template_loading = true
config.active_job.queue_adapter = :test
@@ -18,7 +18,7 @@ Rails.application.configure do
# Eager loading loads your whole application. When running a single test locally,
# this probably isn't necessary. It's a good idea to do in a continuous integration
# system, or in some way before deploying your code.
- config.eager_load = false # ENV['CI'].present?
+ config.eager_load = ENV['CI'].present?
# Attempt to read encrypted secrets from `config/secrets.yml.enc`.
# Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or
commit 859c50106bbeaae3dfe795afc2afbde92c86f22a
Author: Paweł Pacana <pawel.pacana@gmail.com>
Date: Wed Jul 23 12:33:54 2025 +0200
Duplicate and loaded too early
* `rails_helper.rb` first loads `spec_helper.rb` then entire rails environment
- `Shrine.storages` get overwritten by the ones from `config/initializers/shrine.rb`
* `spec/support/shrine.rb` duplicates this and loads _after_ Rails has
loaded, so after the initializers, which is desired in this context
- there's one catch, that will be revealed in subsequent commits
diff --git spec/spec_helper.rb spec/spec_helper.rb
index 209867ffb..661c72391 100644
--- spec/spec_helper.rb
+++ spec/spec_helper.rb
@@ -28,12 +28,6 @@ WebMock.disable_net_connect!(
allow: ['chromedriver.storage.googleapis.com']
)
-require 'shrine/storage/memory'
-Shrine.storages = {
- cache: Shrine::Storage::Memory.new,
- store: Shrine::Storage::Memory.new,
-}
-
# stub everything
Aws.config[:stub_responses] = true
s3 = Aws::S3::Client.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment