active_record_distinct_on 1.4.0 → 1.5.0

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: '03806e7bb7b654ef57351d1b7123f4f07084e256eeb564a4d325b21e2f41bede'
4
- data.tar.gz: 889c8a393d6c907bae4bb78afe291521e7fafd74f4b77f6190d07cbe39197acc
3
+ metadata.gz: 8429fb36902084f4300e9f4edc35399afa40ab14a54da89112803b85c1a6a945
4
+ data.tar.gz: 3ab332e86216c13e2d9989e5c94183d2c94d2c0d4af8118619180311e3e91369
5
5
  SHA512:
6
- metadata.gz: 81b62213427b5a7bf5b18b63a508cd0cd454b6b6ae069817b28be2816c5c3df14698cd178589fbd7fcd693d74c3da664b7f867cf36c6ecffa3666bbc10b09ac5
7
- data.tar.gz: d61728b40cd13d642a6cdb0e73a5e4a9287878d6fbc31ca849eb4c02874bbfd4d467fe4764e862c8ce9e2a8b899724012549fc928a3a62eeef1316ead07297f7
6
+ metadata.gz: 1fa6726a35706a2cfdc8e3aeafe5cb823d51cfe5281a59b0f84a5beebb2bca187e07cb2e257540234ffda58120bca3767030d4d54e2c00025ba1f9bbf4d56280
7
+ data.tar.gz: 98ac18e4bbbd862bb313d4cac040b1a2244c57400f934a22069712e95fadd49b1e4d599d114b54f49f22b743e27ab93929aa6dd6c6d2effbd45b5184e3b8cb61
@@ -32,25 +32,53 @@ module ActiveRecordDistinctOn
32
32
  self
33
33
  end
34
34
 
35
+ def count(column_name = nil)
36
+ if distinct_on_values.empty?
37
+ super
38
+ else
39
+ if column_name && column_name != :all
40
+ raise ArgumentError,
41
+ "Cannot use column_name to .count for scopes that already specify `distinct_on` values"
42
+ end
43
+
44
+ # See https://github.com/rails/rails/pull/41622#issuecomment-1303078730
45
+ # We need to convert SQL that looks like "SELECT DISTINCT ON ( "dogs"."id" ) COUNT(*) FROM "dogs" ..."
46
+ # into SQL that looks like "SELECT COUNT(DISTINCT ( "dogs"."id" )) FROM "dogs" ...".
47
+ scope = spawn
48
+ scope.distinct_on_values = FROZEN_EMPTY_ARRAY
49
+
50
+ column_names = distinct_on_arel_columns.map do |col|
51
+ "\"#{col.relation.name}\".\"#{col.name}\""
52
+ end
53
+
54
+ scope.count("distinct #{column_names.join(', ')}")
55
+ end
56
+ end
57
+
35
58
  private
36
59
 
37
60
  def build_arel(*)
38
61
  super.tap do |arel|
39
- build_distinct_on(arel, distinct_on_values)
62
+ build_distinct_on(arel)
40
63
  end
41
64
  end
42
65
 
43
- def build_distinct_on(arel, columns)
44
- return if columns.empty?
66
+ def build_distinct_on(arel)
67
+ return if distinct_on_values.empty?
45
68
 
46
- arel_attributes = columns.map { |field|
69
+ arel.distinct_on(distinct_on_arel_columns)
70
+ end
71
+
72
+ def distinct_on_arel_columns
73
+ arel_attributes = distinct_on_values.map { |field|
47
74
  if klass.attribute_alias?(field)
48
75
  arel_table[klass.attribute_alias(field).to_sym]
49
76
  else
50
77
  arel_table[field]
51
78
  end
52
79
  }
53
- arel.distinct_on(arel_columns arel_attributes)
80
+
81
+ arel_columns arel_attributes
54
82
  end
55
83
  end
56
84
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordDistinctOn
2
- VERSION = '1.4.0'
2
+ VERSION = '1.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_distinct_on
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alec Larsen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-28 00:00:00.000000000 Z
11
+ date: 2023-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -128,6 +128,20 @@ dependencies:
128
128
  - - ">="
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: pg
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
131
145
  description: ActiveRecordDistinctOn adds support for `DISTINCT ON` to ActiveRecord.
132
146
  At the time of this writing, PostgreSQL is the only database which supports this
133
147
  syntax; however, this gem has been written with database independence in mind so
@@ -162,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
176
  - !ruby/object:Gem::Version
163
177
  version: '0'
164
178
  requirements: []
165
- rubygems_version: 3.2.31
179
+ rubygems_version: 3.2.22
166
180
  signing_key:
167
181
  specification_version: 4
168
182
  summary: Adds support for `DISTINCT ON` statements when querying with ActiveRecord