Skip to content

Commit

Permalink
Merge in mongodb#1387, move functionality into document from timestamps.
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Nov 27, 2011
1 parent 8b03d26 commit 94759ff
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 65 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ For instructions on upgrading to newer versions, visit
* \#1388 `model.add_to_set` now supports adding multiple values and performs an
$addToSet with $each under the covers. (Christian Felder)

* \#1387 Added `Model#cacheKey` for use in Rails caching. (Seivan Heidari)

### Resolved Issues

* \#1449 New documents no longer get persisted when replaced on a has one as
Expand Down
33 changes: 33 additions & 0 deletions lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@ def becomes(klass)
end
end

# Print out the cache key. This will append different values on the
# plural model name.
#
# If new_record? - will append /new
# If not - will append /id-updated_at.to_s(:number)
# Without updated_at - will append /id
#
# This is usually called insode a cache() block
#
# @example Returns the cache key
# document.cache_key
#
# @return [ String ] the string with or without updated_at
#
# @since 2.4.0
def cache_key
return "#{model_key}/new" if new_record?
return "#{model_key}/#{id}-#{updated_at.utc.to_s(:number)}" if updated_at
"#{model_key}/#{id}"
end

private

# Returns the logger
Expand All @@ -200,6 +221,18 @@ def logger
Mongoid.logger
end

# Get the name of the model used in caching.
#
# @example Get the model key.
# model.model_key
#
# @return [ String ] The model key.
#
# @since 2.4.0
def model_key
@model_cache_key ||= "#{self.class.model_name.cache_key}"
end

# Implement this for calls to flatten on array.
#
# @example Get the document as an array.
Expand Down
23 changes: 0 additions & 23 deletions lib/mongoid/timestamps/updated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,6 @@ module Updated
def set_updated_at
self.updated_at = Time.now.utc unless updated_at_changed?
end

# Print out the cache key.
# Will append different values on the plural model name
# If new_record? - will append /new
# If not - will append /id-updated_at.to_s(:number)
# Without updated_at - will append /id

# This is usually called insode a cache() block
#
# @example Returns the cache key
# @return [String] the string with or without updated_at
def cache_key
case
when new_record?
"#{self.class.model_name.cache_key}/new"
when timestamp = self[:updated_at]
timestamp = timestamp.utc.to_s(:number)
"#{self.class.model_name.cache_key}/#{id}-#{timestamp}"
else
"#{self.class.model_name.cache_key}/#{id}"
end
end

end
end
end
43 changes: 43 additions & 0 deletions spec/unit/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,49 @@
end
end

describe "#cache_key" do

let(:person) do
Person.new
end

context "when the document is new" do

it "should have a new key name" do
person.cache_key.should eq("people/new")
end
end

context "when persisted" do

before do
person.save
end

context "with updated_at" do

let!(:updated_at) do
person.updated_at.utc.to_s(:number)
end

it "should have the id and updated_at key name" do
person.cache_key.should eq("people/#{person.id}-#{updated_at}")
end
end

context "without updated_at" do

before do
person.updated_at = nil
end

it "should have the id key name" do
person.cache_key.should eq("people/#{person.id}")
end
end
end
end

describe "#hash" do

let(:person) do
Expand Down
42 changes: 0 additions & 42 deletions spec/unit/mongoid/timestamps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,46 +82,4 @@
person.updated_at.should be_within(10).of(Time.now.utc)
end
end

describe "#cache_key" do
let(:person) do
Person.new
end

let(:fields) do
Person.fields
end

context "when the document is new" do
it "should have a new key name" do
person.cache_key.should eq "people/new"
end
end

context "when persisted" do

before do
person.save
end

context "with updated_at" do
it "should have the id and updated_at key name" do
updated_at = person.updated_at.utc.to_s(:number)
person.cache_key.should eq "people/#{person.id}-#{updated_at}"
end
end

context "without updated_at" do
it "should have the id key name" do
person.updated_at = nil
person.cache_key.should eq "people/#{person.id}"
end
end


end


end

end

0 comments on commit 94759ff

Please sign in to comment.