cached_counts 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -4,8 +4,21 @@ module CachedCounts
|
|
4
4
|
module ActiveRecordBaseMethods
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
|
8
|
-
:
|
9
|
-
|
7
|
+
included do
|
8
|
+
after_save :clear_count_cache
|
9
|
+
after_destroy :clear_count_cache
|
10
|
+
|
11
|
+
class << self
|
12
|
+
delegate :clear_count_cache, :count_with_caching, :length_with_caching,
|
13
|
+
:size_with_caching, :count_without_caching, :length_without_caching,
|
14
|
+
:size_without_caching, :to => :scoped
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def clear_count_cache
|
20
|
+
self.class.clear_count_cache
|
21
|
+
end
|
22
|
+
end
|
10
23
|
end
|
11
24
|
end
|
@@ -3,11 +3,8 @@ module CachedCounts
|
|
3
3
|
initializer 'cached_counts' do |app|
|
4
4
|
ActiveSupport.on_load(:active_record) do
|
5
5
|
::ActiveRecord::Relation.send :include, CachedCounts::ActiveRecordRelationMethods
|
6
|
-
::ActiveRecord::Base.send :
|
6
|
+
::ActiveRecord::Base.send :include, CachedCounts::ActiveRecordBaseMethods
|
7
7
|
end
|
8
|
-
|
9
|
-
app.config.after_initialize { app.config.active_record.observers += [ :cached_count_observer ] }
|
10
|
-
app.config.to_prepare { CachedCounts::CachedCountObserver.instance.reload }
|
11
8
|
end
|
12
9
|
end
|
13
10
|
end
|
data/lib/cached_counts.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cached_counts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -111,7 +111,6 @@ files:
|
|
111
111
|
- lib/cached_counts/active_record_base_methods.rb
|
112
112
|
- lib/cached_counts/active_record_relation_methods.rb
|
113
113
|
- lib/cached_counts/cache.rb
|
114
|
-
- lib/cached_counts/cached_count_observer.rb
|
115
114
|
- lib/cached_counts/railtie.rb
|
116
115
|
- lib/cached_counts/version.rb
|
117
116
|
- spec/cached_counts/cache_spec.rb
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
module CachedCounts
|
4
|
-
class CachedCountObserver < ActiveRecord::Observer
|
5
|
-
observe :'ActiveRecord::Base'
|
6
|
-
|
7
|
-
def reload
|
8
|
-
observed_classes.each do |klass|
|
9
|
-
klass.name.constantize.add_observer(self)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def after_save(record)
|
14
|
-
CachedCounts::Cache.new(record.class).clear
|
15
|
-
end
|
16
|
-
|
17
|
-
def after_destroy(record)
|
18
|
-
CachedCounts::Cache.new(record.class).clear
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|