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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWQ5ZDdlZDIwY2MyMzE2NTQ4YzdjYjMyYjM2NGI1NWM0MWMwZWYyYg==
4
+ N2E5NTg0YWU2MjE2YzI1YjA3NjU0OWQwZGM4ZmUzMGMzNDIzYWQyZA==
5
5
  data.tar.gz: !binary |-
6
- YjczMmM4NzM3OTE2Y2E0ODhiM2YzNzg0OWEyMGIwYTk0M2FhY2Y1OA==
6
+ MGUwM2E5ODQxMmE5MjU4NjBhODE1ZmY0M2I3NzNhZjFmYzAwNGE3Ng==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTJiNWQ2MDU0MTJjMmRmZGFlMjkzYWExMzdlYjUzZjI5ODY3NjNiYjhiYzQ1
10
- YThmMzVlNjFjODVkNTQ0Mjc3YzZiOTlkZmQ2NjBhZjE3NzdlODk2OTNiZTli
11
- ZWI2NmJmOGNjNDM5NThmMmExOTMzMzRjOWI3NjgxZjRiYjNjNDk=
9
+ NzQwZjczOTY0MDYxYTVlNzY1MTMyYWEwYWFhNmFmYzFhNmE2OTAzOTBmMzEz
10
+ MWM0Y2Q2MmQxNzkwM2E5NmI2YWYxNjliYzU2ZGI4NzY4MjY4ZjMxZjMyM2Zm
11
+ NmQ5MjE1ZjYyODg2M2U5NDI0ZWViM2Q1YjI4MjU4NTEzNzMxODY=
12
12
  data.tar.gz: !binary |-
13
- ODcxZTg0YWZhOWE0MmQwODZmZTI3ZDA1YjNhZTExMjY0Y2VmNDA5MTRmZjBm
14
- NjkwNWViNjdjYWI1M2RiNjRlNDMwMmEyN2Q5ZGU4OGEyMTNhY2FmNWUzMWU4
15
- Y2QyMThjMGI3YmY4ZmMzN2FmODUzMzhlNjNjNmJiODExNDRhZjc=
13
+ ZDY4YTVlZTRjYjM3NzM3NTRkYzQzMzRkZDM5NzcxYjE1YmZhNjRjNzQzMzE0
14
+ YjNiZjU5M2YyYTM0YmRlZDkzNTE0OTExNzc0ZDZhM2I1ZDgwYWI3NjFhOWVl
15
+ YjhiYTAyMDFhNWZkZjZjODJmODEwY2IwMjc3NjFkM2M0YzQyM2U=
@@ -1,3 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
+ script: bundle exec rspec spec
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.
@@ -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 = "association_count"
7
+ spec.name = 'association_count'
8
8
  spec.version = AssociationCount::VERSION
9
- spec.authors = ["Albin Svensson", 'Jacob Burenstam']
10
- spec.email = ["albin.svensson@trialbee.com", 'jacob.burenstam@trialbee.com']
11
- spec.summary = %q{ A small gem that allows association counts to be included in your base query }
12
- spec.homepage = "https://github.com/trialbee/association_count"
13
- spec.license = "MIT"
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 = "exe"
16
+ spec.bindir = 'exe'
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
- spec.require_paths = ["lib"]
18
+ spec.require_paths = ['lib']
19
19
 
20
- spec.add_development_dependency "bundler", "~> 1.9"
21
- spec.add_development_dependency "rake", "~> 10.0"
22
- spec.add_development_dependency 'rspec', "~> 3.2"
23
- spec.add_development_dependency "sqlite3-ruby", "~> 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"
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 "activerecord", "~> 4.0"
28
+ spec.add_dependency 'activerecord', '~> 4.0'
29
29
  end
@@ -1,30 +1,43 @@
1
- require "association_count/version"
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
- proc do |base_model, counted_model|
8
- table_name = base_model.table_name
9
- counted_table = counted_model.table_name
10
- counted_name = counted_table.singularize
11
-
12
- joins(counted_table.to_sym).
13
- select("#{table_name}.*, COUNT(DISTINCT #{counted_table}.id) as #{counted_name}_count_raw").
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
- raise 'No such reflection' unless reflection = reflections[model_name.to_s]
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
- return send("#{singular_name}_count_raw") if self.respond_to?("#{singular_name}_count_raw")
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 "include_#{singular_name}_count", -> { association_count(reflection.klass) }
40
+ scope scope_name, -> { association_count(reflection.klass, distinct) }
28
41
  end
29
42
  end
30
43
  end
@@ -1,3 +1,3 @@
1
1
  module AssociationCount
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
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.3
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-21 00:00:00.000000000 Z
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.9'
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.9'
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-ruby
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 in your base query
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: