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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 857a123c7746b0ebd13d93156ced020958521e5c
4
- data.tar.gz: 77645290416e092257151a2ecc2420f4e7405fa6
3
+ metadata.gz: 3bf2b49f0a63f435b0f58e0658e018a3d738728f
4
+ data.tar.gz: f8985015577b6c039a5fca1f6ca0a3f42bbf95a8
5
5
  SHA512:
6
- metadata.gz: 2c362a8eda27b6c42bfeefef6af4a201800985823e7efaa08efecde260d2c08c7c1da55b41c46fcba7a05e25e7f2bcff1098a031fbc663ae47a5b91acae4f769
7
- data.tar.gz: 1eb23a9207d260b706637e22b5ccbd9f47df50e8c4a48c5596204d163d27f9b63c78882a624aaa79b9629cdf4c72ecd8b6a07811e22bb76b19876f6fe24dcc33
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
@@ -1,3 +1,3 @@
1
1
  class ActiveRecordQueryFixer
2
- VERSION = "0.0.1".freeze
2
+ VERSION = "0.0.2".freeze
3
3
  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.1
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-15 00:00:00.000000000 Z
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.