active_record_distinct_on 1.3.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8429fb36902084f4300e9f4edc35399afa40ab14a54da89112803b85c1a6a945
|
4
|
+
data.tar.gz: 3ab332e86216c13e2d9989e5c94183d2c94d2c0d4af8118619180311e3e91369
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fa6726a35706a2cfdc8e3aeafe5cb823d51cfe5281a59b0f84a5beebb2bca187e07cb2e257540234ffda58120bca3767030d4d54e2c00025ba1f9bbf4d56280
|
7
|
+
data.tar.gz: 98ac18e4bbbd862bb313d4cac040b1a2244c57400f934a22069712e95fadd49b1e4d599d114b54f49f22b743e27ab93929aa6dd6c6d2effbd45b5184e3b8cb61
|
@@ -28,27 +28,57 @@ module ActiveRecordDistinctOn
|
|
28
28
|
|
29
29
|
def distinct_on!(*fields)
|
30
30
|
fields.flatten!
|
31
|
-
fields.map! { |field|
|
32
|
-
if klass.attribute_alias?(field)
|
33
|
-
arel_table[klass.attribute_alias(field).to_sym]
|
34
|
-
else
|
35
|
-
arel_table[field]
|
36
|
-
end
|
37
|
-
}
|
38
31
|
self.distinct_on_values += fields
|
39
32
|
self
|
40
33
|
end
|
41
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
|
+
|
42
58
|
private
|
43
59
|
|
44
60
|
def build_arel(*)
|
45
61
|
super.tap do |arel|
|
46
|
-
build_distinct_on(arel
|
62
|
+
build_distinct_on(arel)
|
47
63
|
end
|
48
64
|
end
|
49
65
|
|
50
|
-
def build_distinct_on(arel
|
51
|
-
|
66
|
+
def build_distinct_on(arel)
|
67
|
+
return if distinct_on_values.empty?
|
68
|
+
|
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|
|
74
|
+
if klass.attribute_alias?(field)
|
75
|
+
arel_table[klass.attribute_alias(field).to_sym]
|
76
|
+
else
|
77
|
+
arel_table[field]
|
78
|
+
end
|
79
|
+
}
|
80
|
+
|
81
|
+
arel_columns arel_attributes
|
52
82
|
end
|
53
83
|
end
|
54
84
|
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
|
+
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:
|
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.
|
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
|