scope_counter 0.1.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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in scope_counter.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Yuri Barbashov.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ scope_counter is a simple implementation of counter_cache functionality for models scopes with ActiveRecord
2
+
3
+ Install
4
+ --------
5
+
6
+ ```shell
7
+ gem install scope_counter
8
+ ```
9
+ or add the following line to Gemfile:
10
+
11
+ ```ruby
12
+ gem 'scope_counter'
13
+ ```
14
+ and run `bundle install` from your shell.
15
+
16
+ Usage
17
+ --------
18
+
19
+ Create columns in db according to scopes in your model ("#{scope_name}_count"):
20
+
21
+ ```ruby
22
+ t.integer :usefull_count, :default => 0
23
+ t.integer :useless_count, :default => 0
24
+ ```
25
+
26
+ List in array scopes, which you want to create counter cache for, as :conter_cache option:
27
+
28
+ ```ruby
29
+ class Advice < ActiveRecord::Base
30
+ has_many :utilities
31
+ end
32
+
33
+ class Utility < ActiveRecord::Base
34
+ belongs_to :advice, :counter_cache => [:usefull, :useless]
35
+ scope :usefull, where(:usefull => true)
36
+ scope :useless, where(:usefull => false)
37
+ end
38
+
39
+ advice = Advice.create
40
+
41
+ advice.usefull_count # => 0
42
+
43
+ advice.utilities.usefull.create
44
+
45
+ advice.usefull_count # => 1
46
+ ```
47
+
48
+
49
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module ScopeCounter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,49 @@
1
+ require 'active_record'
2
+ require "scope_counter/version"
3
+
4
+ module ScopeCounter
5
+ module BelongsToExtension
6
+ module ActiveRecord::Associations::Builder
7
+ class BelongsTo < SingularAssociation #:nodoc:
8
+ private
9
+ alias_method :old_add_counter_cache_callbacks, :add_counter_cache_callbacks
10
+
11
+ def add_counter_cache_callbacks(reflection)
12
+ if Array === options[:counter_cache]
13
+ name = self.name
14
+ options[:counter_cache].each do |scope|
15
+ cache_column = "#{scope}_count"
16
+
17
+ method_name = "belongs_to_counter_cache_after_create_for_#{scope}"
18
+ model.redefine_method(method_name) do
19
+ record = send(name)
20
+ record.class.increment_counter(cache_column, record.id) unless record.nil?
21
+ end
22
+
23
+ in_scope_method = "in_#{scope}?"
24
+ model.redefine_method(in_scope_method) do
25
+ send(:class).send(scope).exists?(send(:id))
26
+ end
27
+
28
+ model.after_create(method_name, :if => :"#{in_scope_method}")
29
+
30
+ method_name = "belongs_to_counter_cache_before_destroy_for_#{scope}"
31
+ model.redefine_method(method_name) do
32
+ record = send(name)
33
+ record.class.decrement_counter(cache_column, record.id) unless record.nil?
34
+ end
35
+ model.before_destroy(method_name,:if => :"#{in_scope_method}")
36
+
37
+ model.send(:module_eval,
38
+ "#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)", __FILE__, __LINE__
39
+ )
40
+ end
41
+ else
42
+ old_add_counter_cache_callbacks(reflection)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "scope_counter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "scope_counter"
7
+ s.version = ScopeCounter::VERSION
8
+ s.authors = ["Yuri Barbashov"]
9
+ s.email = ["lolcoltd@gmail.com"]
10
+ s.homepage = "https://github.com/playa/scope_counter"
11
+ s.summary = %q{Provide counter_cache functionality for model scopes}
12
+ s.description = %q{Simple implementation of counter_cache functionality for model scopes}
13
+
14
+ s.rubyforge_project = "scope_counter"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "bundler", ">= 1.0.0"
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "sqlite3-ruby"
24
+ s.add_dependency "activerecord", "~> 3.0"
25
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe "scope_counter" do
4
+ before(:each) do
5
+ @advice = Advice.create(:text => "Some advice")
6
+ end
7
+
8
+ it "should increase usefull count after usefull utility creation" do
9
+ @advice.utilities.create(:usefull => true)
10
+ @advice.reload
11
+ @advice.usefull_count.should == 1
12
+ @advice.useless_count.should == 0
13
+ end
14
+
15
+ it "should increase useless count after useless utility creation" do
16
+ @advice.utilities.create(:usefull => false)
17
+ @advice.reload
18
+ @advice.useless_count.should == 1
19
+ @advice.usefull_count.should == 0
20
+ end
21
+
22
+ end
Binary file
@@ -0,0 +1,6 @@
1
+ require 'scope_counter'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
4
+ :database => File.dirname(__FILE__) + "/scope_counter.sqlite3")
5
+ load File.dirname(__FILE__) + '/support/schema.rb'
6
+ load File.dirname(__FILE__) + '/support/models.rb'
@@ -0,0 +1,9 @@
1
+ class Advice < ActiveRecord::Base
2
+ has_many :utilities
3
+ end
4
+
5
+ class Utility < ActiveRecord::Base
6
+ belongs_to :advice, :counter_cache => [:usefull, :useless]
7
+ scope :usefull, where(:usefull => true)
8
+ scope :useless, where(:usefull => false)
9
+ end
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :advices, :force => true do |t|
5
+ t.string :text
6
+ t.integer :usefull_count, :default => 0
7
+ t.integer :useless_count, :default => 0
8
+ end
9
+
10
+ create_table :utilities, :force => true do |t|
11
+ t.boolean :usefull
12
+ t.integer :advice_id
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scope_counter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yuri Barbashov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &72891890 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *72891890
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &72891660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *72891660
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3-ruby
38
+ requirement: &72891400 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *72891400
47
+ - !ruby/object:Gem::Dependency
48
+ name: activerecord
49
+ requirement: &72891150 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *72891150
58
+ description: Simple implementation of counter_cache functionality for model scopes
59
+ email:
60
+ - lolcoltd@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - Gemfile
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - lib/scope_counter.rb
71
+ - lib/scope_counter/version.rb
72
+ - scope_counter.gemspec
73
+ - spec/lib/scope_counter_spec.rb
74
+ - spec/scope_counter.sqlite3
75
+ - spec/spec_helper.rb
76
+ - spec/support/models.rb
77
+ - spec/support/schema.rb
78
+ homepage: https://github.com/playa/scope_counter
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project: scope_counter
98
+ rubygems_version: 1.8.10
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Provide counter_cache functionality for model scopes
102
+ test_files: []