hightop 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c663a3196a056d66875c95ac2ba74bd54f37671a
4
- data.tar.gz: 6c0e8e59cafcfc7e9efa5e3680e55f093bff9758
3
+ metadata.gz: a5b161bc2d5fcb7b053be0581fe018c56fd818a0
4
+ data.tar.gz: 304d42b406499cbbd396948e1816369353f889f0
5
5
  SHA512:
6
- metadata.gz: fbcb31e2cd64bf75a8ba6352321d3d85bc93a43548281c3d158cc7132c5aee732c8b065506cc22c248811109f24970df5571f842427bcf6ef169d5fb30020445
7
- data.tar.gz: fdcd9b045ba3782787714d38f502d53e991af8f2f5ed26832fddf4610b8d1dfebc15c03fedc253d6b305eefaed965c3e859048d07e5dbe79ee8c4143803911e6
6
+ metadata.gz: 3a47145bd2f6858502a0e02314a04ce0c151924366f4531d15145fc98d171d77ab03dce2cf36bfa5f0a299e100e27d503d100cee8f14a7c4bcbb81b14bd7b9d7
7
+ data.tar.gz: d2c71a0da54bb8a9151c6ff71eb70f0587f1282d436b2dc7dd0af7aed8bef528f7bcfe582c454e4f239d20960e83c4a36af6fe012a1372ffefd2df01745b9fb6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.1
2
+
3
+ - Added `uniq` option
4
+ - Fixed `Model.limit(n).top`
5
+
1
6
  ## 0.1.0
2
7
 
3
8
  - No changes, just bump
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Hightop
2
2
 
3
- Super convenient group count
3
+ A nice shortcut for group count queries
4
4
 
5
5
  ```ruby
6
6
  Visit.top(:browser)
@@ -38,6 +38,12 @@ And expressions
38
38
  Visit.top("LOWER(referring_domain)")
39
39
  ```
40
40
 
41
+ And distinct
42
+
43
+ ```ruby
44
+ Visit.top(:city, uniq: :user_id)
45
+ ```
46
+
41
47
  ## Installation
42
48
 
43
49
  Add this line to your application’s Gemfile:
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
2
3
 
4
+ task :default => :test
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
data/hightop.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Hightop::VERSION
9
9
  spec.authors = ["Andrew Kane"]
10
10
  spec.email = ["andrew@chartkick.com"]
11
- spec.summary = %q{Super convenient group count}
12
- spec.description = %q{Super convenient group count}
11
+ spec.summary = %q{A nice shortcut for group count queries}
12
+ spec.description = %q{A nice shortcut for group count queries}
13
13
  spec.homepage = "https://github.com/ankane/hightop"
14
14
  spec.license = "MIT"
15
15
 
@@ -18,6 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "activerecord"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.6"
22
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "sqlite3"
23
27
  end
@@ -1,3 +1,3 @@
1
1
  module Hightop
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/hightop.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "hightop/version"
2
+ require "active_record"
2
3
 
3
4
  module Hightop
4
5
 
@@ -9,7 +10,10 @@ module Hightop
9
10
  end
10
11
 
11
12
  order_str = column.is_a?(Array) ? column.map(&:to_s).join(", ") : column
12
- relation = group(column).limit(limit).order("count_all DESC, #{order_str}")
13
+ relation = group(column).order("count_#{options[:uniq] || "all"} DESC, #{order_str}")
14
+ if limit
15
+ relation = relation.limit(limit)
16
+ end
13
17
 
14
18
  unless options[:nil]
15
19
  (column.is_a?(Array) ? column : [column]).each do |c|
@@ -17,7 +21,11 @@ module Hightop
17
21
  end
18
22
  end
19
23
 
20
- relation.count
24
+ if options[:uniq]
25
+ relation.uniq.count(options[:uniq])
26
+ else
27
+ relation.count
28
+ end
21
29
  end
22
30
 
23
31
  end
@@ -0,0 +1,83 @@
1
+ require_relative "test_helper"
2
+
3
+ class TestHightop < Minitest::Test
4
+
5
+ def setup
6
+ Visit.delete_all
7
+ end
8
+
9
+ def test_top
10
+ create_city("San Francisco", 3)
11
+ create_city("Chicago", 2)
12
+ expected = {
13
+ "San Francisco" => 3,
14
+ "Chicago" => 2
15
+ }
16
+ assert_equal expected, Visit.top(:city)
17
+ end
18
+
19
+ def test_limit
20
+ create_city("San Francisco", 3)
21
+ create_city("Chicago", 2)
22
+ create_city("Boston", 1)
23
+ expected = {
24
+ "San Francisco" => 3,
25
+ "Chicago" => 2
26
+ }
27
+ assert_equal expected, Visit.top(:city, 2)
28
+ assert_equal expected, Visit.limit(2).top(:city)
29
+ end
30
+
31
+ def test_nil_values
32
+ create_city("San Francisco", 3)
33
+ create_city(nil, 2)
34
+ expected = {
35
+ "San Francisco" => 3,
36
+ }
37
+ assert_equal expected, Visit.top(:city)
38
+ end
39
+
40
+ def test_nil_option
41
+ create_city("San Francisco", 3)
42
+ create_city(nil, 2)
43
+ expected = {
44
+ "San Francisco" => 3,
45
+ nil => 2
46
+ }
47
+ assert_equal expected, Visit.top(:city, nil: true)
48
+ end
49
+
50
+ def test_multiple_groups
51
+ create_city("San Francisco")
52
+ expected = {
53
+ ["San Francisco", "San Francisco"] => 1
54
+ }
55
+ assert_equal expected, Visit.top([:city, :city])
56
+ end
57
+
58
+ def test_expressions
59
+ create_city("San Francisco")
60
+ expected = {
61
+ "san francisco" => 1
62
+ }
63
+ assert_equal expected, Visit.top("LOWER(city)")
64
+ end
65
+
66
+ def test_uniq
67
+ create({city: "San Francisco", user_id: 1})
68
+ create({city: "San Francisco", user_id: 1})
69
+ expected = {
70
+ "San Francisco" => 1
71
+ }
72
+ assert_equal expected, Visit.top(:city, uniq: :user_id)
73
+ end
74
+
75
+ def create_city(city, count = 1)
76
+ create({city: city}, count)
77
+ end
78
+
79
+ def create(attributes, count = 1)
80
+ count.times{ Visit.create!(attributes) }
81
+ end
82
+
83
+ end
@@ -0,0 +1,19 @@
1
+ require "bundler/setup"
2
+ Bundler.require(:default)
3
+ require "minitest/autorun"
4
+ require "minitest/pride"
5
+ require "logger"
6
+
7
+ # for debugging
8
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
9
+
10
+ # migrations
11
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
12
+
13
+ ActiveRecord::Migration.create_table :visits do |t|
14
+ t.string :city
15
+ t.string :user_id
16
+ end
17
+
18
+ class Visit < ActiveRecord::Base
19
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hightop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-12 00:00:00.000000000 Z
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,7 +52,35 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
- description: Super convenient group count
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A nice shortcut for group count queries
42
84
  email:
43
85
  - andrew@chartkick.com
44
86
  executables: []
@@ -54,6 +96,8 @@ files:
54
96
  - hightop.gemspec
55
97
  - lib/hightop.rb
56
98
  - lib/hightop/version.rb
99
+ - test/hightop_test.rb
100
+ - test/test_helper.rb
57
101
  homepage: https://github.com/ankane/hightop
58
102
  licenses:
59
103
  - MIT
@@ -77,5 +121,7 @@ rubyforge_project:
77
121
  rubygems_version: 2.2.2
78
122
  signing_key:
79
123
  specification_version: 4
80
- summary: Super convenient group count
81
- test_files: []
124
+ summary: A nice shortcut for group count queries
125
+ test_files:
126
+ - test/hightop_test.rb
127
+ - test/test_helper.rb