Skip to content

Commit 2fa54b2

Browse files
Change cache_key generation for TenantCommon (#188)
* change cache_key generation for TenantCommon * refactor: simplify cache_key method in TenantCommon * Tweak the CHANGELOG entry. --------- Co-authored-by: Mike Dalessio <mike@37signals.com>
1 parent 2454618 commit 2fa54b2

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## next / unreleased
44

5+
### Changed
6+
7+
- The return value from an Active Record model `#cache_key` has changed from `users/1?tenant=foo` to `foo/users/1`. For existing applications, this will invalidate any relevant cache entries. #187 @miguelmarcondesf
8+
9+
510
### Improved
611

712
- For tenanted model instances, `#inspect` now outputs the tenant attribute first, before the column attributes. #191 @lairtonmendes

lib/active_record/tenanted/tenant.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module TenantCommon # :nodoc:
1313
end
1414

1515
def cache_key
16-
tenant ? "#{super}?tenant=#{tenant}" : super
16+
tenant ? "#{tenant}/#{super}" : super
1717
end
1818

1919
def inspect

test/unit/tenant_test.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,17 @@
11661166
User.create!(email: "user1@example.org")
11671167
end
11681168

1169-
assert_equal("users/1?tenant=foo", user.cache_key)
1169+
assert_equal("foo/users/1", user.cache_key)
11701170

11711171
TenantedApplicationRecord.with_tenant("foo") do
1172-
assert_equal("users/1?tenant=foo", User.find(user.id).cache_key)
1172+
assert_equal("foo/users/1", User.find(user.id).cache_key)
1173+
end
1174+
end
1175+
1176+
test "handles special characters in tenant names" do
1177+
TenantedApplicationRecord.create_tenant("foo-bar_123") do
1178+
user = User.create!(email: "user1@example.org")
1179+
assert_equal("foo-bar_123/users/1", user.cache_key)
11731180
end
11741181
end
11751182
end

0 commit comments

Comments
 (0)