Skip to content

Commit

Permalink
Merge pull request #2064 from elight/3-0-stable
Browse files Browse the repository at this point in the history
Backports cache_key fix from master
  • Loading branch information
spastorino committed Jul 16, 2011
2 parents 9a4d2b2 + b1b5d18 commit 247a50b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -1497,7 +1497,8 @@ def cache_key
when new_record?
"#{self.class.model_name.cache_key}/new"
when timestamp = self[:updated_at]
"#{self.class.model_name.cache_key}/#{id}-#{timestamp.to_s(:number)}"
timestamp = timestamp.utc.to_s(:number)
"#{self.class.model_name.cache_key}/#{id}-#{timestamp}"
else
"#{self.class.model_name.cache_key}/#{id}"
end
Expand Down
37 changes: 37 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -1533,6 +1533,43 @@ def test_default_scope_is_reset
Object.class_eval{ remove_const :UnloadablePost } if defined?(UnloadablePost)
end

def test_cache_key_for_existing_record_is_not_timezone_dependent
ActiveRecord::Base.time_zone_aware_attributes = true

Time.zone = "UTC"
utc_key = Developer.find(1).cache_key

Time.zone = "EST"
est_key = Developer.find(1).cache_key

assert_equal utc_key, est_key
end

def test_cache_key_for_existing_record_is_not_timezone_dependent
ActiveRecord::Base.time_zone_aware_attributes = true

Time.zone = "UTC"
utc_key = Developer.first.cache_key

Time.zone = "EST"
est_key = Developer.first.cache_key

assert_equal utc_key, est_key
ensure
ActiveRecord::Base.time_zone_aware_attributes = false
end

def test_cache_key_format_for_existing_record_with_updated_at
dev = Developer.first
assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:number)}", dev.cache_key
end

def test_cache_key_format_for_existing_record_with_nil_updated_at
dev = Developer.first
dev.update_attribute(:updated_at, nil)
assert_match /\/#{dev.id}$/, dev.cache_key
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
Expand Down

0 comments on commit 247a50b

Please sign in to comment.