order_as_specified 1.4 → 1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 810fb1d84c1606e6a005b15d822d6758a93caf1ae53b9df6a695ff8d2e024582
4
- data.tar.gz: ce7d3be0ef5a951a65bb1e5ceae0aa07ec3e0957fc844a6ecd2d0ca02af9170d
3
+ metadata.gz: c11fb5ed7165ea2a50cdab0cdb421ac0faa8a187539b0c1f661d6f64fe8107ed
4
+ data.tar.gz: 975e533ab7a733c8f3f68c7b6363dab96ac1d90a08f4223ba7bcc067443d21a8
5
5
  SHA512:
6
- metadata.gz: 7e8e491197afaf309031af8d7eb7869d1acdd2884ef776e6440a137a5288601993164ddc31815a42703407d18d51f73c917577cc80dbde2c742f7b6b7e06ab10
7
- data.tar.gz: d8437b518f99ae3a9d6f75069e927b1a677772957d6868c9d3a24f49dd956c3a66f62f31174ae5796447a0b6806b1c15294ad94591ddddf415ca0a6c24ba190e
6
+ metadata.gz: 8b56240f7f9527be2dfb266583ffcccd7aa8992da08139e1824f19c1ea2b82266b5a88bcbbe87db740571d07cd53b4c20bad5d67d4bd4c56d2d3fa0fc132c0cc
7
+ data.tar.gz: cc12430e2ef88f18225235de1900a0e1662a908da3f2f411cb64fcf9336a82f6fad886ceb99f840c718d140b832002f47aafaebc807844cdcd5cfe51ba1e8a90
data/.travis.yml CHANGED
@@ -14,9 +14,9 @@ after_script:
14
14
  - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
15
15
  addons:
16
16
  postgresql: "9.3"
17
- code_climate:
18
- repo_token: 781c439d68cbb928316deaec1c7136f98423e1db87238f99cbc95183de94df9e
19
17
  env:
18
+ global:
19
+ CC_TEST_REPORTER_ID=781c439d68cbb928316deaec1c7136f98423e1db87238f99cbc95183de94df9e
20
20
  matrix:
21
21
  - AR_VERSION="~> 5.0.0"
22
22
  - AR_VERSION="~> 5.1.0"
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 1.5
2
+
3
+ This release improves performance by switching to use `CASE` statements. Huge
4
+ thanks to [Yen-Nan (Maso) Lin](https://github.com/masolin) for this improvement!
5
+
1
6
  # 1.4
2
7
 
3
8
  This release removes deprecation warnings for ActiveRecord 5.2 users, and drops
data/README.md CHANGED
@@ -26,8 +26,8 @@ TestObject.order_as_specified(language: ["es", "en", "fr"])
26
26
 
27
27
  Other gems like `ranked-model`, `acts_as_sortable`, etc. assume you want the
28
28
  same ordering each time, and store data to keep track of this in the database.
29
- They're great at what they do, but if your desired ordering changes, or if you
30
- don't always want an ordering, this gem is your friend.
29
+ They're great at what they do, but if you want to change the ordering, or if
30
+ you don't always want an ordering, this gem is your friend.
31
31
 
32
32
  ## Installation
33
33
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OrderAsSpecified
4
- VERSION = "1.4"
4
+ VERSION = "1.5"
5
5
  end
@@ -13,6 +13,7 @@ module OrderAsSpecified
13
13
  def order_as_specified(hash)
14
14
  distinct_on = hash.delete(:distinct_on)
15
15
  params = extract_params(hash)
16
+ return all if params[:values].empty?
16
17
 
17
18
  table = connection.quote_table_name(params[:table])
18
19
  attribute = connection.quote_column_name(params[:attribute])
@@ -28,11 +29,15 @@ module OrderAsSpecified
28
29
  "#{table}.#{attribute}=#{db_connection.quote(value)}"
29
30
  end
30
31
 
31
- scope = order(Arel.sql(conditions.map { |cond| "#{cond} DESC" }.join(", ")))
32
+ when_queries = conditions.map.with_index do |cond, index|
33
+ "WHEN #{cond} THEN #{index}"
34
+ end
35
+ case_query = "CASE #{when_queries.join(' ')} ELSE #{conditions.size} END"
36
+ scope = order(Arel.sql("#{case_query} ASC"))
32
37
 
33
38
  if distinct_on
34
39
  scope = scope.select(
35
- Arel.sql("DISTINCT ON (#{conditions.join(', ')}) #{table}.*")
40
+ Arel.sql("DISTINCT ON (#{case_query}) #{table}.*")
36
41
  )
37
42
  end
38
43
 
@@ -62,7 +62,8 @@ RSpec.describe "PostgreSQL" do
62
62
  distinct_on: true,
63
63
  table => { column => ["foo"] }
64
64
  ).to_sql
65
- expect(sql).to include("DISTINCT ON (#{quoted_table}.#{quoted_column}")
65
+ pattern = "DISTINCT ON (CASE WHEN #{quoted_table}.#{quoted_column}"
66
+ expect(sql).to include(pattern)
66
67
  end
67
68
  end
68
69
  end
@@ -65,6 +65,22 @@ RSpec.shared_examples ".order_as_specified" do
65
65
  end
66
66
  end
67
67
 
68
+ context "when the order is empty array" do
69
+ subject { TestClass.order_as_specified(field: []) }
70
+
71
+ let(:test_objects) do
72
+ Array.new(5) do |i|
73
+ TestClass.create(field: "Field #{i}")
74
+ end
75
+ end
76
+
77
+ it "keep the original order" do
78
+ test_objects # Build test objects
79
+ expect(subject.map(&:id)).
80
+ to eq test_objects.map(&:id)
81
+ end
82
+ end
83
+
68
84
  context "with another table name specified" do
69
85
  subject do
70
86
  TestClass.
@@ -129,7 +145,8 @@ RSpec.shared_examples ".order_as_specified" do
129
145
  quoted_column = AssociationTestClass.connection.quote_column_name(column)
130
146
 
131
147
  sql = TestClass.order_as_specified(table => { column => ["foo"] }).to_sql
132
- expect(sql).to include("ORDER BY #{quoted_table}.#{quoted_column}")
148
+ pattern = "ORDER BY CASE WHEN #{quoted_table}.#{quoted_column}"
149
+ expect(sql).to include(pattern)
133
150
  end
134
151
  end
135
152
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: order_as_specified
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.4'
4
+ version: '1.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Evelyn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-14 00:00:00.000000000 Z
11
+ date: 2018-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord