active_record_group_count 0.0.1
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/MIT-LICENSE +20 -0
- data/README.rdoc +46 -0
- data/Rakefile +31 -0
- data/lib/active_record_group_count.rb +2 -0
- data/lib/active_record_group_count/railtie.rb +9 -0
- data/lib/active_record_group_count/scope.rb +19 -0
- data/lib/active_record_group_count/version.rb +3 -0
- data/spec/active_record_group_count/active_record_group_count_spec.rb +24 -0
- metadata +136 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 Jan Berdajs
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
= ActiveRecord Group Count
|
2
|
+
|
3
|
+
== Installing
|
4
|
+
|
5
|
+
Add it to your project's Gemfile
|
6
|
+
|
7
|
+
gem "active_record_group_count"
|
8
|
+
|
9
|
+
and run
|
10
|
+
|
11
|
+
bundle install
|
12
|
+
|
13
|
+
== Description
|
14
|
+
|
15
|
+
When you do group in Rails (SQL GROUP BY), count will return an ActiveSupport::OrderedHash. Sometimes you want the sum count (count of all the results, not by group). You can do count.keys.count, however this has serious performance issues if you have a lot of groups in the results.
|
16
|
+
|
17
|
+
I wrote an ActiveRecord scope that will override the count method to perform a more optimised count query (of course it will now return a FixNum instead of a Hash). You can use this in combination with Kaminari and get much more efficient calculation of total_pages and total_count.
|
18
|
+
|
19
|
+
|
20
|
+
== Use
|
21
|
+
|
22
|
+
Just use the returns_count_sum scope before calling count:
|
23
|
+
|
24
|
+
MyModel.group(:something).returns_count_sum.count
|
25
|
+
|
26
|
+
You can use it with Kaminari too (it's much faster):
|
27
|
+
|
28
|
+
MyModel.group(:something).returns_count_sum.page(1).per(10).total_pages
|
29
|
+
|
30
|
+
== Tests
|
31
|
+
|
32
|
+
I've tested this project using RSpec.
|
33
|
+
|
34
|
+
You can run the tests by running
|
35
|
+
|
36
|
+
rake spec
|
37
|
+
|
38
|
+
in the root directory of this gem (so you have to clone it first).
|
39
|
+
|
40
|
+
== Problems
|
41
|
+
|
42
|
+
If you have problems open an issue here on Github.
|
43
|
+
|
44
|
+
== License
|
45
|
+
|
46
|
+
This project rocks and uses MIT-LICENSE.
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'ActiveRecordGroupCount'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
Bundler::GemHelper.install_tasks
|
24
|
+
|
25
|
+
require 'rspec/core/rake_task'
|
26
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
27
|
+
spec.rspec_opts = %w(--color)
|
28
|
+
spec.pattern = 'spec{,/*/**}/*_spec.rb'
|
29
|
+
end
|
30
|
+
|
31
|
+
task :default => [:spec]
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActiveRecordGroupCount
|
2
|
+
module Scope
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ExtensionMethods
|
6
|
+
def count(*args)
|
7
|
+
scope = except(:select).select("1")
|
8
|
+
query = "SELECT count(*) AS count_all FROM (#{scope.to_sql}) x"
|
9
|
+
ActiveRecord::Base.connection.execute(query).first.try(:[], "count_all").to_i
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def returns_count_sum
|
15
|
+
scoped.extending(ExtensionMethods)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
def create(attrs)
|
4
|
+
TestModel.new.tap do |model|
|
5
|
+
model.assign_attributes(attrs, without_protection: true)
|
6
|
+
model.save
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe ActiveRecordGroupCount::Scope do
|
11
|
+
before do
|
12
|
+
5.times { |i| create group_id: 1, value: i }
|
13
|
+
3.times { |i| create group_id: 2, value: i }
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:scope) { TestModel.group(:group_id) }
|
17
|
+
|
18
|
+
it { scope.count.class.should == ActiveSupport::OrderedHash }
|
19
|
+
it { scope.count[1].should == 5 }
|
20
|
+
it { scope.count[2].should == 3 }
|
21
|
+
it { scope.pluck('MAX(value)').should == [4, 2] }
|
22
|
+
|
23
|
+
it { scope.returns_count_sum.count.should == 2 }
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_group_count
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jan Berdajs
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.1.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.1.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: psych
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.11'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.11'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: database_cleaner
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Efficient Rails group count if you need the summed up count instead of
|
95
|
+
the hash. Can be used with Kaminari to speed up total_pages and total_count.
|
96
|
+
email:
|
97
|
+
- mrbrdo@gmail.com
|
98
|
+
executables: []
|
99
|
+
extensions: []
|
100
|
+
extra_rdoc_files: []
|
101
|
+
files:
|
102
|
+
- lib/active_record_group_count/railtie.rb
|
103
|
+
- lib/active_record_group_count/scope.rb
|
104
|
+
- lib/active_record_group_count/version.rb
|
105
|
+
- lib/active_record_group_count.rb
|
106
|
+
- MIT-LICENSE
|
107
|
+
- Rakefile
|
108
|
+
- README.rdoc
|
109
|
+
- spec/active_record_group_count/active_record_group_count_spec.rb
|
110
|
+
homepage: https://github.com/mrbrdo/active_record_group_count
|
111
|
+
licenses: []
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 1.8.25
|
131
|
+
signing_key:
|
132
|
+
specification_version: 3
|
133
|
+
summary: Efficient Rails group count if you need the summed up count instead of the
|
134
|
+
hash.
|
135
|
+
test_files:
|
136
|
+
- spec/active_record_group_count/active_record_group_count_spec.rb
|