order_as_specified 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.overcommit.yml +8 -0
- data/.rubocop.yml +260 -0
- data/.travis.yml +12 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +125 -0
- data/lib/order_as_specified/version.rb +3 -0
- data/lib/order_as_specified.rb +50 -0
- data/order_as_specified.gemspec +31 -0
- data/spec/order_as_specified_spec.rb +87 -0
- data/spec/spec_helper.rb +48 -0
- metadata +171 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1e011d7a057022124433414e9da18da5578a809d
|
4
|
+
data.tar.gz: 331b7087e75d2610e695f86db3741db58ca46bc4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b1371f79ccd95c02c121775077b0779089cd570605180b19722e471949ee14f640d4aaeaf0c14305670c15fa8801388f7c801dac8aa83e83f904697dcf6beef
|
7
|
+
data.tar.gz: 9da69d269c2bdf3e01b9390d478234b0d07257aa7e8b06696211ec57748b513b0f2e2add820944f9ebb97cf85e9ce726cbb640e6dcc6c53198e0117063be9a80
|
data/.gitignore
ADDED
data/.overcommit.yml
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,260 @@
|
|
1
|
+
# This (http://c2.com/cgi/wiki?AbcMetric) is super obnoxious
|
2
|
+
AbcSize:
|
3
|
+
Enabled: false
|
4
|
+
|
5
|
+
AccessorMethodName:
|
6
|
+
Enabled: false
|
7
|
+
|
8
|
+
Alias:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
AllCops:
|
12
|
+
RunRailsCops: true
|
13
|
+
|
14
|
+
AmbiguousOperator:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
AmbiguousRegexpLiteral:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
ArrayJoin:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
AsciiComments:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
AsciiIdentifiers:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
AssignmentInCondition:
|
30
|
+
Enabled: true
|
31
|
+
|
32
|
+
Attr:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
BlockNesting:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
BracesAroundHashParameters:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
CaseEquality:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
CharacterLiteral:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
ClassLength:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
ClassVars:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
CollectionMethods:
|
54
|
+
PreferredMethods:
|
55
|
+
find: detect
|
56
|
+
reduce: inject
|
57
|
+
collect: map
|
58
|
+
find_all: select
|
59
|
+
|
60
|
+
ColonMethodCall:
|
61
|
+
Enabled: false
|
62
|
+
|
63
|
+
CommentAnnotation:
|
64
|
+
Enabled: false
|
65
|
+
|
66
|
+
ConditionPosition:
|
67
|
+
Enabled: false
|
68
|
+
|
69
|
+
CyclomaticComplexity:
|
70
|
+
Enabled: false
|
71
|
+
|
72
|
+
Delegate:
|
73
|
+
Enabled: false
|
74
|
+
|
75
|
+
DeprecatedClassMethods:
|
76
|
+
Enabled: false
|
77
|
+
|
78
|
+
DeprecatedHashMethods:
|
79
|
+
Enabled: false
|
80
|
+
|
81
|
+
Documentation:
|
82
|
+
Enabled: false
|
83
|
+
|
84
|
+
DotPosition:
|
85
|
+
EnforcedStyle: trailing
|
86
|
+
|
87
|
+
DoubleNegation:
|
88
|
+
Enabled: false
|
89
|
+
|
90
|
+
ElseLayout:
|
91
|
+
Enabled: false
|
92
|
+
|
93
|
+
EmptyLiteral:
|
94
|
+
Enabled: false
|
95
|
+
|
96
|
+
Encoding:
|
97
|
+
Enabled: false
|
98
|
+
|
99
|
+
EvenOdd:
|
100
|
+
Enabled: false
|
101
|
+
|
102
|
+
FileName:
|
103
|
+
Enabled: false
|
104
|
+
|
105
|
+
FlipFlop:
|
106
|
+
Enabled: false
|
107
|
+
|
108
|
+
FormatString:
|
109
|
+
Enabled: false
|
110
|
+
|
111
|
+
GlobalVars:
|
112
|
+
Enabled: false
|
113
|
+
|
114
|
+
GuardClause:
|
115
|
+
Enabled: false
|
116
|
+
|
117
|
+
HandleExceptions:
|
118
|
+
Enabled: false
|
119
|
+
|
120
|
+
IfUnlessModifier:
|
121
|
+
Enabled: false
|
122
|
+
|
123
|
+
IfWithSemicolon:
|
124
|
+
Enabled: false
|
125
|
+
|
126
|
+
InvalidCharacterLiteral:
|
127
|
+
Enabled: false
|
128
|
+
|
129
|
+
Lambda:
|
130
|
+
Enabled: false
|
131
|
+
|
132
|
+
LambdaCall:
|
133
|
+
Enabled: false
|
134
|
+
|
135
|
+
LineEndConcatenation:
|
136
|
+
Enabled: false
|
137
|
+
|
138
|
+
LineLength:
|
139
|
+
Max: 80
|
140
|
+
|
141
|
+
LiteralInCondition:
|
142
|
+
Enabled: false
|
143
|
+
|
144
|
+
LiteralInInterpolation:
|
145
|
+
Enabled: false
|
146
|
+
|
147
|
+
Loop:
|
148
|
+
Enabled: false
|
149
|
+
|
150
|
+
MethodLength:
|
151
|
+
Enabled: false
|
152
|
+
|
153
|
+
ModuleFunction:
|
154
|
+
Enabled: false
|
155
|
+
|
156
|
+
NegatedIf:
|
157
|
+
Enabled: false
|
158
|
+
|
159
|
+
NegatedWhile:
|
160
|
+
Enabled: false
|
161
|
+
|
162
|
+
Next:
|
163
|
+
Enabled: false
|
164
|
+
|
165
|
+
NilComparison:
|
166
|
+
Enabled: false
|
167
|
+
|
168
|
+
Not:
|
169
|
+
Enabled: false
|
170
|
+
|
171
|
+
NumericLiterals:
|
172
|
+
Enabled: false
|
173
|
+
|
174
|
+
OneLineConditional:
|
175
|
+
Enabled: false
|
176
|
+
|
177
|
+
OpMethod:
|
178
|
+
Enabled: false
|
179
|
+
|
180
|
+
ParameterLists:
|
181
|
+
Enabled: false
|
182
|
+
|
183
|
+
ParenthesesAsGroupedExpression:
|
184
|
+
Enabled: false
|
185
|
+
|
186
|
+
PercentLiteralDelimiters:
|
187
|
+
PreferredDelimiters:
|
188
|
+
'%': '{}'
|
189
|
+
|
190
|
+
PerceivedComplexity:
|
191
|
+
Enabled: false
|
192
|
+
|
193
|
+
PerlBackrefs:
|
194
|
+
Enabled: false
|
195
|
+
|
196
|
+
PredicateName:
|
197
|
+
Enabled: false
|
198
|
+
|
199
|
+
Proc:
|
200
|
+
Enabled: false
|
201
|
+
|
202
|
+
RaiseArgs:
|
203
|
+
Enabled: false
|
204
|
+
|
205
|
+
RedundantReturn:
|
206
|
+
AllowMultipleReturnValues: true
|
207
|
+
|
208
|
+
RegexpLiteral:
|
209
|
+
Enabled: false
|
210
|
+
|
211
|
+
RequireParentheses:
|
212
|
+
Enabled: false
|
213
|
+
|
214
|
+
SelfAssignment:
|
215
|
+
Enabled: false
|
216
|
+
|
217
|
+
SignalException:
|
218
|
+
EnforcedStyle: only_raise
|
219
|
+
|
220
|
+
SingleLineBlockParams:
|
221
|
+
Enabled: false
|
222
|
+
|
223
|
+
SingleLineMethods:
|
224
|
+
Enabled: false
|
225
|
+
|
226
|
+
SpecialGlobalVars:
|
227
|
+
Enabled: false
|
228
|
+
|
229
|
+
StringLiterals:
|
230
|
+
EnforcedStyle: double_quotes
|
231
|
+
|
232
|
+
Style/MultilineBlockChain:
|
233
|
+
Enabled: false
|
234
|
+
|
235
|
+
VariableInterpolation:
|
236
|
+
Enabled: false
|
237
|
+
|
238
|
+
TrailingComma:
|
239
|
+
Enabled: false
|
240
|
+
|
241
|
+
TrivialAccessors:
|
242
|
+
Enabled: false
|
243
|
+
|
244
|
+
UnderscorePrefixedVariableName:
|
245
|
+
Enabled: false
|
246
|
+
|
247
|
+
VariableInterpolation:
|
248
|
+
Enabled: false
|
249
|
+
|
250
|
+
Void:
|
251
|
+
Enabled: false
|
252
|
+
|
253
|
+
WhenThen:
|
254
|
+
Enabled: false
|
255
|
+
|
256
|
+
WhileUntilModifier:
|
257
|
+
Enabled: false
|
258
|
+
|
259
|
+
WordArray:
|
260
|
+
Enabled: false
|
data/.travis.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.2.1
|
4
|
+
script: bundle exec rspec
|
5
|
+
addons:
|
6
|
+
code_climate:
|
7
|
+
repo_token: 781c439d68cbb928316deaec1c7136f98423e1db87238f99cbc95183de94df9e
|
8
|
+
notifications:
|
9
|
+
email: false
|
10
|
+
hipchat:
|
11
|
+
rooms:
|
12
|
+
secure: pLL6WXsWnvigZNgcYYwr0el3AG3WbRXOV4zAoBx/pAD/y51/KOjWboUO5szOqIicdSBJTIY5el7wK4uUi5elseumjl1jvOQSG7izvCGzDekuJuOTj9f6MdLtigbIaWO5/NWtXw0/JGHDQJpB4HyOv1mGAjQ3Y6MKxMNv+RUsgRI=
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Panorama Education
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
[![Code Climate](https://codeclimate.com/github/panorama-ed/order_as_specified/badges/gpa.svg)](https://codeclimate.com/github/panorama-ed/order_as_specified) [![Test Coverage](https://codeclimate.com/github/panorama-ed/order_as_specified/badges/coverage.svg)](https://codeclimate.com/github/panorama-ed/order_as_specified) [![Build Status](https://travis-ci.org/panorama-ed/order_as_specified.svg)](https://travis-ci.org/panorama-ed/order_as_specified) [![Inline docs](http://inch-ci.org/github/panorama-ed/order_as_specified.png)](http://inch-ci.org/github/panorama-ed/order_as_specified) [![Gem Version](https://badge.fury.io/rb/order_as_specified.svg)](http://badge.fury.io/rb/order_as_specified)
|
2
|
+
|
3
|
+
# OrderAsSpecified
|
4
|
+
|
5
|
+
`OrderAsSpecified` adds the ability to query an `ActiveRecord` class for results
|
6
|
+
from the database in an arbitrary order, without having to store anything extra
|
7
|
+
in the database.
|
8
|
+
|
9
|
+
It's as easy as:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
class TestObject
|
13
|
+
extend OrderAsSpecified
|
14
|
+
end
|
15
|
+
|
16
|
+
TestObject.order_as_specified(language: ["es", "en", "fr"])
|
17
|
+
=> #<ActiveRecord::Relation [
|
18
|
+
#<TestObject id: 3, language: "es">,
|
19
|
+
#<TestObject id: 1, language: "en">,
|
20
|
+
#<TestObject id: 4, language: "en">,
|
21
|
+
#<TestObject id: 2, language: "fr">
|
22
|
+
]>
|
23
|
+
```
|
24
|
+
|
25
|
+
### Is this like ranked-model?
|
26
|
+
|
27
|
+
Other gems like `ranked-model`, `acts_as_sortable`, etc. assume you want the
|
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.
|
31
|
+
|
32
|
+
## Installation
|
33
|
+
|
34
|
+
Add this line to your application's Gemfile:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
gem 'order_as_specified'
|
38
|
+
```
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
$ bundle
|
43
|
+
|
44
|
+
Or install it yourself as:
|
45
|
+
|
46
|
+
$ gem install order_as_specified
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
Basic usage is simple:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class TestObject
|
54
|
+
extend OrderAsSpecified
|
55
|
+
end
|
56
|
+
|
57
|
+
TestObject.order_as_specified(language: ["es", "en", "fr"])
|
58
|
+
=> #<ActiveRecord::Relation [
|
59
|
+
#<TestObject id: 3, language: "es">,
|
60
|
+
#<TestObject id: 1, language: "en">,
|
61
|
+
#<TestObject id: 4, language: "en">,
|
62
|
+
#<TestObject id: 2, language: "fr">
|
63
|
+
]>
|
64
|
+
```
|
65
|
+
|
66
|
+
This returns all `TestObject`s in the given language order. Note that this
|
67
|
+
ordering is not possible with a simple `ORDER BY`. Magic!
|
68
|
+
|
69
|
+
Like any other `ActiveRecord` relation, it can be chained:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
TestObject.
|
73
|
+
where(language: ["es", "en", "fr"]).
|
74
|
+
order_as_specified(language: ["es", "en", "fr"]).
|
75
|
+
limit(3)
|
76
|
+
=> #<ActiveRecord::Relation [
|
77
|
+
#<TestObject id: 3, language: "es">,
|
78
|
+
#<TestObject id: 1, language: "en">,
|
79
|
+
#<TestObject id: 4, language: "en">
|
80
|
+
]>
|
81
|
+
```
|
82
|
+
|
83
|
+
We can also use this when we want to sort by an attribute in another model:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
TestObject.
|
87
|
+
joins(:other_object).
|
88
|
+
order_as_specified(other_objects: { id: [other1.id, other3.id, other2.id] })
|
89
|
+
```
|
90
|
+
|
91
|
+
Neat, huh?
|
92
|
+
|
93
|
+
In all cases, results with attribute values not in the given list will be
|
94
|
+
sorted as though the attribute is `NULL` in a typical `ORDER BY`:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
TestObject.order_as_specified(language: ["fr", "es"])
|
98
|
+
=> #<ActiveRecord::Relation [
|
99
|
+
#<TestObject id: 2, language: "fr">,
|
100
|
+
#<TestObject id: 3, language: "es">,
|
101
|
+
#<TestObject id: 1, language: "en">,
|
102
|
+
#<TestObject id: 4, language: "en">
|
103
|
+
]>
|
104
|
+
```
|
105
|
+
|
106
|
+
## Documentation
|
107
|
+
|
108
|
+
We have documentation on [RubyDoc](http://www.rubydoc.info/github/panorama-ed/order_as_specified/master).
|
109
|
+
|
110
|
+
## Contributing
|
111
|
+
|
112
|
+
1. Fork it (https://github.com/panorama-ed/order_as_specified/fork)
|
113
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
114
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
115
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
116
|
+
5. Create a new Pull Request
|
117
|
+
|
118
|
+
**Make sure your changes have appropriate tests (`bundle exec rspec`)
|
119
|
+
and conform to the Rubocop style specified.** We use
|
120
|
+
[overcommit](https://github.com/causes/overcommit) to enforce good code.
|
121
|
+
|
122
|
+
## License
|
123
|
+
|
124
|
+
`OrderAsSpecified` is released under the
|
125
|
+
[MIT License](https://github.com/panorama-ed/order_as_specified/blob/master/LICENSE.txt).
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "order_as_specified/version"
|
2
|
+
|
3
|
+
# This module adds the ability to query an ActiveRecord class for results from
|
4
|
+
# the database in an arbitrary order, without having to store anything extra
|
5
|
+
# in the database. Simply `extend` it into your class and then you can use the
|
6
|
+
# `order_as_specified` class method.
|
7
|
+
|
8
|
+
module OrderAsSpecified
|
9
|
+
# @param hash [Hash] the ActiveRecord arguments hash
|
10
|
+
# @return [ActiveRecord::Relation] the objects, ordered as specified
|
11
|
+
def order_as_specified(hash)
|
12
|
+
params = extract_params(hash)
|
13
|
+
|
14
|
+
table = params[:table]
|
15
|
+
attribute = params[:attribute]
|
16
|
+
|
17
|
+
# We have to explicitly quote for now because SQL sanitization for ORDER BY
|
18
|
+
# queries hasn't yet merged into Rails.
|
19
|
+
# See: https://github.com/rails/rails/pull/13008
|
20
|
+
order_by = params[:values].map do |value|
|
21
|
+
"#{table}.#{attribute}='#{value}' DESC"
|
22
|
+
end.join(", ")
|
23
|
+
|
24
|
+
order(order_by)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# Recursively search through the hash to find the last elements, which
|
30
|
+
# indicate the name of the table we want to condition on, the attribute name,
|
31
|
+
# and the attribute values for ordering by.
|
32
|
+
# @param table [String/Symbol] the name of the table, default: the class table
|
33
|
+
# @param hash [Hash] the ActiveRecord-style arguments, such as:
|
34
|
+
# { other_objects: { id: [1, 5, 3] } }
|
35
|
+
def extract_params(table = table_name, hash)
|
36
|
+
raise "Could not parse params" unless hash.size == 1
|
37
|
+
|
38
|
+
key, val = hash.first
|
39
|
+
|
40
|
+
if val.is_a? Hash
|
41
|
+
extract_params(key, hash[key])
|
42
|
+
else
|
43
|
+
{
|
44
|
+
table: table,
|
45
|
+
attribute: key,
|
46
|
+
values: val
|
47
|
+
}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "order_as_specified/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "order_as_specified"
|
8
|
+
spec.version = OrderAsSpecified::VERSION
|
9
|
+
spec.authors = ["Jacob Evelyn"]
|
10
|
+
spec.email = ["jevelyn@panoramaed.com"]
|
11
|
+
spec.summary = "Add arbitrary ordering to ActiveRecord queries."
|
12
|
+
spec.description = "Obtain ActiveRecord results with a custom ordering "\
|
13
|
+
"with no need to store anything in the database."
|
14
|
+
spec.homepage = "https://github.com/panorama-ed/order_as_specified"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "activerecord", "~> 4.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
|
26
|
+
spec.add_development_dependency "overcommit", "~> 0.23"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
28
|
+
spec.add_development_dependency "rspec-rails", "~> 3.2"
|
29
|
+
spec.add_development_dependency "rubocop", "~> 0.29"
|
30
|
+
spec.add_development_dependency "temping", "~> 3.2"
|
31
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.describe OrderAsSpecified do
|
4
|
+
describe ".order_as_specified" do
|
5
|
+
Temping.create :test_class do
|
6
|
+
with_columns do |t|
|
7
|
+
t.string :field
|
8
|
+
end
|
9
|
+
|
10
|
+
extend OrderAsSpecified
|
11
|
+
|
12
|
+
has_one :association_test_class
|
13
|
+
end
|
14
|
+
|
15
|
+
Temping.create :association_test_class do
|
16
|
+
with_columns do |t|
|
17
|
+
t.integer :test_class_id
|
18
|
+
end
|
19
|
+
|
20
|
+
belongs_to :test_class
|
21
|
+
end
|
22
|
+
|
23
|
+
# Clean up after each test. This is a lot lighter for these few tests than
|
24
|
+
# trying to wrangle with RSpec-Rails to get transactional tests to work.
|
25
|
+
after :each do
|
26
|
+
TestClass.delete_all
|
27
|
+
AssociationTestClass.delete_all
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:shuffled_objects) do
|
31
|
+
5.times.map { |i| TestClass.create(field: "Field #{i}") }.shuffle
|
32
|
+
end
|
33
|
+
let(:shuffled_object_fields) { shuffled_objects.map(&:field) }
|
34
|
+
let(:shuffled_object_ids) { shuffled_objects.map(&:id) }
|
35
|
+
let(:omitted_object) { TestClass.create }
|
36
|
+
|
37
|
+
context "with no table name specified" do
|
38
|
+
subject { TestClass.order_as_specified(field: shuffled_object_fields) }
|
39
|
+
|
40
|
+
it "returns results including unspecified objects" do
|
41
|
+
omitted_object # Build an object that isn't sorted in this list.
|
42
|
+
expect(subject).to include omitted_object
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns results in the given order" do
|
46
|
+
omitted_object # Build an object that isn't sorted in this list.
|
47
|
+
expect(subject.map(&:id)).
|
48
|
+
to eq [*shuffled_object_ids, omitted_object.id]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "with another table name specified" do
|
53
|
+
let(:associated_objects) do
|
54
|
+
shuffled_objects.map do |object|
|
55
|
+
AssociationTestClass.create(test_class: object)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
let(:associated_object_ids) { associated_objects.map(&:id) }
|
59
|
+
let(:omitted_associated_object) do
|
60
|
+
AssociationTestClass.create(test_class: omitted_object)
|
61
|
+
end
|
62
|
+
|
63
|
+
subject do
|
64
|
+
TestClass.
|
65
|
+
joins(:association_test_class).
|
66
|
+
order_as_specified(
|
67
|
+
association_test_classes: { id: associated_object_ids }
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns results including unspecified objects" do
|
72
|
+
# Build an object that isn't sorted in this list.
|
73
|
+
omitted_associated_object
|
74
|
+
|
75
|
+
expect(subject).to include omitted_object
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns results in the given order" do
|
79
|
+
# Build an object that isn't sorted in this list.
|
80
|
+
omitted_associated_object
|
81
|
+
|
82
|
+
expect(subject.map(&:id)).
|
83
|
+
to eq [*shuffled_object_ids, omitted_object.id]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "codeclimate-test-reporter"
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
4
|
+
# Connect to an in-memory database for ActiveRecord tests.
|
5
|
+
require "temping"
|
6
|
+
ActiveRecord::Base.
|
7
|
+
establish_connection(adapter: "sqlite3", database: ":memory:")
|
8
|
+
|
9
|
+
require "order_as_specified"
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
# These two settings work together to allow you to limit a spec run
|
13
|
+
# to individual examples or groups you care about by tagging them with
|
14
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
15
|
+
# get run.
|
16
|
+
config.filter_run :focus
|
17
|
+
config.run_all_when_everything_filtered = true
|
18
|
+
|
19
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
20
|
+
# recommended.
|
21
|
+
# For more details, see:
|
22
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
23
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
24
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
25
|
+
config.disable_monkey_patching!
|
26
|
+
|
27
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
28
|
+
# file, and it's useful to allow more verbose output when running an
|
29
|
+
# individual spec file.
|
30
|
+
if config.files_to_run.one?
|
31
|
+
# Use the documentation formatter for detailed output,
|
32
|
+
# unless a formatter has already been configured
|
33
|
+
# (e.g. via a command-line flag).
|
34
|
+
config.default_formatter = "doc"
|
35
|
+
end
|
36
|
+
|
37
|
+
# Run specs in random order to surface order dependencies. If you find an
|
38
|
+
# order dependency and want to debug it, you can fix the order by providing
|
39
|
+
# the seed, which is printed after each run.
|
40
|
+
# --seed 1234
|
41
|
+
config.order = :random
|
42
|
+
|
43
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
44
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
45
|
+
# test failures related to randomization by passing the same `--seed` value
|
46
|
+
# as the one that triggered the failure.
|
47
|
+
Kernel.srand config.seed
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,171 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: order_as_specified
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacob Evelyn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: codeclimate-test-reporter
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: overcommit
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.23'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.23'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.2'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec-rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.2'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.29'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.29'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: temping
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.2'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ~>
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.2'
|
125
|
+
description: Obtain ActiveRecord results with a custom ordering with no need to store
|
126
|
+
anything in the database.
|
127
|
+
email:
|
128
|
+
- jevelyn@panoramaed.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- .overcommit.yml
|
135
|
+
- .rubocop.yml
|
136
|
+
- .travis.yml
|
137
|
+
- Gemfile
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- lib/order_as_specified.rb
|
141
|
+
- lib/order_as_specified/version.rb
|
142
|
+
- order_as_specified.gemspec
|
143
|
+
- spec/order_as_specified_spec.rb
|
144
|
+
- spec/spec_helper.rb
|
145
|
+
homepage: https://github.com/panorama-ed/order_as_specified
|
146
|
+
licenses:
|
147
|
+
- MIT
|
148
|
+
metadata: {}
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - '>='
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 2.2.2
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: Add arbitrary ordering to ActiveRecord queries.
|
169
|
+
test_files:
|
170
|
+
- spec/order_as_specified_spec.rb
|
171
|
+
- spec/spec_helper.rb
|