lhs 5.4.1 → 5.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -14
- data/lib/lhs/pagination.rb +3 -3
- data/lib/lhs/version.rb +1 -1
- data/spec/record/pagination_chain_spec.rb +7 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8355a11dabd2c8cd1e889120f20e7c6eaa133d25
|
4
|
+
data.tar.gz: 9c576f20dbda1dec490ad144760f30c61ff51050
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36258e2eff638180df2066b889bc4c861c9dca3f89839d96c9d82d366a369de6b4be64cffd8e0ba2e53de5a499344d93e28437070001bceeebf0efe8063d5d44
|
7
|
+
data.tar.gz: 54a33e04d23850e597ab94e9b21592725fa0748b10577852a57c5f22fc1c2c3e530d2094977fde57f5f672de7db50fd309efdfa249e8c4a166e150d182375466
|
data/README.md
CHANGED
@@ -83,6 +83,7 @@ records.where(available: true).each do |record|
|
|
83
83
|
...
|
84
84
|
end
|
85
85
|
```
|
86
|
+
|
86
87
|
The example would fetch records with the following parameters: `{color: blue, available: true}`.
|
87
88
|
|
88
89
|
## Where values hash
|
@@ -371,7 +372,7 @@ end
|
|
371
372
|
|
372
373
|
Nested records (in nested data) are automaticaly casted when the href matches any defined endpoint of any LHS::Record.
|
373
374
|
|
374
|
-
```
|
375
|
+
```ruby
|
375
376
|
class Place < LHS::Record
|
376
377
|
endpoint ':datastore/v2/places'
|
377
378
|
|
@@ -394,7 +395,7 @@ If automatic-detection of nested records does not work, make sure your LHS::Reco
|
|
394
395
|
|
395
396
|
You can change attributes of LHS::Records:
|
396
397
|
|
397
|
-
```
|
398
|
+
```ruby
|
398
399
|
record = Feedback.find(id: 'z12f-3asm3ngals')
|
399
400
|
rcord.recommended = false
|
400
401
|
```
|
@@ -435,7 +436,7 @@ In order to validate LHS::Records before persisting them, you can use the `valid
|
|
435
436
|
|
436
437
|
The specific endpoint has to support validations with the `persist=false` parameter. The endpoint has to be enabled (opt-in) for validations in the service configuration.
|
437
438
|
|
438
|
-
```
|
439
|
+
```ruby
|
439
440
|
class User < LHS::Record
|
440
441
|
endpoint ':datastore/v2/users', validates: true
|
441
442
|
end
|
@@ -523,30 +524,30 @@ You can use chainable pagination in combination with query chains:
|
|
523
524
|
|
524
525
|
```ruby
|
525
526
|
class Record < LHS::Record
|
526
|
-
endpoint '
|
527
|
+
endpoint ':datastore/records'
|
527
528
|
end
|
528
529
|
Record.page(3).per(20).where(color: 'blue')
|
529
|
-
#
|
530
|
+
# /records?offset=40&limit=20&color=blue
|
530
531
|
```
|
531
532
|
|
532
533
|
The applied pagination strategy depends on the actual configured pagination, so the interface is the same for all strategies:
|
533
534
|
|
534
535
|
```ruby
|
535
536
|
class Record < LHS::Record
|
536
|
-
endpoint '
|
537
|
+
endpoint ':datastore/records'
|
537
538
|
configuration pagination_strategy: 'page'
|
538
539
|
end
|
539
540
|
Record.page(3).per(20).where(color: 'blue')
|
540
|
-
#
|
541
|
+
# /records?page=3&limit=20&color=blue
|
541
542
|
```
|
542
543
|
|
543
544
|
```ruby
|
544
545
|
class Record < LHS::Record
|
545
|
-
endpoint '
|
546
|
+
endpoint ':datastore/records'
|
546
547
|
configuration pagination_strategy: 'start'
|
547
548
|
end
|
548
549
|
Record.page(3).per(20).where(color: 'blue')
|
549
|
-
#
|
550
|
+
# /records?start=41&limit=20&color=blue
|
550
551
|
```
|
551
552
|
|
552
553
|
`limit(argument)` is an alias for `per(argument)`. Take notice that `limit` without argument instead, makes the query resolve and provides the current limit from the responds.
|
@@ -559,10 +560,7 @@ The kaminari’s page parameter is in params[:page]. For example, you can use ka
|
|
559
560
|
|
560
561
|
```ruby
|
561
562
|
# controller
|
562
|
-
|
563
|
-
page = params[:page].to_i
|
564
|
-
limit = 100
|
565
|
-
@items = Record.page(page).per(limit)
|
563
|
+
@items = Record.page(params[:page]).per(100)
|
566
564
|
```
|
567
565
|
|
568
566
|
```ruby
|
@@ -572,7 +570,7 @@ limit = 100
|
|
572
570
|
|
573
571
|
## form_for Helper
|
574
572
|
Rails `form_for` view-helper can be used in combination with instances of LHS::Record to autogenerate forms:
|
575
|
-
```
|
573
|
+
```ruby
|
576
574
|
<%= form_for(@instance, url: '/create') do |f| %>
|
577
575
|
<%= f.text_field :name %>
|
578
576
|
<%= f.text_area :text %>
|
data/lib/lhs/pagination.rb
CHANGED
@@ -66,7 +66,7 @@ class LHS::Pagination
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def self.page_to_offset(page, _limit)
|
69
|
-
page
|
69
|
+
page.to_i
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
@@ -92,7 +92,7 @@ class LHS::StartPagination < LHS::Pagination
|
|
92
92
|
end
|
93
93
|
|
94
94
|
def self.page_to_offset(page, limit = LHS::Pagination::DEFAULT_LIMIT)
|
95
|
-
(page - 1) * limit + 1
|
95
|
+
(page.to_i - 1) * limit.to_i + 1
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
@@ -107,6 +107,6 @@ class LHS::OffsetPagination < LHS::Pagination
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def self.page_to_offset(page, limit = LHS::Pagination::DEFAULT_LIMIT)
|
110
|
-
(page - 1) * limit
|
110
|
+
(page.to_i - 1) * limit.to_i
|
111
111
|
end
|
112
112
|
end
|
data/lib/lhs/version.rb
CHANGED
@@ -45,6 +45,13 @@ describe LHS::Record do
|
|
45
45
|
Record.page("").limit(10).first
|
46
46
|
expect(request).to have_been_made.times(2)
|
47
47
|
end
|
48
|
+
|
49
|
+
it 'also works with strings' do
|
50
|
+
request = stub_request(:get, "http://local.ch/records?limit=10&offset=0").to_return(body: [].to_json)
|
51
|
+
Record.limit('10').first
|
52
|
+
Record.page('1').limit('10').first
|
53
|
+
expect(request).to have_been_made.times(2)
|
54
|
+
end
|
48
55
|
end
|
49
56
|
|
50
57
|
context 'start pagination' do
|