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: d2bdc5ffaa6c0bd2aed0a69fdab6a454a0ca1d5010907a91a3083c47f86e9f5f
4
- data.tar.gz: 4a14067373b241a5f9c816d6dca59edfcb73410aa88929e3d6c6bfc7f94a0d20
3
+ metadata.gz: 8429fb36902084f4300e9f4edc35399afa40ab14a54da89112803b85c1a6a945
4
+ data.tar.gz: 3ab332e86216c13e2d9989e5c94183d2c94d2c0d4af8118619180311e3e91369
5
5
  SHA512:
6
- metadata.gz: beaca47b08017c4c728c9d1ddfe30f480e88c5ef12a8905b18e2e6d7791eaee3cd1fa64028ecf89831d57bfe121c45eae29ff398dbfba54ab835ef55ae0ed4dd
7
- data.tar.gz: 3b43fad485e74d7c4e514475962e0280e5ee929a755ef74cc4bea013c5389ef7b6b1305ffdddd908058f7a92b635545a288d575af809bc74fd02bf481699f036
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, distinct_on_values)
62
+ build_distinct_on(arel)
47
63
  end
48
64
  end
49
65
 
50
- def build_distinct_on(arel, columns)
51
- arel.distinct_on arel_columns columns unless columns.empty?
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
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordDistinctOn
2
- VERSION = '1.3.0'
2
+ VERSION = '1.5.0'
3
3
  end
@@ -8,6 +8,7 @@ module ActiveRecordDistinctOn
8
8
  def self.install
9
9
  ActiveRecord::Relation.include DistinctOnQueryMethods
10
10
  ActiveRecord::Querying.delegate :distinct_on, to: :all
11
+ ActiveRecord::Relation::Merger::NORMAL_VALUES << :distinct_on
11
12
  end
12
13
  end
13
14
 
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.3.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-01-10 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.1.6
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