scope_counter 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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 :usefull_count, :default => 0
23
- t.integer :useless_count, :default => 0
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.usefull_count # => 0
37
+ advice.usefull_utilities_count # => 0
42
38
 
43
39
  advice.utilities.usefull.create
44
40
 
45
- advice.usefull_count # => 1
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].each do |scope|
15
- cache_column = "#{scope}_count"
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
- model.redefine_method(method_name) do
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
- model.redefine_method(method_name) do
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
@@ -1,3 +1,3 @@
1
1
  module ScopeCounter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  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.usefull_count.should == 1
12
- @advice.useless_count.should == 0
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.useless_count.should == 1
19
- @advice.usefull_count.should == 0
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
Binary file
@@ -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
@@ -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 :usefull_count, :default => 0
7
- t.integer :useless_count, :default => 0
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.1.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: 2011-11-23 00:00:00.000000000 Z
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: &72891890 !ruby/object:Gem::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: *72891890
24
+ version_requirements: *10995340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &72891660 !ruby/object:Gem::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: *72891660
35
+ version_requirements: *10942180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3-ruby
38
- requirement: &72891400 !ruby/object:Gem::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: *72891400
46
+ version_requirements: *10899200
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activerecord
49
- requirement: &72891150 !ruby/object:Gem::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: *72891150
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.10
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