simple_cacheable 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cacheable.rb +39 -14
- data/lib/cacheable/version.rb +1 -1
- data/spec/cacheable_spec.rb +35 -0
- data/spec/models/image.rb +5 -0
- data/spec/models/post.rb +3 -1
- data/spec/models/user.rb +2 -1
- data/spec/spec_helper.rb +5 -0
- metadata +6 -4
data/lib/cacheable.rb
CHANGED
@@ -29,7 +29,6 @@ module Cacheable
|
|
29
29
|
class_eval <<-EOF
|
30
30
|
after_commit :expire_attribute_cache, :on => :update
|
31
31
|
after_commit :expire_all_attribute_cache, :on => :update
|
32
|
-
|
33
32
|
EOF
|
34
33
|
|
35
34
|
attributes.each do |attribute|
|
@@ -85,20 +84,45 @@ module Cacheable
|
|
85
84
|
end
|
86
85
|
EOF
|
87
86
|
else
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
87
|
+
if through_reflection_name = association.options[:through]
|
88
|
+
through_association = self.reflect_on_association(through_reflection_name)
|
89
|
+
|
90
|
+
# FIXME it should be the only reflection but I'm not 100% positive
|
91
|
+
reverse_through_association = through_association.klass.reflect_on_all_associations(:belongs_to).first
|
92
|
+
|
93
|
+
reverse_association = association.klass.reflect_on_all_associations(:belongs_to).find { |reverse_association|
|
94
|
+
reverse_association.options[:polymorphic] ? reverse_association.name == association.source_reflection.options[:as] : reverse_association.klass == self
|
95
|
+
}
|
96
|
+
|
97
|
+
association.klass.class_eval <<-EOF
|
98
|
+
after_commit :expire_#{association_name}_cache
|
99
|
+
|
100
|
+
def expire_#{association_name}_cache
|
101
|
+
if respond_to? :cached_#{reverse_association.name}
|
102
|
+
# cached_viewable.expire_association_cache
|
103
|
+
cached_#{reverse_association.name}.expire_association_cache(:#{association_name})
|
104
|
+
else
|
105
|
+
#{reverse_association.name}.#{reverse_through_association.name}.expire_association_cache(:#{association_name})
|
106
|
+
end
|
99
107
|
end
|
100
|
-
|
101
|
-
|
108
|
+
EOF
|
109
|
+
else
|
110
|
+
reverse_association = association.klass.reflect_on_all_associations(:belongs_to).find { |reverse_association|
|
111
|
+
reverse_association.options[:polymorphic] ? reverse_association.name == association.options[:as] : reverse_association.klass == self
|
112
|
+
}
|
113
|
+
association.klass.class_eval <<-EOF
|
114
|
+
after_commit :expire_#{association_name}_cache
|
115
|
+
|
116
|
+
def expire_#{association_name}_cache
|
117
|
+
if respond_to? :cached_#{reverse_association.name}
|
118
|
+
# cached_viewable.expire_association_cache
|
119
|
+
cached_#{reverse_association.name}.expire_association_cache(:#{association_name})
|
120
|
+
else
|
121
|
+
#{reverse_association.name}.expire_association_cache(:#{association_name})
|
122
|
+
end
|
123
|
+
end
|
124
|
+
EOF
|
125
|
+
end
|
102
126
|
|
103
127
|
class_eval <<-EOF
|
104
128
|
def cached_#{association_name}
|
@@ -125,6 +149,7 @@ module Cacheable
|
|
125
149
|
def expire_model_cache
|
126
150
|
expire_key_cache if self.class.cached_key
|
127
151
|
expire_attribute_cache if self.class.cached_indices.present?
|
152
|
+
expire_all_attribute_cache if self.class.cached_indices.present?
|
128
153
|
expire_method_cache if self.class.cached_methods.present?
|
129
154
|
end
|
130
155
|
|
data/lib/cacheable/version.rb
CHANGED
data/spec/cacheable_spec.rb
CHANGED
@@ -8,6 +8,8 @@ describe Cacheable do
|
|
8
8
|
@account = @user.create_account
|
9
9
|
@post1 = @user.posts.create(:title => 'post1')
|
10
10
|
@post2 = @user.posts.create(:title => 'post2')
|
11
|
+
@image1 = @post1.images.create
|
12
|
+
@image2 = @post1.images.create
|
11
13
|
@comment1 = @post1.comments.create
|
12
14
|
@comment2 = @post1.comments.create
|
13
15
|
end
|
@@ -162,6 +164,32 @@ describe Cacheable do
|
|
162
164
|
@user.cached_account.should == @account
|
163
165
|
end
|
164
166
|
end
|
167
|
+
|
168
|
+
context "has_many through" do
|
169
|
+
it "should not cache associations" do
|
170
|
+
Rails.cache.read("users/#{@user.id}/association/images").should be_nil
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should cache User#images" do
|
174
|
+
@user.cached_images.should == [@image1, @image2]
|
175
|
+
Rails.cache.read("users/#{@user.id}/association/images").should == [@image1, @image2]
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should cache User#images multiple times" do
|
179
|
+
@user.cached_images
|
180
|
+
@user.cached_images.should == [@image1, @image2]
|
181
|
+
end
|
182
|
+
|
183
|
+
context "expiry" do
|
184
|
+
it "should have the correct collection" do
|
185
|
+
@image3 = @post1.images.create
|
186
|
+
Rails.cache.read("users/#{@user.id}/association/images").should be_nil
|
187
|
+
@user.cached_images.should == [@image1, @image2, @image3]
|
188
|
+
Rails.cache.read("users/#{@user.id}/association/images").should == [@image1, @image2, @image3]
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
165
193
|
end
|
166
194
|
|
167
195
|
context "expire_model_cache" do
|
@@ -200,6 +228,13 @@ describe Cacheable do
|
|
200
228
|
Rails.cache.read("posts/#{@post1.id}/association/comments").should be_nil
|
201
229
|
end
|
202
230
|
|
231
|
+
it "should delete has_many through with_association cache" do
|
232
|
+
@user.cached_images
|
233
|
+
Rails.cache.read("users/#{@user.id}/association/images").should_not be_nil
|
234
|
+
@image2.save
|
235
|
+
Rails.cache.read("users/#{@user.id}/association/images").should be_nil
|
236
|
+
end
|
237
|
+
|
203
238
|
it "should delete has_one with_association cache" do
|
204
239
|
@user.cached_account
|
205
240
|
Rails.cache.read("users/#{@user.id}/association/account").should_not be_nil
|
data/spec/models/post.rb
CHANGED
@@ -4,9 +4,11 @@ class Post < ActiveRecord::Base
|
|
4
4
|
belongs_to :user
|
5
5
|
has_many :comments, :as => :commentable
|
6
6
|
|
7
|
+
has_many :images, :as => :viewable
|
8
|
+
|
7
9
|
model_cache do
|
8
10
|
with_key
|
9
11
|
with_attribute :user_id
|
10
|
-
with_association :user, :comments
|
12
|
+
with_association :user, :comments, :images
|
11
13
|
end
|
12
14
|
end
|
data/spec/models/user.rb
CHANGED
@@ -3,12 +3,13 @@ class User < ActiveRecord::Base
|
|
3
3
|
|
4
4
|
has_many :posts
|
5
5
|
has_one :account
|
6
|
+
has_many :images, through: :posts
|
6
7
|
|
7
8
|
model_cache do
|
8
9
|
with_key
|
9
10
|
with_attribute :login
|
10
11
|
with_method :last_post
|
11
|
-
with_association :posts, :account
|
12
|
+
with_association :posts, :account, :images
|
12
13
|
end
|
13
14
|
|
14
15
|
def last_post
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_cacheable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- spec/cacheable_spec.rb
|
80
80
|
- spec/models/account.rb
|
81
81
|
- spec/models/comment.rb
|
82
|
+
- spec/models/image.rb
|
82
83
|
- spec/models/post.rb
|
83
84
|
- spec/models/user.rb
|
84
85
|
- spec/spec_helper.rb
|
@@ -96,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
97
|
version: '0'
|
97
98
|
segments:
|
98
99
|
- 0
|
99
|
-
hash:
|
100
|
+
hash: -672054861354724444
|
100
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
102
|
none: false
|
102
103
|
requirements:
|
@@ -105,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
106
|
version: '0'
|
106
107
|
segments:
|
107
108
|
- 0
|
108
|
-
hash:
|
109
|
+
hash: -672054861354724444
|
109
110
|
requirements: []
|
110
111
|
rubyforge_project:
|
111
112
|
rubygems_version: 1.8.24
|
@@ -116,6 +117,7 @@ test_files:
|
|
116
117
|
- spec/cacheable_spec.rb
|
117
118
|
- spec/models/account.rb
|
118
119
|
- spec/models/comment.rb
|
120
|
+
- spec/models/image.rb
|
119
121
|
- spec/models/post.rb
|
120
122
|
- spec/models/user.rb
|
121
123
|
- spec/spec_helper.rb
|