Skip to content

Commit

Permalink
Ensure that preference keys are not prefixed with '/' if ENV['RAILS_C…
Browse files Browse the repository at this point in the history
…ACHE_KEY'] is not set

Fixes spree#3831
  • Loading branch information
radar committed Oct 11, 2013
1 parent 7996428 commit 1730926
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 5 additions & 1 deletion core/app/models/spree/preferences/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def configure
end

def preference_cache_key(name)
[ENV['RAILS_CACHE_ID'], self.class.name, name].flatten.join('::').underscore
[rails_cache_id, self.class.name, name].compact.join('::').underscore
end

def rails_cache_id
ENV['RAILS_CACHE_ID']
end

def reset
Expand Down
6 changes: 5 additions & 1 deletion core/app/models/spree/preferences/preferable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ def prefers?(name)

def preference_cache_key(name)
return unless id
[ENV["RAILS_CACHE_ID"], self.class.name, name, id].join('::').underscore
[rails_cache_id, self.class.name, name, id].compact.join('::').underscore
end

def rails_cache_id
ENV['RAILS_CACHE_ID']
end

def save_pending_preferences
Expand Down
17 changes: 17 additions & 0 deletions core/spec/models/spree/preferences/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ class AppConfig < Spree::Preferences::Configuration
@config = AppConfig.new
end

# Regression test for #3831
context "with a rails cache id set" do
before do
@config.stub :rails_cache_id => "cache"
end

it "can access the preference cache key" do
expect(@config.preference_cache_key("foo")).to eql("cache/app_config/foo")
end
end

context "with no rails cache id set" do
it "does not prefix the preference cache key with a slash" do
expect(@config.preference_cache_key("foo")).to eql("app_config/foo")
end
end

it "has named methods to access preferences" do
@config.color = 'orange'
@config.color.should eq 'orange'
Expand Down
20 changes: 20 additions & 0 deletions core/spec/models/spree/preferences/preferable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ class B < A
store.persistence = true
end

# Regression test for #3831
describe "preference cache key" do
context "with a rails_cache_id set" do
before do
@a.stub(:rails_cache_id => 'cache')
end

it "includes the cache id within the key" do
expect(@a.preference_cache_key('foo')).to eql("cache/a/foo/#{@a.id}")
end
end

context "without a rails_cache_id set" do
it "includes the cache id within the key" do
expect(@a.preference_cache_key('foo')).to eql("a/foo/#{@a.id}")
end
end

end

describe "preference definitions" do
it "parent should not see child definitions" do
@a.has_preference?(:color).should be_true
Expand Down

0 comments on commit 1730926

Please sign in to comment.