hightop 0.2.3 → 0.2.4

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
  SHA256:
3
- metadata.gz: 340c609cb491de09f5c69f80b3e237126c081a8167565c78d6f28cac692cc34b
4
- data.tar.gz: 0aa4d15e3ac6503a73f664602d0d07f1a0e956202f93411ec51d663dcca9b2ff
3
+ metadata.gz: e986b06b74104ea5fb99fe92abdb061eab4cc225ba9fa4f34216eadcd704e556
4
+ data.tar.gz: 5ca8954b8ce39c1212e0bc0b651e22450ff26b17795665b561cc7e4bbb134067
5
5
  SHA512:
6
- metadata.gz: 31bcc5dcc655886f5dad2ebe27f33a7f56702b0503883bac779a944b502dc9bf1f27694c1eb83e4d36ca908fbb1bbe5e9b09706b5536f1ba53616e555204c579
7
- data.tar.gz: 1e4d59093c22c49d3166368d8b23386ca7366dd9fc1b31e813e9df81bdd5d3b3e7d87ece4d36bf6b94deef69d2cd093927c0d9eeb62582fd357feed0c381564d
6
+ metadata.gz: e9d93d7b9edcce9dcfb6f3b7912ef3252454731bcc68d45cc9ff391df854a6fe49e5d7ca269780e55e98cf1481a17ef3472427de69694d6098cfdcacf9e61c7d
7
+ data.tar.gz: cd86dfd59b77d39482d06d0a6e9f59869ca15eaab6eef6e5cc62f754ad9d3addd781e21749a192a181385620b2508b520ee22da1f2c397566ac82159778a65d2
@@ -1,6 +1,11 @@
1
+ ## 0.2.4 (2020-09-07)
2
+
3
+ - Added warning for non-attribute argument
4
+ - Added deprecation warning for `uniq`
5
+
1
6
  ## 0.2.3 (2020-06-18)
2
7
 
3
- - Dropped support for Rails 4.2 and Ruby 2.3
8
+ - Dropped support for Active Record 4.2 and Ruby 2.3
4
9
  - Fixed deprecation warning in Ruby 2.7
5
10
 
6
11
  ## 0.2.2 (2019-08-12)
@@ -6,6 +6,7 @@ require "hightop/enumerable"
6
6
  require "hightop/version"
7
7
 
8
8
  ActiveSupport.on_load(:active_record) do
9
+ require "hightop/utils"
9
10
  require "hightop/kicks"
10
11
  extend Hightop::Kicks
11
12
  end
@@ -1,22 +1,34 @@
1
1
  module Hightop
2
2
  module Kicks
3
3
  def top(column, limit = nil, distinct: nil, uniq: nil, min: nil, nil: nil)
4
+ warn "[hightop] uniq is deprecated. Use distinct instead" if uniq
5
+
6
+ columns = column.is_a?(Array) ? column : [column]
7
+ columns.each { |c| Utils.validate_column(c) }
8
+
4
9
  distinct ||= uniq
5
- order_str = column.is_a?(Array) ? column.map(&:to_s).join(", ") : column
6
- relation = group(column).order(["count_#{distinct || 'all'} DESC", order_str])
10
+ Utils.validate_column(distinct) if distinct
11
+
12
+ relation = group(*columns).order("1 DESC", *columns)
7
13
  if limit
8
14
  relation = relation.limit(limit)
9
15
  end
10
16
 
11
17
  # terribly named option
12
18
  unless binding.local_variable_get(:nil)
13
- (column.is_a?(Array) ? column : [column]).each do |c|
19
+ columns.each do |c|
20
+ c = Utils.resolve_column(self, c)
14
21
  relation = relation.where("#{c} IS NOT NULL")
15
22
  end
16
23
  end
17
24
 
18
25
  if min
19
- relation = relation.having("COUNT(#{distinct ? "DISTINCT #{distinct}" : '*'}) >= #{min.to_i}")
26
+ if distinct
27
+ d = Utils.resolve_column(self, distinct)
28
+ relation = relation.having("COUNT(DISTINCT #{d}) >= #{min.to_i}")
29
+ else
30
+ relation = relation.having("COUNT(*) >= #{min.to_i}")
31
+ end
20
32
  end
21
33
 
22
34
  if distinct
@@ -3,19 +3,23 @@ module Hightop
3
3
  # super helpful article
4
4
  # https://maximomussini.com/posts/mongoid-aggregation-dsl/
5
5
  def top(column, limit = nil, distinct: nil, uniq: nil, min: nil, nil: nil)
6
+ warn "[hightop] uniq is deprecated. Use distinct instead" if uniq
7
+
8
+ columns = column.is_a?(Array) ? column : [column]
9
+
6
10
  distinct ||= uniq
7
11
 
8
12
  relation = all
9
13
 
10
14
  # terribly named option
11
15
  unless binding.local_variable_get(:nil)
12
- (column.is_a?(Array) ? column : [column]).each do |c|
16
+ columns.each do |c|
13
17
  relation = relation.and(c.ne => nil)
14
18
  end
15
19
  end
16
20
 
17
21
  ids = {}
18
- Array(column).each_with_index do |c, i|
22
+ columns.each_with_index do |c, i|
19
23
  ids["c#{i}"] = "$#{c}"
20
24
  end
21
25
 
@@ -0,0 +1,21 @@
1
+ module Hightop
2
+ module Utils
3
+ class << self
4
+ # basic version of Active Record disallow_raw_sql!
5
+ # symbol = column (safe), Arel node = SQL (safe), other = untrusted
6
+ # matches table.column and column
7
+ def validate_column(column)
8
+ unless column.is_a?(Symbol) || column.is_a?(Arel::Nodes::SqlLiteral) || /\A\w+(\.\w+)?\z/i.match(column.to_s)
9
+ warn "[hightop] Non-attribute argument: #{column}. Use Arel.sql() for known-safe values. This will raise an error in Hightop 0.3.0"
10
+ end
11
+ end
12
+
13
+ # resolves eagerly
14
+ def resolve_column(relation, column)
15
+ node = relation.send(:relation).send(:arel_columns, [column]).first
16
+ node = Arel::Nodes::SqlLiteral.new(node) if node.is_a?(String)
17
+ relation.connection.visitor.accept(node, Arel::Collectors::SQLString.new).value
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Hightop
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
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.2.3
4
+ version: 0.2.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: 2020-06-18 00:00:00.000000000 Z
11
+ date: 2020-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -93,6 +93,7 @@ files:
93
93
  - lib/hightop/enumerable.rb
94
94
  - lib/hightop/kicks.rb
95
95
  - lib/hightop/mongoid.rb
96
+ - lib/hightop/utils.rb
96
97
  - lib/hightop/version.rb
97
98
  homepage: https://github.com/ankane/hightop
98
99
  licenses: