activerecord-summarize 0.2.3 → 0.3.0

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
  SHA256:
3
- metadata.gz: 86517402b9294b544b4d452e8198a49e23148141bf1614cdedff938970a32213
4
- data.tar.gz: d87578a41a70e44cce3efea3114a1e465ecf2428af4f2aa8be86556fff55a655
3
+ metadata.gz: fbba2c0577555f891c1f860b76cc9f0194504d47d59922c8337afb9a1b3ab1a0
4
+ data.tar.gz: 3c1945b1652a4305ec4df2d13a0c5a6966e308018d2b34c5c21e396f02a1e31c
5
5
  SHA512:
6
- metadata.gz: fd7587584640872da28f5cf78d16f547f6965d9227f665a65658a1ecf5349ad1cb9bb06ddcba57e777acda9d03133f5f83a9bf0095b0e24c853428d98db28ed0
7
- data.tar.gz: 42f7a3c1f745f41edb882d681c85ea02c3672739a4edff781b6af0756c99ff0923348b89438969392a2b659ab3bf4613acc9454c5817ef278db0684918de3521
6
+ metadata.gz: a2a9e27420e30861c5c713e757ae66ead1af9d5aa9e10b259620b10d9289652464fc561745e720bae100cbda1b265e9f551cfd1f05c78f53c8dae39bfd788c33
7
+ data.tar.gz: 7e8632ae8ced794bdfb539624a64ebeef8d4a0a6568e3f625cca13b0d669365dd309981d351649c2af3a33f21c45e0fed2848853d581681dd0656d55b8163005
data/Gemfile CHANGED
@@ -6,7 +6,8 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
-
10
9
  gem "minitest", "~> 5.0"
11
-
12
10
  gem "standard", "~> 1.3"
11
+
12
+ gem "activerecord", "7.0.3"
13
+ gem "sqlite3", "1.4.2"
data/Gemfile.lock CHANGED
@@ -1,24 +1,24 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-summarize (0.2.3)
4
+ activerecord-summarize (0.3.0)
5
5
  activerecord (>= 5.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (7.0.2.2)
11
- activesupport (= 7.0.2.2)
12
- activerecord (7.0.2.2)
13
- activemodel (= 7.0.2.2)
14
- activesupport (= 7.0.2.2)
15
- activesupport (7.0.2.2)
10
+ activemodel (7.0.3)
11
+ activesupport (= 7.0.3)
12
+ activerecord (7.0.3)
13
+ activemodel (= 7.0.3)
14
+ activesupport (= 7.0.3)
15
+ activesupport (7.0.3)
16
16
  concurrent-ruby (~> 1.0, >= 1.0.2)
17
17
  i18n (>= 1.6, < 2)
18
18
  minitest (>= 5.1)
19
19
  tzinfo (~> 2.0)
20
20
  ast (2.4.2)
21
- concurrent-ruby (1.1.9)
21
+ concurrent-ruby (1.1.10)
22
22
  i18n (1.10.0)
23
23
  concurrent-ruby (~> 1.0)
24
24
  minitest (5.15.0)
@@ -44,6 +44,7 @@ GEM
44
44
  rubocop (>= 1.7.0, < 2.0)
45
45
  rubocop-ast (>= 0.4.0)
46
46
  ruby-progressbar (1.11.0)
47
+ sqlite3 (1.4.2)
47
48
  standard (1.7.2)
48
49
  rubocop (= 1.25.1)
49
50
  rubocop-performance (= 1.13.2)
@@ -56,9 +57,11 @@ PLATFORMS
56
57
  x86_64-linux
57
58
 
58
59
  DEPENDENCIES
60
+ activerecord (= 7.0.3)
59
61
  activerecord-summarize!
60
62
  minitest (~> 5.0)
61
63
  rake (~> 13.0)
64
+ sqlite3 (= 1.4.2)
62
65
  standard (~> 1.3)
63
66
 
64
67
  BUNDLED WITH
@@ -11,6 +11,8 @@ For each subreddit that a user moderates, the user should see these stats with r
11
11
  - count of how many posts from this period were buried, i.e., ended up with negative karma
12
12
  - grouped by post creation date, the percentage of posts that ended up being popular, where popular means having a karma score greater than a per-subreddit-configured threshold
13
13
  - grouped by post creation day of the week, the average number of comments per non-buried post
14
+
15
+ > *Below, grouping by day of the week is handled with `.group("EXTRACT(DOW FROM posts.created_at)")`*
14
16
 
15
17
  ## Background
16
18
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Summarize
5
- VERSION = "0.2.3"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
@@ -137,6 +137,10 @@ module ActiveRecord::Summarize
137
137
  starting_values, reducers = @calculations.each_with_index.map do |f, i|
138
138
  value_column = groups.size + i
139
139
  group_columns = f.relation.group_values.map { |k| group_idx[k] }
140
+ # `row[value_column] || 0` pattern in reducers because SQL SUM(NULL)
141
+ # returns NULL, but like ActiveRecord we always want .sum to return a
142
+ # number, and our "starting_values and reducers" implementation means
143
+ # we sometimes will have to add NULL to our numbers.
140
144
  case group_columns.size
141
145
  when 0 then [
142
146
  0,
@@ -145,14 +149,14 @@ module ActiveRecord::Summarize
145
149
  when 1 then [
146
150
  Hash.new(0), # Default 0 makes the reducer much cleaner, but we have to clean it up later
147
151
  ->(memo, row) {
148
- memo[row[group_columns[0]]] += row[value_column] unless row[value_column].zero?
152
+ memo[row[group_columns[0]]] += row[value_column] unless (row[value_column] || 0).zero?
149
153
  memo
150
154
  }
151
155
  ]
152
156
  else [
153
157
  Hash.new(0),
154
158
  ->(memo, row) {
155
- memo[group_columns.map { |i| row[i] }] += row[value_column] unless row[value_column].zero?
159
+ memo[group_columns.map { |i| row[i] }] += row[value_column] unless (row[value_column] || 0).zero?
156
160
  memo
157
161
  }
158
162
  ]
@@ -237,14 +241,14 @@ module ActiveRecord::Summarize
237
241
  def select_value(base_relation)
238
242
  where = relation.where_clause - base_relation.where_clause
239
243
  for_select = column
240
- for_select = Arel::Nodes::Case.new(where.ast).when(true, for_select).else(unmatch_value) unless where.empty?
244
+ for_select = Arel::Nodes::Case.new(where.ast).when(true, for_select).else(unmatch_arel_node) unless where.empty?
241
245
  function.new([for_select]).tap { |f| f.distinct = relation.distinct_value }
242
246
  end
243
247
 
244
- def unmatch_value
248
+ def unmatch_arel_node
245
249
  case method
246
- when "sum" then 0
247
- when "count" then nil
250
+ when "sum" then 0 # Adding zero to a sum does nothing
251
+ when "count" then nil # In SQL, null is no value and is not counted
248
252
  else raise "Unknown calculation method"
249
253
  end
250
254
  end
@@ -272,6 +276,7 @@ module ActiveRecord::Summarize
272
276
  case operation = operation.to_s.downcase
273
277
  when "count", "sum"
274
278
  column_name = :id if [nil, "*", :all].include? column_name
279
+ raise Unsummarizable, "DISTINCT in SQL is not reliably correct with summarize" if column_name.is_a?(String) && /\bdistinct\b/i === column_name
275
280
  @summarize.add_calculation(self, operation, aggregate_column(column_name))
276
281
  else super
277
282
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-summarize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Paine
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-01 00:00:00.000000000 Z
11
+ date: 2022-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord