Skip to content

Commit ce674b4

Browse files
committed
make Array#cache_key work with frozen arrays
test-plan (script/console): - make an Array (ary = []) - freeze it (ary.freeze) - try and get a cache_key from it (ary.cache_key) - should not raise an error Change-Id: I2646440d7d48d2cea16140cd15c8bef56cd3e063 Reviewed-on: https://gerrit.instructure.com/18717 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Cody Cutrer <cody@instructure.com> Product-Review: Jacob Fugal <jacob@instructure.com> QA-Review: Jacob Fugal <jacob@instructure.com>
1 parent f10b0c8 commit ce674b4

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

lib/ext/array.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ def to_atom
3232
end
3333

3434
def cache_key
35-
@cache_key ||= self.collect{|element| ActiveSupport::Cache.expand_cache_key(element) }.to_param
35+
if @cache_key
36+
@cache_key
37+
else
38+
value = self.collect{|element| ActiveSupport::Cache.expand_cache_key(element) }.to_param
39+
@cache_key = value unless self.frozen?
40+
value
41+
end
3642
end
3743

3844
def once_per(&block)

spec/lib/ext/array_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Copyright (C) 2011 Instructure, Inc.
3+
#
4+
# This file is part of Canvas.
5+
#
6+
# Canvas is free software: you can redistribute it and/or modify it under
7+
# the terms of the GNU Affero General Public License as published by the Free
8+
# Software Foundation, version 3 of the License.
9+
#
10+
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
11+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12+
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13+
# details.
14+
#
15+
# You should have received a copy of the GNU Affero General Public License along
16+
# with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#
18+
19+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
20+
21+
describe "Array#cache_key" do
22+
it "should work with frozen arrays" do
23+
array = [1, 2, 3]
24+
array.freeze
25+
array.cache_key.should == '1/2/3'
26+
end
27+
end

0 commit comments

Comments
 (0)