Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.

Commit 58d3e00

Browse files
authored
.order (#5)
* adding .order * fix rubocop
1 parent 223c170 commit 58d3e00

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,20 @@ records = Record.blue.available(true)
305305
GET https://service.example.com/records?color=blue&available=true
306306
```
307307

308+
#### order
309+
310+
Set the expected order of things using `.order`
311+
312+
```ruby
313+
# app/controllers/some_controller.rb
314+
315+
Record.where(color: 'blue').order(:name, { created_at: :desc })
316+
317+
```
318+
```
319+
GET https://service.example.com/records?color=blue&order[name]=asc&order[created_at]=desc
320+
```
321+
308322
#### all
309323

310324
You can fetch all remote records by using `all`. Pagination will be performed automatically (See: [Record pagination](#record-pagination))

lib/dhs/concerns/record/chainable.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@ def where(args = nil)
222222
end
223223
end
224224

225+
def order(*args)
226+
order_params = {}
227+
args.each do |arg|
228+
if arg.is_a?(Hash)
229+
arg.each do |key, value|
230+
order_params[key] = value
231+
end
232+
else
233+
order_params[arg.to_s] = 'asc'
234+
end
235+
end
236+
push(Parameter.new(order: order_params))
237+
end
238+
225239
def all(hash = nil)
226240
push([Parameter.new(hash), Option.new(all: true)])
227241
end

lib/dhs/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module DHS
4-
VERSION = '1.1.0'
4+
VERSION = '1.2.0'
55
end

spec/record/order_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
describe DHS::Record do
6+
context 'order in where chains' do
7+
before do
8+
class Record < DHS::Record
9+
endpoint 'http://records'
10+
end
11+
end
12+
13+
context 'single parameter for order' do
14+
before do
15+
stub_request(:get, 'http://records/?color=blue&order[created_at]=desc')
16+
.to_return(body: [{ name: 'ordered by created_at desc' }].to_json)
17+
end
18+
19+
it 'allows to add order params with .order' do
20+
records = Record.where(color: 'blue').order(created_at: :desc)
21+
expect(records.first.name).to eq 'ordered by created_at desc'
22+
end
23+
end
24+
25+
context 'multiple parameters for order' do
26+
before do
27+
stub_request(:get, 'http://records/?color=blue&order[name]=asc&order[created_at]=desc')
28+
.to_return(body: [{ name: 'ordered by name asc (implicitly) and created_at desc (explicitly)' }].to_json)
29+
end
30+
31+
it 'allows to add order params with .order' do
32+
records = Record.where(color: 'blue').order(:name, created_at: :desc)
33+
expect(records.first.name).to eq 'ordered by name asc (implicitly) and created_at desc (explicitly)'
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)