Skip to content

Commit 1336127

Browse files
committed
added check_collection utility method to NamespacedEnvCache
1 parent b85fb06 commit 1336127

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

lib/namespaced_env_cache.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ def persistent(name, **opts, &block)
8080
# @param opts [Hash] options hash - any unlisted options will be passed to the underlying cache
8181
# @option opts [Boolean] :include_community whether to include the community ID in the cache key
8282
def write_collection(name, value, **opts)
83-
types = value.map(&:class).uniq
84-
if types.size > 1
85-
raise TypeError, "Can't cache more than one type of object via write_collection"
86-
end
83+
check_collection(value)
8784

8885
data = NamespacedEnvCache.normalize_collection(value)
8986
namespaced = construct_ns_key(name, include_community: include_community(opts))
@@ -152,6 +149,15 @@ def self.supports_cache_versioning?
152149

153150
private
154151

152+
# Raises an error if a given collection is not cacheable
153+
# @param collection [ActiveRecord::Relation] collection to check
154+
def check_collection(collection)
155+
types = collection.map(&:class).uniq
156+
if types.size > 1
157+
raise TypeError, "Can't cache more than one type of object via write_collection"
158+
end
159+
end
160+
155161
def construct_ns_key(key, include_community: true)
156162
key = expanded_key(key)
157163
c_id = RequestContext.community_id if include_community

0 commit comments

Comments
 (0)