sinja-sequel 0.1.1 → 0.1.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 +73 -0
- data/lib/sinatra/jsonapi/sequel.rb +1 -1
- data/lib/sinja/sequel/core.rb +3 -39
- data/lib/sinja/sequel/pagination.rb +42 -0
- data/lib/sinja/sequel/version.rb +1 -1
- data/lib/sinja/sequel.rb +7 -3
- data/sinja-sequel.gemspec +1 -1
- metadata +7 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b6983191193091edf129496e971949a02941748
|
4
|
+
data.tar.gz: 1f88be88d7d6366362543c054c2607cf4e8eb81d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64489f2281df7b119dbe0d2fc8d727dec44ff785bc9c45faa2d38b1176a165ddf36b84523ee4de18be14209376b2188209d828183660ba73e71c549dfbc64a2c
|
7
|
+
data.tar.gz: 9965fd332e77a4823a822e70e9377a51af27c872bafb70aa3773c261edf60e417e2d5b78cad295cde981f93d3963fe0e8eccd47fb05c7f66386111d914332931
|
data/README.md
CHANGED
@@ -33,6 +33,7 @@ out. Testers and community contributions welcome!
|
|
33
33
|
- [`remove_present`](#remove_present)
|
34
34
|
- [`add_remove`](#add_remove)
|
35
35
|
- [Extension](#extension)
|
36
|
+
- [Pagination](#pagination)
|
36
37
|
- [Development](#development)
|
37
38
|
- [Contributing](#contributing)
|
38
39
|
- [License](#license)
|
@@ -112,6 +113,10 @@ class MyApp < Sinatra::Base
|
|
112
113
|
helpers do
|
113
114
|
prepend Sinja::Sequel::Core
|
114
115
|
end
|
116
|
+
|
117
|
+
# ..
|
118
|
+
|
119
|
+
freeze_jsonapi
|
115
120
|
end
|
116
121
|
```
|
117
122
|
|
@@ -162,6 +167,10 @@ class MyApp < Sinatra::Base
|
|
162
167
|
register Sinja
|
163
168
|
|
164
169
|
helpers Sinja::Sequel::Helpers
|
170
|
+
|
171
|
+
# ..
|
172
|
+
|
173
|
+
freeze_jsonapi
|
165
174
|
end
|
166
175
|
```
|
167
176
|
|
@@ -234,6 +243,10 @@ require 'sinja/sequel'
|
|
234
243
|
class MyApp < Sinatra::Base
|
235
244
|
register Sinja
|
236
245
|
register Sinja::Sequel
|
246
|
+
|
247
|
+
# ..
|
248
|
+
|
249
|
+
freeze_jsonapi
|
237
250
|
end
|
238
251
|
```
|
239
252
|
|
@@ -255,6 +268,66 @@ These action helpers can be subsequently overridden, customized by setting
|
|
255
268
|
action helper options (i.e. `:roles`) and/or defining `before_<action>` hooks,
|
256
269
|
or removed entirely with `remove_<action>`.
|
257
270
|
|
271
|
+
Given a database connection and Foo, Bar, and Qux models and serializers,
|
272
|
+
here's an example "classic"-style application using the extension:
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
require 'sinatra/jsonapi/sequel'
|
276
|
+
|
277
|
+
resource :foos do
|
278
|
+
has_many :bars
|
279
|
+
has_one :qux
|
280
|
+
end
|
281
|
+
|
282
|
+
resource :bars do
|
283
|
+
has_one :foo
|
284
|
+
end
|
285
|
+
|
286
|
+
resource :quxes do
|
287
|
+
has_many :foos
|
288
|
+
end
|
289
|
+
|
290
|
+
freeze_jsonapi
|
291
|
+
```
|
292
|
+
|
293
|
+
Pretty neat, huh?
|
294
|
+
|
295
|
+
### Pagination
|
296
|
+
|
297
|
+
Sinja::Sequel uses the first Sequel database to determine whether or not to
|
298
|
+
enable pagination. If (and only if!) you have _multiple_ databases in your
|
299
|
+
application and only _some_ support pagination, you _may_ need to prepend
|
300
|
+
[Sinja::Sequel::Pagination](/lib/sinja/sequel/pagination.rb) after prepending
|
301
|
+
Core, including Helpers, or registering Sinja:
|
302
|
+
|
303
|
+
```ruby
|
304
|
+
require 'sinja'
|
305
|
+
require 'sinja/sequel'
|
306
|
+
require 'sinja/sequel/pagination'
|
307
|
+
|
308
|
+
DB = Sequel.connect ENV['DB_URL']
|
309
|
+
|
310
|
+
OTHER_DB = Sequel.connect ENV['OTHER_DB_URL']
|
311
|
+
OTHER_DB.extension :pagination
|
312
|
+
|
313
|
+
class MyApp < Sinatra::Base
|
314
|
+
register Sinja
|
315
|
+
register Sinja::Sequel
|
316
|
+
|
317
|
+
helpers do
|
318
|
+
prepend Sinja::Sequel::Pagination
|
319
|
+
|
320
|
+
def database
|
321
|
+
OTHER_DB
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
# ..
|
326
|
+
|
327
|
+
freeze_jsonapi
|
328
|
+
end
|
329
|
+
```
|
330
|
+
|
258
331
|
## Development
|
259
332
|
|
260
333
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
data/lib/sinja/sequel/core.rb
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
require 'forwardable'
|
3
3
|
require 'sequel'
|
4
4
|
|
5
|
+
require_relative 'pagination'
|
6
|
+
|
5
7
|
module Sinja
|
6
8
|
module Sequel
|
7
9
|
module Core
|
@@ -15,7 +17,7 @@ module Sinja
|
|
15
17
|
c.validation_formatter = ->(e) { e.errors.keys.zip(e.errors.full_messages) }
|
16
18
|
end
|
17
19
|
|
18
|
-
base.prepend Pagination if ::Sequel::
|
20
|
+
base.prepend Pagination if ::Sequel::Model.db.dataset.respond_to?(:paginate)
|
19
21
|
end
|
20
22
|
|
21
23
|
def self.included(_)
|
@@ -38,43 +40,5 @@ module Sinja
|
|
38
40
|
raise ::Sequel::ValidationFailed, resource unless resource.valid?
|
39
41
|
end
|
40
42
|
end
|
41
|
-
|
42
|
-
module Pagination
|
43
|
-
def self.prepended(base)
|
44
|
-
base.sinja { |c| c.page_using = {
|
45
|
-
:number=>1,
|
46
|
-
:size=>10,
|
47
|
-
:record_count=>nil
|
48
|
-
}}
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.included(_)
|
52
|
-
abort "You must `prepend' Sinja::Sequel::Pagination, not `include' it!"
|
53
|
-
end
|
54
|
-
|
55
|
-
def page(collection, opts)
|
56
|
-
collection = collection.dataset unless collection.respond_to?(:paginate)
|
57
|
-
|
58
|
-
opts = settings._sinja.page_using.merge(opts)
|
59
|
-
collection = collection.paginate opts[:number].to_i, opts[:size].to_i,
|
60
|
-
(opts[:record_count].to_i if opts[:record_count])
|
61
|
-
|
62
|
-
# Attributes common to all pagination links
|
63
|
-
base = {
|
64
|
-
:size=>collection.page_size,
|
65
|
-
:record_count=>collection.pagination_record_count
|
66
|
-
}
|
67
|
-
|
68
|
-
pagination = {
|
69
|
-
:first=>base.merge(:number=>1),
|
70
|
-
:self=>base.merge(:number=>collection.current_page),
|
71
|
-
:last=>base.merge(:number=>collection.page_count)
|
72
|
-
}
|
73
|
-
pagination[:next] = base.merge(:number=>collection.next_page) if collection.next_page
|
74
|
-
pagination[:prev] = base.merge(:number=>collection.prev_page) if collection.prev_page
|
75
|
-
|
76
|
-
return collection, pagination
|
77
|
-
end
|
78
|
-
end
|
79
43
|
end
|
80
44
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Sinja
|
3
|
+
module Sequel
|
4
|
+
module Pagination
|
5
|
+
def self.prepended(base)
|
6
|
+
base.sinja { |c| c.page_using = {
|
7
|
+
:number=>1,
|
8
|
+
:size=>10,
|
9
|
+
:record_count=>nil
|
10
|
+
}}
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.included(_)
|
14
|
+
abort "You must `prepend' Sinja::Sequel::Pagination, not `include' it!"
|
15
|
+
end
|
16
|
+
|
17
|
+
def page(collection, opts)
|
18
|
+
collection = collection.dataset unless collection.respond_to?(:paginate)
|
19
|
+
|
20
|
+
opts = settings._sinja.page_using.merge(opts)
|
21
|
+
collection = collection.paginate opts[:number].to_i, opts[:size].to_i,
|
22
|
+
(opts[:record_count].to_i if opts[:record_count])
|
23
|
+
|
24
|
+
# Attributes common to all pagination links
|
25
|
+
base = {
|
26
|
+
:size=>collection.page_size,
|
27
|
+
:record_count=>collection.pagination_record_count
|
28
|
+
}
|
29
|
+
|
30
|
+
pagination = {
|
31
|
+
:first=>base.merge(:number=>1),
|
32
|
+
:self=>base.merge(:number=>collection.current_page),
|
33
|
+
:last=>base.merge(:number=>collection.page_count)
|
34
|
+
}
|
35
|
+
pagination[:next] = base.merge(:number=>collection.next_page) if collection.next_page
|
36
|
+
pagination[:prev] = base.merge(:number=>collection.prev_page) if collection.prev_page
|
37
|
+
|
38
|
+
return collection, pagination
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/sinja/sequel/version.rb
CHANGED
data/lib/sinja/sequel.rb
CHANGED
@@ -15,19 +15,23 @@ module Sinja
|
|
15
15
|
register Resource
|
16
16
|
|
17
17
|
helpers do
|
18
|
+
def dataset
|
19
|
+
klass.dataset
|
20
|
+
end
|
21
|
+
|
18
22
|
def find(id)
|
19
|
-
|
23
|
+
dataset[id.send(try_convert)]
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
23
27
|
show
|
24
28
|
|
25
29
|
show_many do |ids|
|
26
|
-
|
30
|
+
dataset.where(klass.primary_key=>ids.map!(&try_convert)).all
|
27
31
|
end
|
28
32
|
|
29
33
|
index do
|
30
|
-
|
34
|
+
dataset
|
31
35
|
end
|
32
36
|
|
33
37
|
create do |attr|
|
data/sinja-sequel.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.required_ruby_version = '>= 2.3.0'
|
22
22
|
|
23
23
|
spec.add_dependency 'sequel', '~> 4.0'
|
24
|
-
spec.add_dependency 'sinja', '
|
24
|
+
spec.add_dependency 'sinja', '~> 1.2'
|
25
25
|
|
26
26
|
spec.add_development_dependency 'bundler', '~> 1.11'
|
27
27
|
spec.add_development_dependency 'minitest', '~> 5.9'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinja-sequel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Pastore
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -28,22 +28,16 @@ dependencies:
|
|
28
28
|
name: sinja
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 1.2.0.pre2
|
34
|
-
- - "<"
|
31
|
+
- - "~>"
|
35
32
|
- !ruby/object:Gem::Version
|
36
|
-
version: '2'
|
33
|
+
version: '1.2'
|
37
34
|
type: :runtime
|
38
35
|
prerelease: false
|
39
36
|
version_requirements: !ruby/object:Gem::Requirement
|
40
37
|
requirements:
|
41
|
-
- - "
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 1.2.0.pre2
|
44
|
-
- - "<"
|
38
|
+
- - "~>"
|
45
39
|
- !ruby/object:Gem::Version
|
46
|
-
version: '2'
|
40
|
+
version: '1.2'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
42
|
name: bundler
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -105,6 +99,7 @@ files:
|
|
105
99
|
- lib/sinja/sequel.rb
|
106
100
|
- lib/sinja/sequel/core.rb
|
107
101
|
- lib/sinja/sequel/helpers.rb
|
102
|
+
- lib/sinja/sequel/pagination.rb
|
108
103
|
- lib/sinja/sequel/version.rb
|
109
104
|
- sinja-sequel.gemspec
|
110
105
|
homepage: https://github.com/mwpastore/sinja-sequel
|