dase 3.2.4 → 3.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +9 -0
- data/README.md +33 -22
- data/dase.gemspec +11 -3
- data/lib/dase/mp_active_record_relation.rb +4 -3
- data/lib/dase/version.rb +1 -1
- data/test/test_dase.rb +7 -1
- metadata +33 -21
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
[![Build Status](https://secure.travis-ci.org/vovayartsev/dase.png)](http://travis-ci.org/vovayartsev/dase)
|
2
2
|
|
3
3
|
## Overview
|
4
4
|
|
@@ -16,22 +16,19 @@ Calling 'includes_count_of(:articles)' on a relation object adds 'articles_count
|
|
16
16
|
|
17
17
|
## Installation
|
18
18
|
|
19
|
-
Add this line to your application's Gemfile:
|
19
|
+
Add this line to your Rails 3.2.x application's Gemfile:
|
20
20
|
|
21
|
-
gem 'dase', "~> 3.2.
|
22
|
-
|
23
|
-
### Note on version numbers
|
24
|
-
|
25
|
-
Dase version number correlates with the Active Record's versions number,
|
26
|
-
which it has been tested with.
|
27
|
-
E.g. the latest 3.2.* version of Dase will play nicely with the latest 3.2.* version of Active Record.
|
28
|
-
Since it's a sort of a "hack", make sure you specified the version number for "dase" gem in your Gemfile.
|
21
|
+
gem 'dase', "~> 3.2.4"
|
29
22
|
|
30
23
|
## Usage
|
31
24
|
|
32
25
|
### Basic usage:
|
33
26
|
|
34
27
|
```
|
28
|
+
class Author
|
29
|
+
has_many :articles
|
30
|
+
end
|
31
|
+
|
35
32
|
Author.includes_count_of(:articles).find_each do |author|
|
36
33
|
puts "#{author.name} has #{author.articles_count} articles published"
|
37
34
|
end
|
@@ -46,15 +43,17 @@ Author.includes_count_of(:articles, :conditions => {:year => 2012}) # counts o
|
|
46
43
|
|
47
44
|
### Using scope merging
|
48
45
|
```
|
49
|
-
|
50
|
-
|
51
|
-
|
46
|
+
class Article
|
47
|
+
belongs_to :author
|
48
|
+
scope this_year, lambda { where(:year => 2012) }
|
49
|
+
end
|
52
50
|
|
53
|
-
|
54
|
-
|
55
|
-
Author.includes_count_of(:articles){ where(:year => 2012) } # in the block, 'self' is a Relation instance
|
56
|
-
Author.includes_count_of(:articles){ |scope| scope.where(:year => 2012) } # 'self' is the same inside and outside the block
|
51
|
+
results = Author.includes_count_of(:articles, :only => Article.this_year)
|
52
|
+
results.first.articles_count # => # number of articles of given Author for year 2012 only
|
57
53
|
```
|
54
|
+
This is achieved by merging the association scope with the scope provided as ":only => ..." option.
|
55
|
+
No additional checks are performed, and providing the association of proper type is solely your responsibility.
|
56
|
+
|
58
57
|
|
59
58
|
### Renaming counter column
|
60
59
|
```
|
@@ -62,10 +61,25 @@ sites = WebSite.includes_count_of(:users, :conditions => {:role => 'admin'}, :as
|
|
62
61
|
sites.each { |site| puts "Site #{site.url} has #{site.admins_count} admin users" }
|
63
62
|
```
|
64
63
|
|
64
|
+
## Compatibility
|
65
|
+
|
66
|
+
### Rails versions
|
65
67
|
|
66
|
-
|
68
|
+
This gem is for Rails 3.2.x . Earlier versions are not supported.
|
67
69
|
|
68
|
-
Dase
|
70
|
+
Note: the Dase gem version number correlates with the Active Record's versions number,
|
71
|
+
which it has been tested with.
|
72
|
+
E.g. the latest 3.2.* version of Dase will play nicely with the latest 3.2.* version of Active Record.
|
73
|
+
Since dase gem is a sort of a "hack", make sure you specified the version number for "dase" gem in your Gemfile.
|
74
|
+
|
75
|
+
### Polymorphic associations and HasManyThrough associations
|
76
|
+
|
77
|
+
Polymorphic associations and HasManyThrough associations support should work, but it is not tested quite well.
|
78
|
+
Bug reports and pull requests are very welcome.
|
79
|
+
|
80
|
+
### jRuby support
|
81
|
+
|
82
|
+
Not yet
|
69
83
|
|
70
84
|
## How it works
|
71
85
|
|
@@ -77,9 +91,6 @@ Here's a pseudo-code that gives an idea on how it works internally
|
|
77
91
|
end
|
78
92
|
```
|
79
93
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
94
|
## Name origin
|
84
95
|
|
85
96
|
The gem is named by the german mathematician [Johann Dase](http://en.wikipedia.org/wiki/Zacharias_Dase),
|
data/dase.gemspec
CHANGED
@@ -14,11 +14,19 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.summary = %q{Provides includes_count_of method on ActiveRecord::Relation to count associated records efficiently}
|
15
15
|
gem.homepage = "https://github.com/vovayartsev/dase"
|
16
16
|
|
17
|
-
gem.add_runtime_dependency "activerecord", "~> 3.2.0"
|
18
17
|
gem.add_runtime_dependency "activesupport", "~> 3.2.0"
|
19
18
|
gem.add_development_dependency 'shoulda'
|
20
|
-
|
21
|
-
|
19
|
+
|
20
|
+
if defined? JRUBY_VERSION
|
21
|
+
gem.add_development_dependency 'activerecord-jdbcsqlite3-adapter'
|
22
|
+
else
|
23
|
+
gem.add_runtime_dependency "activerecord", "~> 3.2.0"
|
24
|
+
gem.add_development_dependency 'sqlite3', '~> 1.3.3'
|
25
|
+
end
|
26
|
+
|
27
|
+
# gem.add_development_dependency 'debugger'
|
28
|
+
gem.add_development_dependency 'rake'
|
29
|
+
gem.add_development_dependency 'rspec-core'
|
22
30
|
|
23
31
|
gem.files = `git ls-files`.split($/)
|
24
32
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -14,9 +14,10 @@ module Dase
|
|
14
14
|
relation.dase_values ||= {}
|
15
15
|
#options[:proc] = block if block
|
16
16
|
args.each do |arg|
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
opts = options.clone
|
18
|
+
opts[:association] = arg.to_sym
|
19
|
+
opts[:as] = (opts[:as] || "#{arg}_count").to_sym
|
20
|
+
relation.dase_values[opts[:as]] = opts
|
20
21
|
end
|
21
22
|
relation
|
22
23
|
end
|
data/lib/dase/version.rb
CHANGED
data/test/test_dase.rb
CHANGED
@@ -50,7 +50,7 @@ class TestBase < Test::Unit::TestCase
|
|
50
50
|
end
|
51
51
|
|
52
52
|
should "count books for year 1990" do
|
53
|
-
traditional_counts = Author.order(:name).map { |a| a.books.where(year
|
53
|
+
traditional_counts = Author.order(:name).map { |a| a.books.where(:year => 1990).count }
|
54
54
|
dase_counts = Author.includes_count_of(:books, :conditions => {:year => 1990}).order(:name).map { |a| a.books_count }
|
55
55
|
# the order is: Bobby, Joe, Teddy - due to order(:name)
|
56
56
|
true_counts = [1, 2, 0] # see books.yml
|
@@ -94,5 +94,11 @@ class TestBase < Test::Unit::TestCase
|
|
94
94
|
compare_counts(traditional_counts, dase_counts, true_counts)
|
95
95
|
end
|
96
96
|
|
97
|
+
should 'support multiple arguments' do
|
98
|
+
joe = Author.includes_count_of(:books, :old_books).where(:name => 'Joe').first
|
99
|
+
assert_equal 3, joe.books_count, "Invalid books_count"
|
100
|
+
assert_equal 2, joe.old_books_count, "Invalid old_books_count"
|
101
|
+
end
|
102
|
+
|
97
103
|
end
|
98
104
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dase
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.2.
|
4
|
+
version: 3.2.5
|
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: 2012-
|
12
|
+
date: 2012-11-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement: &
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70173173459420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,21 @@ dependencies:
|
|
21
21
|
version: 3.2.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70173173459420
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement: &
|
26
|
+
name: shoulda
|
27
|
+
requirement: &70173173458740 !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: *70173173458740
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activerecord
|
38
|
+
requirement: &70173173458200 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ~>
|
@@ -32,32 +43,32 @@ dependencies:
|
|
32
43
|
version: 3.2.0
|
33
44
|
type: :runtime
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *70173173458200
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
-
requirement: &
|
48
|
+
name: sqlite3
|
49
|
+
requirement: &70173173457620 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
|
-
- -
|
52
|
+
- - ~>
|
42
53
|
- !ruby/object:Gem::Version
|
43
|
-
version:
|
54
|
+
version: 1.3.3
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *70173173457620
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
49
|
-
requirement: &
|
59
|
+
name: rake
|
60
|
+
requirement: &70173173457020 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
|
-
- -
|
63
|
+
- - ! '>='
|
53
64
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
65
|
+
version: '0'
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *70173173457020
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
|
-
name:
|
60
|
-
requirement: &
|
70
|
+
name: rspec-core
|
71
|
+
requirement: &70173173456380 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
74
|
- - ! '>='
|
@@ -65,7 +76,7 @@ dependencies:
|
|
65
76
|
version: '0'
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *70173173456380
|
69
80
|
description: ! "Dase gem creates includes_count_of method in ActiveRecord::Relation\n
|
70
81
|
\ to count associated records efficiently. See examples at
|
71
82
|
https://github.com/vovayartsev/dase\n "
|
@@ -76,6 +87,7 @@ extensions: []
|
|
76
87
|
extra_rdoc_files: []
|
77
88
|
files:
|
78
89
|
- .gitignore
|
90
|
+
- .travis.yml
|
79
91
|
- Gemfile
|
80
92
|
- LICENSE.txt
|
81
93
|
- README.md
|