association_count 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.travis.yml +1 -0
- data/README.md +13 -2
- data/association_count.gemspec +16 -16
- data/lib/association_count.rb +28 -15
- data/lib/association_count/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
N2E5NTg0YWU2MjE2YzI1YjA3NjU0OWQwZGM4ZmUzMGMzNDIzYWQyZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MGUwM2E5ODQxMmE5MjU4NjBhODE1ZmY0M2I3NzNhZjFmYzAwNGE3Ng==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NzQwZjczOTY0MDYxYTVlNzY1MTMyYWEwYWFhNmFmYzFhNmE2OTAzOTBmMzEz
|
10
|
+
MWM0Y2Q2MmQxNzkwM2E5NmI2YWYxNjliYzU2ZGI4NzY4MjY4ZjMxZjMyM2Zm
|
11
|
+
NmQ5MjE1ZjYyODg2M2U5NDI0ZWViM2Q1YjI4MjU4NTEzNzMxODY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZDY4YTVlZTRjYjM3NzM3NTRkYzQzMzRkZDM5NzcxYjE1YmZhNjRjNzQzMzE0
|
14
|
+
YjNiZjU5M2YyYTM0YmRlZDkzNTE0OTExNzc0ZDZhM2I1ZDgwYWI3NjFhOWVl
|
15
|
+
YjhiYTAyMDFhNWZkZjZjODJmODEwY2IwMjc3NjFkM2M0YzQyM2U=
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# AssociationCount
|
1
|
+
# AssociationCount [![Build Status](https://travis-ci.org/trialbee/association_count.svg?branch=master)](https://travis-ci.org/trialbee/association_count)
|
2
2
|
|
3
|
-
A small gem that allows association counts to be included in your base query.
|
3
|
+
A small gem for activerecord that allows association counts to be included in your base query.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -35,6 +35,17 @@ foos = Foo.all.include_bar_count
|
|
35
35
|
bar_counts = foos.map(&:bar_count) # only one SQL query executed
|
36
36
|
```
|
37
37
|
|
38
|
+
This works for any `has_many` relationship even if it uses non standard foreign keys or is a `has_many :x, through: y`.
|
39
|
+
|
40
|
+
By default the count will be distinct, if this is not desired use:
|
41
|
+
|
42
|
+
```
|
43
|
+
class Foo < ActiveRecord::Base
|
44
|
+
has_many :bars
|
45
|
+
can_count :bars, distinct: false
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
38
49
|
## Development
|
39
50
|
|
40
51
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/association_count.gemspec
CHANGED
@@ -4,26 +4,26 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'association_count/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'association_count'
|
8
8
|
spec.version = AssociationCount::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.homepage =
|
13
|
-
spec.license =
|
9
|
+
spec.authors = ['Albin Svensson', 'Jacob Burenstam']
|
10
|
+
spec.email = ['albin.svensson@trialbee.com', 'jacob.burenstam@trialbee.com']
|
11
|
+
spec.summary = 'A small gem for activerecord that allows association counts to be included in your base query'
|
12
|
+
spec.homepage = 'https://github.com/trialbee/association_count'
|
13
|
+
spec.license = 'MIT'
|
14
14
|
|
15
15
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
-
spec.bindir =
|
16
|
+
spec.bindir = 'exe'
|
17
17
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
-
spec.require_paths = [
|
18
|
+
spec.require_paths = ['lib']
|
19
19
|
|
20
|
-
spec.add_development_dependency
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency 'rspec',
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency 'simplecov',
|
25
|
-
spec.add_development_dependency 'guard',
|
26
|
-
spec.add_development_dependency 'guard-rspec',
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
23
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'simplecov', '~> 0.7'
|
25
|
+
spec.add_development_dependency 'guard', '~> 2.12'
|
26
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.5'
|
27
27
|
|
28
|
-
spec.add_dependency
|
28
|
+
spec.add_dependency 'activerecord', '~> 4.0'
|
29
29
|
end
|
data/lib/association_count.rb
CHANGED
@@ -1,30 +1,43 @@
|
|
1
|
-
require
|
1
|
+
require 'association_count/version'
|
2
2
|
require 'active_record'
|
3
3
|
|
4
4
|
ActiveRecord::Base.extend AssociationCount
|
5
5
|
module AssociationCount
|
6
|
-
def association_count(counted_model)
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
select("#{table_name}.*, COUNT(
|
14
|
-
group("#{table_name}.id")
|
15
|
-
end.call(self, counted_model)
|
6
|
+
def association_count(counted_model, distinct)
|
7
|
+
table_name = self.table_name
|
8
|
+
counted_table = counted_model.table_name
|
9
|
+
counted_name = counted_table.singularize
|
10
|
+
distinct_sql = distinct ? 'DISTINCT' : ''
|
11
|
+
|
12
|
+
joins(counted_table.to_sym)
|
13
|
+
.select("#{table_name}.*, COUNT(#{distinct_sql} #{counted_table}.id) as #{counted_name}_count_raw")
|
14
|
+
.group("#{table_name}.id")
|
16
15
|
end
|
17
16
|
|
18
|
-
def can_count(model_name)
|
17
|
+
def can_count(model_name, opts = {})
|
19
18
|
model_name = model_name.to_s
|
20
|
-
|
19
|
+
reflection = reflections[model_name]
|
20
|
+
fail "No such reflection: '#{model_name}'" unless reflection
|
21
|
+
|
22
|
+
options = { distinct: true }.merge!(opts)
|
21
23
|
singular_name = model_name.singularize
|
24
|
+
|
25
|
+
define_association_count_method(model_name, singular_name)
|
26
|
+
define_count_scope(singular_name, reflection, options[:distinct])
|
27
|
+
end
|
28
|
+
|
29
|
+
def define_association_count_method(model_name, singular_name)
|
22
30
|
define_method "#{singular_name}_count" do
|
23
|
-
|
31
|
+
raw_count_name = "#{singular_name}_count_raw"
|
32
|
+
return send(raw_count_name) if self.respond_to?(raw_count_name)
|
24
33
|
send(model_name).count
|
25
34
|
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def define_count_scope(singular_name, reflection, distinct)
|
38
|
+
scope_name = "include_#{singular_name}_count"
|
26
39
|
class_eval do
|
27
|
-
scope
|
40
|
+
scope scope_name, -> { association_count(reflection.klass, distinct) }
|
28
41
|
end
|
29
42
|
end
|
30
43
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: association_count
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Albin Svensson
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-04-
|
12
|
+
date: 2015-04-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ~>
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '1.
|
20
|
+
version: '1.5'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ~>
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '1.
|
27
|
+
version: '1.5'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '3.2'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
|
-
name: sqlite3
|
57
|
+
name: sqlite3
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
60
|
- - ~>
|
@@ -167,6 +167,7 @@ rubyforge_project:
|
|
167
167
|
rubygems_version: 2.2.2
|
168
168
|
signing_key:
|
169
169
|
specification_version: 4
|
170
|
-
summary: A small gem that allows association counts to be included
|
170
|
+
summary: A small gem for activerecord that allows association counts to be included
|
171
|
+
in your base query
|
171
172
|
test_files: []
|
172
173
|
has_rdoc:
|