forked from rails/kredis
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support scoping attribute keys with a generated prefix
It's common for models to belong a parent model, such as a user. We can now scope the attribute keys to the parent model, so that the keys can be easily identified and deleted in case of a user deletion.
- Loading branch information
Showing
3 changed files
with
119 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class Identity | ||
def id | ||
1 | ||
end | ||
end | ||
|
||
class Person | ||
include Kredis::Attributes | ||
|
||
kredis_list :names_with_scope, scope: :identity | ||
kredis_list :names_with_scope_and_key, scope: :identity, key: ->(person) { "custom_key_#{person.example_method}" } | ||
|
||
def identity | ||
Identity.new | ||
end | ||
|
||
def example_method | ||
"example" | ||
end | ||
end | ||
|
||
class Family | ||
include Kredis::Attributes | ||
|
||
kredis_list :members | ||
kredis_list :pets, key: "pets" | ||
|
||
def id | ||
1 | ||
end | ||
end | ||
|
||
|
||
class ScopeTest < ActiveSupport::TestCase | ||
setup { @person = Person.new } | ||
|
||
test "key is scoped" do | ||
assert_equal @person.names_with_scope.key, "identities:1:people:names_with_scope" | ||
end | ||
|
||
test "key is scoped and has custom key component" do | ||
assert_equal @person.names_with_scope_and_key.key, "identities:1:custom_key_example" | ||
end | ||
|
||
test "custom key" do | ||
assert_equal Family.new.pets.key, "pets" | ||
end | ||
|
||
test "key without scope" do | ||
assert_equal Family.new.members.key, "families:1:members" | ||
end | ||
end |