Permalink
Browse files

allow cache to be called without a key, updated README

Functionally the routine works exactly the same, the key is just now optional.

Given the first example under caching, calling `cache @user` makes sense.
For the second example though, @user isn't within context so calling
cache by itself is cleaner.
  • Loading branch information...
databyte committed Apr 5, 2012
1 parent 43ebf75 commit 9e6a040f1dd71bee311ace9062bea681208987c8
Showing with 21 additions and 4 deletions.
  1. +18 −2 README.md
  2. +2 −1 lib/rabl/engine.rb
  3. +1 −1 test/integration/rails3_2/posts_controller_test.rb
View
@@ -496,12 +496,28 @@ cache @users # key = rabl/users/[cache_key]/users/[cache_key]/...
extends "users/show"
```
-and within the inherited template:
+And within the extended template:
```ruby
# app/views/users/show.json.rabl
object @user
-cache @user # key = rabl/user/[cache_key]/...
+cache @user # key = rabl/user/[cache_key]/...
+
+attributes :name, :email
+```
+
+Another example of extending your object templates.
+
+```ruby
+# app/views/users/show.json.rabl
+object @user
+
+extends "users/user"
+```
+
+```ruby
+# app/views/users/user.json.rabl
+cache # key = rabl/user/[cache_key]/...
attributes :name, :email
```
View
@@ -118,9 +118,10 @@ def collection(data, options={})
# cache @user # calls @user.cache_key
# cache ['rabl', @user] # calls @user.cache_key and prefixes with rabl/
# cache 'user' # explicit key of 'user'
+ # cache # uses the current item within a collection
# cache 'user', expires_in: 1.hour
# options is passed through to the cache store
- def cache(key, options = nil)
+ def cache(key = nil, options = nil)
key ||= @_data # if called but missing, use object
@_cache = [key, options]
end
@@ -14,7 +14,6 @@
setup do
create_users!
- Rails.cache.clear
Post.delete_all
@post1 = Post.create(:title => "Foo", :body => "Bar", :user_id => @user1.id)
@post2 = Post.create(:title => "Baz", :body => "Bah", :user_id => @user2.id)
@@ -132,6 +131,7 @@
setup do
mock(ActionController::Base).perform_caching.any_number_of_times { true }
+ Rails.cache.clear
end
context "for index action with caching in json" do

0 comments on commit 9e6a040

Please sign in to comment.