active_record_query_fixer 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_record_query_fixer.rb +13 -6
- data/lib/active_record_query_fixer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bf2b49f0a63f435b0f58e0658e018a3d738728f
|
4
|
+
data.tar.gz: f8985015577b6c039a5fca1f6ca0a3f42bbf95a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f55ef85d975ec9a09126a88f030a50ea8b1a9f2f61332db9e4eca55a3cbb69ffb22d5a3484ab9e9a5781a69d603e298e96c29d333fc69f2abe92cc32a7b57b5f
|
7
|
+
data.tar.gz: 27670b512d1c16e700145195d791cc81c90e8c9e66ef6e064523a39b49ce8f282a0cdc5e25c613b1c35fd5e4ea107b9a5febd394277d2334bd4d30a08a94c5cb
|
@@ -7,16 +7,16 @@ class ActiveRecordQueryFixer
|
|
7
7
|
|
8
8
|
def initialize(args)
|
9
9
|
@query = args.fetch(:query)
|
10
|
+
@count_select = 0
|
10
11
|
end
|
11
12
|
|
12
13
|
def fix
|
13
|
-
fix_order_group
|
14
|
-
fix_order_select_distinct
|
14
|
+
fix_order_group if fix_order_group?
|
15
|
+
fix_order_select_distinct if fix_order_select_distinct?
|
15
16
|
self
|
16
17
|
end
|
17
18
|
|
18
19
|
def fix_order_group
|
19
|
-
return if @query.values[:joins].blank?
|
20
20
|
@query = @query.group("#{@query.model.table_name}.#{@query.model.primary_key}")
|
21
21
|
|
22
22
|
@query.values[:order].each do |order|
|
@@ -27,12 +27,11 @@ class ActiveRecordQueryFixer
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def fix_order_select_distinct
|
30
|
-
return unless @query.values[:distinct]
|
31
|
-
|
32
30
|
changed = false
|
33
31
|
@query.values[:order].each do |order|
|
34
|
-
@query = @query.select(extract_table_and_column_from_expression(order))
|
32
|
+
@query = @query.select("#{extract_table_and_column_from_expression(order)} AS active_record_query_fixer_#{@count_select}")
|
35
33
|
changed = true
|
34
|
+
@count_select += 1
|
36
35
|
end
|
37
36
|
|
38
37
|
@query = @query.select("#{@query.table_name}.*") if changed
|
@@ -54,4 +53,12 @@ private
|
|
54
53
|
raise "Couldn't extract table and column from: #{order}"
|
55
54
|
end
|
56
55
|
end
|
56
|
+
|
57
|
+
def fix_order_group?
|
58
|
+
@query.values[:joins].blank? && @query.values[:distinct].present?
|
59
|
+
end
|
60
|
+
|
61
|
+
def fix_order_select_distinct?
|
62
|
+
@query.values[:distinct].present?
|
63
|
+
end
|
57
64
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_query_fixer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaspernj
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A library for automatically added `.select` on a column used for `.distinct`
|
14
14
|
or automatically adding `.group` for a column used for order.
|