public
Fork of defunkt/cache_fu
Description: Everyone's favorite memcached plugin for ActiveRecord.
Homepage: http://errtheblog.com
Clone URL: git://github.com/entombedvirus/cache_fu.git
ported over has_paginated_list. Need someone *cough*Alex*cough* to write 
tests for it.
entombedvirus (author)
Fri Aug 15 22:59:24 -0700 2008
commit  e8970b56e80a674c7309ff87e5bcbc44f7103880
tree    81dc7433b188c2b6b11e7d1757295ba2350b3925
parent  b0481e2fff7d90d0aa883f83a8e1fc31033a9620
...
3
4
5
 
6
7
8
...
17
18
19
20
21
22
 
 
 
23
24
25
...
28
29
30
31
32
 
 
33
34
35
36
 
 
 
37
38
39
40
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
43
44
...
56
57
58
 
 
 
 
 
 
59
60
61
...
3
4
5
6
7
8
9
...
18
19
20
 
 
 
21
22
23
24
25
26
...
29
30
31
 
 
32
33
34
 
 
 
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
75
76
77
78
79
80
81
82
83
84
85
86
0
@@ -3,6 +3,7 @@
0
   cached_association_proxy
0
   has_many_cached
0
   belongs_to_cached
0
+ has_paginated_list
0
 ).each { |file| require File.join("acts_as_cached", "cached_associations", file)}
0
 
0
 module ActsAsCached
0
@@ -17,9 +18,9 @@ module ActsAsCached
0
     module ClassMethods
0
       # Usage:
0
       #
0
- # class User < ActiveRecord::Base
0
- # has_many_cached :cats
0
- # end
0
+ # class User < ActiveRecord::Base
0
+ # has_many_cached :cats
0
+ # end
0
       def has_many_cached(association_id, options = {}, &extensions)
0
         raise "Cannot have :limit and :order on has_many_cached associations" if options[:order] || options[:limit]
0
         
0
@@ -28,17 +29,35 @@ module ActsAsCached
0
       end
0
       
0
       # Usage:
0
- # class User < ActiveRecord::Base
0
- # end
0
+ # class User < ActiveRecord::Base
0
+ # end
0
       #
0
- # class Cat < ActiveRecord::Base
0
- # belongs_to_cached :user
0
- # end
0
+ # class Cat < ActiveRecord::Base
0
+ # belongs_to_cached :user
0
+ # end
0
       def belongs_to_cached(association_id, options = {}, &extensions)
0
         belongs_to(association_id, options, &extensions)
0
         create_belongs_to_cached_reflection(association_id, options)
0
       end
0
       
0
+ # A macro to define a has_many relationship and the accompanying cache machinery specifically to
0
+ # handle an association that is paginated and displayed in reverse chronological order (implemented
0
+ # by ordering using "id DESC").
0
+ #
0
+ # class User < ActiveRecord::Base
0
+ # acts_as_cached
0
+ #
0
+ # # Blog posts are displayed automagically ordered by "id DESC", 10 at a time.
0
+ # has_paginated_list :blog_posts, :limit => 10
0
+ # end
0
+ def has_paginated_list(association_id, options = {})
0
+ raise ":limit and :order are required options for a has_paginated_list association." unless options[:limit]
0
+
0
+ association_class = options[:class_name] ? options[:class_name].to_s.constantize : association_id.to_s.classify.constantize
0
+ has_many(association_id, options.merge({:order => "#{association_class.primary_key} DESC"}))
0
+ create_has_paginated_list_reflection(association_id, options)
0
+ end
0
+
0
       def cached_reflections
0
         read_inheritable_attribute(:cached_reflections) || write_inheritable_attribute(:cached_reflections, {})
0
       end
0
@@ -56,6 +75,12 @@ module ActsAsCached
0
         write_inheritable_hash :cached_reflections, name => reflection
0
         reflection
0
       end
0
+
0
+ def create_has_paginated_list_reflection(name, options)
0
+ reflection = ActsAsCached::CachedAssociations::HasPaginatedListReflection.new(:has_paginated_list, name, options, self)
0
+ write_inheritable_hash :cached_reflections, name => reflection
0
+ reflection
0
+ end
0
     end
0
    
0
    module InstanceMethods
...
11
12
13
14
 
15
16
17
...
11
12
13
 
14
15
16
17
0
@@ -11,7 +11,7 @@ module ActsAsCached
0
           options[:source_type] || source_reflection.class_name
0
         else
0
           class_name = name.to_s.camelize
0
- class_name = class_name.singularize if [ :has_many_cached ].include?(macro)
0
+ class_name = class_name.singularize if [ :has_many_cached :has_paginated_list ].include?(macro)
0
           class_name
0
         end
0
       end

Comments

    No one has commented yet.