Permalink
Browse files

add `cache_key` to Mingo models and ManyProxy

  • Loading branch information...
1 parent ae9c29d commit 1123f042d2b29e28c29abfca1346596e7d00a5be @mislav committed Sep 20, 2011
Showing with 33 additions and 1 deletion.
  1. +11 −0 lib/mingo.rb
  2. +4 −0 lib/mingo/many_proxy.rb
  3. +18 −1 spec/mingo_spec.rb
View
@@ -54,4 +54,15 @@ def id
def ==(other)
other.is_a?(self.class) and other.id == self.id
end
+
+ def cache_key
+ model_key = self.class.model_name.cache_key
+ if not persisted?
+ "#{model_key}/new"
+ elsif timestamp = self['updated_at']
+ "#{model_key}/#{id}-#{timestamp.utc.strftime("%Y%m%d%H%M%S")}"
+ else
+ "#{model_key}/#{id}"
+ end
+ end
end
View
@@ -102,6 +102,10 @@ def reset_counter_cache
counter_cache
end
+ def cache_key
+ [@parent, @property, counter_cache]
+ end
+
private
def method_missing(method, *args, &block)
View
@@ -220,7 +220,24 @@ def age=(value)
results.should be_empty
end
end
-
+
+ context "cache_key" do
+ it "has it" do
+ build.cache_key.should == "users/new"
+ end
+
+ it "includes the ID if persisted" do
+ user = create
+ user.cache_key.should == "users/#{user.id}"
+ end
+
+ it "includes the timestamp if present" do
+ user = create
+ user['updated_at'] = Time.utc_time 2011, 12, 13, 14, 15, 16
+ user.cache_key.should == "users/#{user.id}-20111213141516"
+ end
+ end
+
def build(*args)
described_class.new(*args)
end

0 comments on commit 1123f04

Please sign in to comment.