association_count 0.0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- N2E5NTg0YWU2MjE2YzI1YjA3NjU0OWQwZGM4ZmUzMGMzNDIzYWQyZA==
5
- data.tar.gz: !binary |-
6
- MGUwM2E5ODQxMmE5MjU4NjBhODE1ZmY0M2I3NzNhZjFmYzAwNGE3Ng==
2
+ SHA1:
3
+ metadata.gz: 1fcc68fe097ba408a4885766a0cd17102bbe5089
4
+ data.tar.gz: a96d3b8013465d5278b8f99d66344231f7bd7c0e
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NzQwZjczOTY0MDYxYTVlNzY1MTMyYWEwYWFhNmFmYzFhNmE2OTAzOTBmMzEz
10
- MWM0Y2Q2MmQxNzkwM2E5NmI2YWYxNjliYzU2ZGI4NzY4MjY4ZjMxZjMyM2Zm
11
- NmQ5MjE1ZjYyODg2M2U5NDI0ZWViM2Q1YjI4MjU4NTEzNzMxODY=
12
- data.tar.gz: !binary |-
13
- ZDY4YTVlZTRjYjM3NzM3NTRkYzQzMzRkZDM5NzcxYjE1YmZhNjRjNzQzMzE0
14
- YjNiZjU5M2YyYTM0YmRlZDkzNTE0OTExNzc0ZDZhM2I1ZDgwYWI3NjFhOWVl
15
- YjhiYTAyMDFhNWZkZjZjODJmODEwY2IwMjc3NjFkM2M0YzQyM2U=
6
+ metadata.gz: 9fe60b5703414c9e644defc0eeab240d711028e24b43da349e6c0001699814f773108a6fb713085fb2a76f62e973995ca33e456f857404f9b1bd71b8e7cb0d6d
7
+ data.tar.gz: 550d3574989d39bce857f802bf75acbe0e9af9490e76a5da0c039a02bce7602122bfae8bad2c2262bdda8915ea31ce81b77b67e8691ce80d696c8e044f677447
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
3
+ - 2.2
4
4
  script: bundle exec rspec spec
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # AssociationCount [![Build Status](https://travis-ci.org/trialbee/association_count.svg?branch=master)](https://travis-ci.org/trialbee/association_count)
1
+ # AssociationCount [![Build Status](https://travis-ci.org/buren/association_count.svg?branch=master)](https://travis-ci.org/buren/association_count)
2
2
 
3
- A small gem for activerecord 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
 
@@ -20,6 +20,36 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ __Include in specific model__
24
+
25
+ Simply add
26
+
27
+ ```ruby
28
+ class Post < ApplicationRecord
29
+ extend AssociationCount
30
+
31
+ # [...]
32
+ end
33
+ ```
34
+
35
+ __Include in all models__
36
+
37
+ Rails 5, add it to `ApplicationRecord`
38
+
39
+ ```ruby
40
+ class ApplicationRecord < ActiveRecord::Base
41
+ self.abstract_class = true
42
+ # [...]
43
+ extend AssociationCount
44
+ end
45
+ ```
46
+
47
+ Rails 4, add it to `ActiveRecord::Base`
48
+
49
+ ```ruby
50
+ ActiveRecord::Base.extend AssociationCount
51
+ ```
52
+
23
53
  ```ruby
24
54
  class Foo < ActiveRecord::Base
25
55
  has_many :bars
@@ -30,9 +60,11 @@ class Bar < ActiveRecord::Base
30
60
  belongs_to :foo
31
61
  end
32
62
 
33
- # Now you can use this in order for each of your Foo instances to come with a preloaded bar_count
34
- foos = Foo.all.include_bar_count
35
- bar_counts = foos.map(&:bar_count) # only one SQL query executed
63
+ # Each Foo instance will come with a "preloaded" count method: bar_count
64
+ Foo.all.include_bar_count.map(&:bar_count) # only one SQL query executed
65
+
66
+ # you can also achieve the same with
67
+ foos = Foo.all.association_count(Bar)
36
68
  ```
37
69
 
38
70
  This works for any `has_many` relationship even if it uses non standard foreign keys or is a `has_many :x, through: y`.
@@ -54,7 +86,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
54
86
 
55
87
  ## Contributing
56
88
 
57
- 1. Fork it ( https://github.com/trialbee/association_count/fork )
89
+ 1. Fork it ( https://github.com/buren/association_count/fork )
58
90
  2. Create your feature branch (`git checkout -b my-new-feature`)
59
91
  3. Commit your changes (`git commit -am 'Add some feature'`)
60
92
  4. Push to the branch (`git push origin my-new-feature`)
@@ -7,9 +7,9 @@ Gem::Specification.new do |spec|
7
7
  spec.name = 'association_count'
8
8
  spec.version = AssociationCount::VERSION
9
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'
10
+ spec.email = ['burenstam@gmail.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/buren/association_count'
13
13
  spec.license = 'MIT'
14
14
 
15
15
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -24,6 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'simplecov', '~> 0.7'
25
25
  spec.add_development_dependency 'guard', '~> 2.12'
26
26
  spec.add_development_dependency 'guard-rspec', '~> 4.5'
27
-
28
- spec.add_dependency 'activerecord', '~> 4.0'
27
+ spec.add_development_dependency 'activerecord', '>= 4.0'
29
28
  end
@@ -1,9 +1,9 @@
1
1
  require 'association_count/version'
2
- require 'active_record'
3
2
 
4
- ActiveRecord::Base.extend AssociationCount
5
3
  module AssociationCount
6
- def association_count(counted_model, distinct)
4
+ DEFAULT_DISTINCT = false
5
+
6
+ def association_count(counted_model, distinct: DEFAULT_DISTINCT)
7
7
  table_name = self.table_name
8
8
  counted_table = counted_model.table_name
9
9
  counted_name = counted_table.singularize
@@ -19,7 +19,7 @@ module AssociationCount
19
19
  reflection = reflections[model_name]
20
20
  fail "No such reflection: '#{model_name}'" unless reflection
21
21
 
22
- options = { distinct: true }.merge!(opts)
22
+ options = { distinct: DEFAULT_DISTINCT }.merge!(opts)
23
23
  singular_name = model_name.singularize
24
24
 
25
25
  define_association_count_method(model_name, singular_name)
@@ -34,10 +34,12 @@ module AssociationCount
34
34
  end
35
35
  end
36
36
 
37
- def define_count_scope(singular_name, reflection, distinct)
37
+ def define_count_scope(singular_name, reflection, default_distinct)
38
38
  scope_name = "include_#{singular_name}_count"
39
39
  class_eval do
40
- scope scope_name, -> { association_count(reflection.klass, distinct) }
40
+ scope scope_name, ->(distinct: default_distinct) {
41
+ association_count(reflection.klass, distinct: distinct)
42
+ }
41
43
  end
42
44
  end
43
45
  end
@@ -1,3 +1,3 @@
1
1
  module AssociationCount
2
- VERSION = "0.0.4"
2
+ VERSION = "1.0.0"
3
3
  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
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Albin Svensson
@@ -9,131 +9,130 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-04-24 00:00:00.000000000 Z
12
+ date: 2018-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ~>
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
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
27
  version: '1.5'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ~>
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
34
  version: '10.0'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ~>
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
41
  version: '10.0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rspec
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ~>
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
48
  version: '3.2'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ~>
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '3.2'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: sqlite3
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ~>
60
+ - - "~>"
61
61
  - !ruby/object:Gem::Version
62
62
  version: '1.3'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ~>
67
+ - - "~>"
68
68
  - !ruby/object:Gem::Version
69
69
  version: '1.3'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: simplecov
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ~>
74
+ - - "~>"
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0.7'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ~>
81
+ - - "~>"
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0.7'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: guard
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - ~>
88
+ - - "~>"
89
89
  - !ruby/object:Gem::Version
90
90
  version: '2.12'
91
91
  type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - ~>
95
+ - - "~>"
96
96
  - !ruby/object:Gem::Version
97
97
  version: '2.12'
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: guard-rspec
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - ~>
102
+ - - "~>"
103
103
  - !ruby/object:Gem::Version
104
104
  version: '4.5'
105
105
  type: :development
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - ~>
109
+ - - "~>"
110
110
  - !ruby/object:Gem::Version
111
111
  version: '4.5'
112
112
  - !ruby/object:Gem::Dependency
113
113
  name: activerecord
114
114
  requirement: !ruby/object:Gem::Requirement
115
115
  requirements:
116
- - - ~>
116
+ - - ">="
117
117
  - !ruby/object:Gem::Version
118
118
  version: '4.0'
119
- type: :runtime
119
+ type: :development
120
120
  prerelease: false
121
121
  version_requirements: !ruby/object:Gem::Requirement
122
122
  requirements:
123
- - - ~>
123
+ - - ">="
124
124
  - !ruby/object:Gem::Version
125
125
  version: '4.0'
126
126
  description:
127
127
  email:
128
- - albin.svensson@trialbee.com
129
- - jacob.burenstam@trialbee.com
128
+ - burenstam@gmail.com
130
129
  executables: []
131
130
  extensions: []
132
131
  extra_rdoc_files: []
133
132
  files:
134
- - .gitignore
135
- - .rspec
136
- - .travis.yml
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
137
136
  - Gemfile
138
137
  - Guardfile
139
138
  - LICENSE.txt
@@ -144,7 +143,7 @@ files:
144
143
  - bin/setup
145
144
  - lib/association_count.rb
146
145
  - lib/association_count/version.rb
147
- homepage: https://github.com/trialbee/association_count
146
+ homepage: https://github.com/buren/association_count
148
147
  licenses:
149
148
  - MIT
150
149
  metadata: {}
@@ -154,20 +153,19 @@ require_paths:
154
153
  - lib
155
154
  required_ruby_version: !ruby/object:Gem::Requirement
156
155
  requirements:
157
- - - ! '>='
156
+ - - ">="
158
157
  - !ruby/object:Gem::Version
159
158
  version: '0'
160
159
  required_rubygems_version: !ruby/object:Gem::Requirement
161
160
  requirements:
162
- - - ! '>='
161
+ - - ">="
163
162
  - !ruby/object:Gem::Version
164
163
  version: '0'
165
164
  requirements: []
166
165
  rubyforge_project:
167
- rubygems_version: 2.2.2
166
+ rubygems_version: 2.6.13
168
167
  signing_key:
169
168
  specification_version: 4
170
- summary: A small gem for activerecord that allows association counts to be included
169
+ summary: A small gem for ActiveRecord that allows association counts to be included
171
170
  in your base query
172
171
  test_files: []
173
- has_rdoc: