order_as_specified 1.4 → 1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -2
- data/CHANGELOG.md +5 -0
- data/README.md +2 -2
- data/lib/order_as_specified/version.rb +1 -1
- data/lib/order_as_specified.rb +7 -2
- data/spec/postgresql_spec.rb +2 -1
- data/spec/shared/order_as_specified_examples.rb +18 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c11fb5ed7165ea2a50cdab0cdb421ac0faa8a187539b0c1f661d6f64fe8107ed
|
4
|
+
data.tar.gz: 975e533ab7a733c8f3f68c7b6363dab96ac1d90a08f4223ba7bcc067443d21a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
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
|
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
|
|
data/lib/order_as_specified.rb
CHANGED
@@ -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
|
-
|
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 (#{
|
40
|
+
Arel.sql("DISTINCT ON (#{case_query}) #{table}.*")
|
36
41
|
)
|
37
42
|
end
|
38
43
|
|
data/spec/postgresql_spec.rb
CHANGED
@@ -62,7 +62,8 @@ RSpec.describe "PostgreSQL" do
|
|
62
62
|
distinct_on: true,
|
63
63
|
table => { column => ["foo"] }
|
64
64
|
).to_sql
|
65
|
-
|
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
|
-
|
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
|
+
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-
|
11
|
+
date: 2018-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|