Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

## next / unreleased

### Added

- Add callbacks for `:with_tenant` which are invoked when `.with_tenant` is called. @flavorjones
- Add callbacks for `:set_current_tenant` which are invoked when `.current_tenant=` is called. @flavorjones

### Fixed

- `.current_tenant = nil` now clears the tenant context, properly setting the shard to `UNTENANTED_SENTINEL` instead of `""` @flavorjones
- `Tenanted::DiskService#path_for` now handles keys without a tenant prefix (e.g., from `ActiveStorage::FixtureSet.blob`) by falling back to standard DiskService behavior. @flavorjones


## v0.6.0 / 2025-11-05
Expand Down Expand Up @@ -59,8 +65,6 @@ Read the [Rails Guide documentation on `config.active_record.query_log_tags`](ht

### Added

- Add callbacks for `:with_tenant` which are invoked when `.with_tenant` is called.
- Add callbacks for `:set_current_tenant` which are invoked when `.current_tenant=` is called.
- `UntenantedConnectionPool#size` returns the database configuration's `max_connections` value, so that code (like Solid Queue) can inspect config params without a tenant context.


Expand Down
6 changes: 1 addition & 5 deletions lib/active_record/tenanted/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ def root
end

def path_for(key)
if ActiveRecord::Tenanted.connection_class
# TODO: this is brittle if the key isn't tenanted ... errors in folder_for:
#
# NoMethodError undefined method '[]' for nil (NoMethodError) [ key[0..1], key[2..3] ].join("/")
#
if ActiveRecord::Tenanted.connection_class && key.include?("/")
tenant, key = key.split("/", 2)
File.join(root, tenant, folder_for(key), key)
else
Expand Down
5 changes: 5 additions & 0 deletions test/integration/test/active_storage_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require "test_helper"

class TestActiveStorage < ActionDispatch::IntegrationTest
test "fixtures work with ActiveStorage::FixtureSet" do
note = notes(:one)
assert_predicate(note.image, :attached?, "Expected note fixture to have an attached image from ActiveStorage fixtures")
end

test "can upload a file" do
post(notes_path,
params: {
Expand Down
4 changes: 4 additions & 0 deletions test/smarty/test/fixtures/active_storage/attachments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
one_image:
name: image
record: one (Note)
blob: one_image_blob
1 change: 1 addition & 0 deletions test/smarty/test/fixtures/active_storage/blobs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
one_image_blob: <%= ActiveStorage::FixtureSet.blob filename: "goruco.jpg", service_name: "test" %>
13 changes: 13 additions & 0 deletions test/unit/storage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@
end
end
end

test "Disk Service path_for falls back for keys without tenant prefix" do
ActiveRecord::Tenanted.stub(:connection_class, TenantedApplicationRecord) do
TenantedApplicationRecord.create_tenant("foo") do
blob = ActiveStorage::Blob.new(filename: "foo.jpg", byte_size: 100, checksum: "abc123", service_name: service_name)

# Keys from ActiveStorage::FixtureSet don't have tenant prefix
non_tenanted_key = "abc123def456"
expected_path = "/path/to/storage/ab/c1/#{non_tenanted_key}"
assert_equal expected_path, blob.service.path_for(non_tenanted_key)
end
end
end
end
end
end
Loading