scope_counter 0.1.0 → 0.2.0
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/README.md +10 -12
- data/lib/scope_counter.rb +10 -4
- data/lib/scope_counter/version.rb +1 -1
- data/spec/lib/scope_counter_spec.rb +6 -4
- data/spec/scope_counter.sqlite3 +0 -0
- data/spec/support/models.rb +1 -1
- data/spec/support/schema.rb +3 -2
- metadata +11 -11
data/README.md
CHANGED
@@ -3,11 +3,6 @@ scope_counter is a simple implementation of counter_cache functionality for mode
|
|
3
3
|
Install
|
4
4
|
--------
|
5
5
|
|
6
|
-
```shell
|
7
|
-
gem install scope_counter
|
8
|
-
```
|
9
|
-
or add the following line to Gemfile:
|
10
|
-
|
11
6
|
```ruby
|
12
7
|
gem 'scope_counter'
|
13
8
|
```
|
@@ -16,14 +11,15 @@ and run `bundle install` from your shell.
|
|
16
11
|
Usage
|
17
12
|
--------
|
18
13
|
|
19
|
-
Create columns in db according to scopes in your model ("#{scope_name}_count"):
|
14
|
+
Create columns in db according to scopes in your model ("#{scope_name}_#{table_name}_count"):
|
20
15
|
|
21
16
|
```ruby
|
22
|
-
t.integer :
|
23
|
-
t.integer :
|
17
|
+
t.integer :usefull_utilities_count, :default => 0
|
18
|
+
t.integer :useless_utilities_count, :default => 0
|
19
|
+
t.integer :utilities_count, :default => 0
|
24
20
|
```
|
25
21
|
|
26
|
-
List in array scopes, which you want to create counter cache for, as :conter_cache option:
|
22
|
+
List in array scopes, which you want to create counter cache for, as :conter_cache option (if you want to add a default behavior just add :all in array):
|
27
23
|
|
28
24
|
```ruby
|
29
25
|
class Advice < ActiveRecord::Base
|
@@ -31,18 +27,20 @@ class Advice < ActiveRecord::Base
|
|
31
27
|
end
|
32
28
|
|
33
29
|
class Utility < ActiveRecord::Base
|
34
|
-
belongs_to :advice, :counter_cache => [:usefull, :useless]
|
30
|
+
belongs_to :advice, :counter_cache => [:all,:usefull, :useless]
|
35
31
|
scope :usefull, where(:usefull => true)
|
36
32
|
scope :useless, where(:usefull => false)
|
37
33
|
end
|
38
34
|
|
39
35
|
advice = Advice.create
|
40
36
|
|
41
|
-
advice.
|
37
|
+
advice.usefull_utilities_count # => 0
|
42
38
|
|
43
39
|
advice.utilities.usefull.create
|
44
40
|
|
45
|
-
advice.
|
41
|
+
advice.usefull_utilities_count # => 1
|
42
|
+
|
43
|
+
advice.utilities_count # => 1
|
46
44
|
```
|
47
45
|
|
48
46
|
|
data/lib/scope_counter.rb
CHANGED
@@ -11,11 +11,17 @@ module ScopeCounter
|
|
11
11
|
def add_counter_cache_callbacks(reflection)
|
12
12
|
if Array === options[:counter_cache]
|
13
13
|
name = self.name
|
14
|
-
options[:counter_cache]
|
15
|
-
|
14
|
+
counter_cache_options = options[:counter_cache]
|
15
|
+
counter_cache_options.each do |scope|
|
16
|
+
if scope == :all
|
17
|
+
options[:counter_cache] = true
|
18
|
+
old_add_counter_cache_callbacks(reflection)
|
19
|
+
next
|
20
|
+
end
|
21
|
+
cache_column = "#{scope}_#{model.table_name}_count"
|
16
22
|
|
17
23
|
method_name = "belongs_to_counter_cache_after_create_for_#{scope}"
|
18
|
-
|
24
|
+
mixin.redefine_method(method_name) do
|
19
25
|
record = send(name)
|
20
26
|
record.class.increment_counter(cache_column, record.id) unless record.nil?
|
21
27
|
end
|
@@ -28,7 +34,7 @@ module ScopeCounter
|
|
28
34
|
model.after_create(method_name, :if => :"#{in_scope_method}")
|
29
35
|
|
30
36
|
method_name = "belongs_to_counter_cache_before_destroy_for_#{scope}"
|
31
|
-
|
37
|
+
mixin.redefine_method(method_name) do
|
32
38
|
record = send(name)
|
33
39
|
record.class.decrement_counter(cache_column, record.id) unless record.nil?
|
34
40
|
end
|
@@ -8,15 +8,17 @@ describe "scope_counter" do
|
|
8
8
|
it "should increase usefull count after usefull utility creation" do
|
9
9
|
@advice.utilities.create(:usefull => true)
|
10
10
|
@advice.reload
|
11
|
-
@advice.
|
12
|
-
@advice.
|
11
|
+
@advice.usefull_utilities_count.should == 1
|
12
|
+
@advice.useless_utilities_count.should == 0
|
13
|
+
@advice.utilities_count.should == 1
|
13
14
|
end
|
14
15
|
|
15
16
|
it "should increase useless count after useless utility creation" do
|
16
17
|
@advice.utilities.create(:usefull => false)
|
17
18
|
@advice.reload
|
18
|
-
@advice.
|
19
|
-
@advice.
|
19
|
+
@advice.useless_utilities_count.should == 1
|
20
|
+
@advice.usefull_utilities_count.should == 0
|
21
|
+
@advice.utilities_count.should == 1
|
20
22
|
end
|
21
23
|
|
22
24
|
end
|
data/spec/scope_counter.sqlite3
CHANGED
Binary file
|
data/spec/support/models.rb
CHANGED
@@ -3,7 +3,7 @@ class Advice < ActiveRecord::Base
|
|
3
3
|
end
|
4
4
|
|
5
5
|
class Utility < ActiveRecord::Base
|
6
|
-
belongs_to :advice, :counter_cache => [:usefull, :useless]
|
6
|
+
belongs_to :advice, :counter_cache => [:all,:usefull, :useless]
|
7
7
|
scope :usefull, where(:usefull => true)
|
8
8
|
scope :useless, where(:usefull => false)
|
9
9
|
end
|
data/spec/support/schema.rb
CHANGED
@@ -3,8 +3,9 @@ ActiveRecord::Schema.define do
|
|
3
3
|
|
4
4
|
create_table :advices, :force => true do |t|
|
5
5
|
t.string :text
|
6
|
-
t.integer :
|
7
|
-
t.integer :
|
6
|
+
t.integer :utilities_count, :default => 0
|
7
|
+
t.integer :usefull_utilities_count, :default => 0
|
8
|
+
t.integer :useless_utilities_count, :default => 0
|
8
9
|
end
|
9
10
|
|
10
11
|
create_table :utilities, :force => true do |t|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scope_counter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-04-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &10995340 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.0.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *10995340
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &10942180 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *10942180
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3-ruby
|
38
|
-
requirement: &
|
38
|
+
requirement: &10899200 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *10899200
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: activerecord
|
49
|
-
requirement: &
|
49
|
+
requirement: &10816600 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '3.0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *10816600
|
58
58
|
description: Simple implementation of counter_cache functionality for model scopes
|
59
59
|
email:
|
60
60
|
- lolcoltd@gmail.com
|
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
97
|
rubyforge_project: scope_counter
|
98
|
-
rubygems_version: 1.8.
|
98
|
+
rubygems_version: 1.8.15
|
99
99
|
signing_key:
|
100
100
|
specification_version: 3
|
101
101
|
summary: Provide counter_cache functionality for model scopes
|