hightop 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8123384a25f9687e070f4ee86677c1afc5060a1f
4
- data.tar.gz: 3b3776446331302a639859ba54d12cfa77b450aa
3
+ metadata.gz: 19538edc7ac8f8cc81df7ad682c47ec6dfe0d38e
4
+ data.tar.gz: 2f2bb4d909ead9b64fa293627a784c5adeaadd2d
5
5
  SHA512:
6
- metadata.gz: 51c28c43483583167675608a90698f5a3e5b2b7bcf362278ab89b63a56c065d7adad51993e111005201b70e2e63ae5f7570db5e5eb1ce39cec1df085f32952ad
7
- data.tar.gz: 191b537904fcb730a4e313714f9168b475e8017a791131e60c9173ea4ec5c7a5e817e73324af5388c0161a106eb336e20da3b4a8e6074f2ed5bcc81db0d2dd8c
6
+ metadata.gz: ac01a53983e69a760c745bcddd99287774ae73d7b743dcf4be327b252c0524e3147b6e815a1560b735f6cdf2252d651402ea9b775468bd93300d056fdcea18d3
7
+ data.tar.gz: 37252ef2688c355c2aa327f36fd5025c7809986abfc256bb4f8ea0df48442c8add698d944261b2a0ee0d16ee5c3d52b352023ad7c334a6ec39fe80e80a3922f0
data/.travis.yml CHANGED
@@ -3,6 +3,8 @@ rvm:
3
3
  - 2.1
4
4
  gemfile:
5
5
  - Gemfile
6
+ sudo: false
7
+ before_install: gem install bundler
6
8
  script: bundle exec rake test
7
9
  notifications:
8
10
  email:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.1.4
2
+
3
+ - Added `distinct` option to replace `uniq`
4
+
1
5
  ## 0.1.3
2
6
 
3
7
  - Fixed `min` option with `uniq`
data/README.md CHANGED
@@ -43,7 +43,7 @@ Visit.top("LOWER(referring_domain)")
43
43
  And distinct
44
44
 
45
45
  ```ruby
46
- Visit.top(:city, uniq: :user_id)
46
+ Visit.top(:city, distinct: :user_id)
47
47
  ```
48
48
 
49
49
  And min count
data/lib/hightop.rb CHANGED
@@ -8,8 +8,9 @@ module Hightop
8
8
  limit = nil
9
9
  end
10
10
 
11
+ distinct = options[:distinct] || options[:uniq]
11
12
  order_str = column.is_a?(Array) ? column.map(&:to_s).join(", ") : column
12
- relation = group(column).order("count_#{options[:uniq] || 'all'} DESC, #{order_str}")
13
+ relation = group(column).order("count_#{distinct || 'all'} DESC, #{order_str}")
13
14
  if limit
14
15
  relation = relation.limit(limit)
15
16
  end
@@ -21,11 +22,16 @@ module Hightop
21
22
  end
22
23
 
23
24
  if options[:min]
24
- relation = relation.having("COUNT(#{options[:uniq] ? "DISTINCT #{options[:uniq]}" : '*'}) >= #{options[:min].to_i}")
25
+ relation = relation.having("COUNT(#{distinct ? "DISTINCT #{distinct}" : '*'}) >= #{options[:min].to_i}")
25
26
  end
26
27
 
27
- if options[:uniq]
28
- relation.uniq.count(options[:uniq])
28
+ if distinct
29
+ # since relation.respond_to?(:distinct) can't be used
30
+ if ActiveRecord::VERSION::MAJOR > 3
31
+ relation.distinct.count(distinct)
32
+ else
33
+ relation.uniq.count(distinct)
34
+ end
29
35
  else
30
36
  relation.count
31
37
  end
@@ -1,3 +1,3 @@
1
1
  module Hightop
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/test/hightop_test.rb CHANGED
@@ -62,6 +62,15 @@ class TestHightop < Minitest::Test
62
62
  assert_equal expected, Visit.top("LOWER(city)")
63
63
  end
64
64
 
65
+ def test_distinct
66
+ create(city: "San Francisco", user_id: 1)
67
+ create(city: "San Francisco", user_id: 1)
68
+ expected = {
69
+ "San Francisco" => 1
70
+ }
71
+ assert_equal expected, Visit.top(:city, distinct: :user_id)
72
+ end
73
+
65
74
  def test_uniq
66
75
  create(city: "San Francisco", user_id: 1)
67
76
  create(city: "San Francisco", user_id: 1)
@@ -80,6 +89,18 @@ class TestHightop < Minitest::Test
80
89
  assert_equal expected, Visit.top(:city, min: 3)
81
90
  end
82
91
 
92
+ def test_min_distinct
93
+ create(city: "San Francisco", user_id: 1)
94
+ create(city: "San Francisco", user_id: 1)
95
+ create(city: "San Francisco", user_id: 2)
96
+ create(city: "Chicago", user_id: 1)
97
+ create(city: "Chicago", user_id: 1)
98
+ expected = {
99
+ "San Francisco" => 2
100
+ }
101
+ assert_equal expected, Visit.top(:city, min: 2, distinct: :user_id)
102
+ end
103
+
83
104
  def create_city(city, count = 1)
84
105
  create({city: city}, count)
85
106
  end
data/test/test_helper.rb CHANGED
@@ -4,6 +4,8 @@ require "minitest/autorun"
4
4
  require "minitest/pride"
5
5
  require "logger"
6
6
 
7
+ Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test)
8
+
7
9
  # for debugging
8
10
  # ActiveRecord::Base.logger = Logger.new(STDOUT)
9
11
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hightop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-18 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.4.5
122
+ rubygems_version: 2.4.5.1
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: A nice shortcut for group count queries