sinja-sequel 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/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +274 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sinatra/jsonapi/sequel.rb +7 -0
- data/lib/sinja-sequel.rb +2 -0
- data/lib/sinja/sequel.rb +110 -0
- data/lib/sinja/sequel/core.rb +72 -0
- data/lib/sinja/sequel/helpers.rb +78 -0
- data/lib/sinja/sequel/version.rb +6 -0
- data/sinja-sequel.gemspec +29 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1f7b795742fcfdbe13930f92bd6946f3de0d89ed
|
4
|
+
data.tar.gz: c12983f2a42794564b744ecfef193c84f6ef064e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d619607435ab470cef5837a7603d0b60845ef346133962d1332f9a29d1eb48961a959449c865f94b7fb179909d10a2a0935c0401490801ee7ffdf6d2cb7b4f24
|
7
|
+
data.tar.gz: 129a26fe0d65b9a69c886c010202229bb6a11603aee4348e119c2ef71372dbdf7c69bae15ee10fc3c2066f9049e75750374a384ef4bf7ab563eb0841de36213d
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Mike Pastore
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,274 @@
|
|
1
|
+
# Sinja::Sequel
|
2
|
+
|
3
|
+
<!--
|
4
|
+
Title: Sinja::Sequel
|
5
|
+
Description: Sequel-specific Helpers and DSL for Sinja
|
6
|
+
Author: Mike Pastore
|
7
|
+
Keywords: Ruby, Sinatra, Sinatra::JSONAPI, Sinja, Sequel
|
8
|
+
-->
|
9
|
+
|
10
|
+
[![Gem Version](https://badge.fury.io/rb/sinja-sequel.svg)](https://badge.fury.io/rb/sinja-sequel)
|
11
|
+
|
12
|
+
Sinja::Sequel configures your [Sinja][1] application to work with [Sequel][2]
|
13
|
+
out of the box, and provides additional helpers to greatly simplify the process
|
14
|
+
of writing the more complex action helpers (specifically `replace`, `merge`,
|
15
|
+
and `subtract`). An optional extension enhances Sinja's DSL to generate basic
|
16
|
+
action helpers that can be overridden, customized, or removed.
|
17
|
+
|
18
|
+
The core configuration and helpers are in pretty good shape (Sinja uses them in
|
19
|
+
its [demo app][3] and test suite), but the extension could use some fleshing
|
20
|
+
out. Testers and community contributions welcome!
|
21
|
+
|
22
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
23
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
24
|
+
|
25
|
+
|
26
|
+
- [Installation](#installation)
|
27
|
+
- [Usage](#usage)
|
28
|
+
- [Core](#core)
|
29
|
+
- [Helpers](#helpers)
|
30
|
+
- [`next_pk`](#next_pk)
|
31
|
+
- [`add_missing`](#add_missing)
|
32
|
+
- [`remove_present`](#remove_present)
|
33
|
+
- [`add_remove`](#add_remove)
|
34
|
+
- [Extension](#extension)
|
35
|
+
- [Development](#development)
|
36
|
+
- [Contributing](#contributing)
|
37
|
+
- [License](#license)
|
38
|
+
|
39
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
40
|
+
|
41
|
+
## Installation
|
42
|
+
|
43
|
+
Add this line to your application's Gemfile:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
gem 'sinja-sequel'
|
47
|
+
```
|
48
|
+
|
49
|
+
And then execute:
|
50
|
+
|
51
|
+
```sh
|
52
|
+
$ bundle
|
53
|
+
```
|
54
|
+
|
55
|
+
Or install it yourself as:
|
56
|
+
|
57
|
+
```sh
|
58
|
+
$ gem install sinja-sequel
|
59
|
+
```
|
60
|
+
|
61
|
+
## Usage
|
62
|
+
|
63
|
+
Always return Sequel datasets (instead of arrays of objects) from your `index`
|
64
|
+
(e.g. `Foo.dataset`) and `fetch` (e.g. `resource.bars_dataset`) action
|
65
|
+
helpers. The `finalize` helper, described below, will ensure they are
|
66
|
+
"rasterized" before being passed to [JSONAPI::Serializers][5].
|
67
|
+
|
68
|
+
You'll want to enable Sequel's `:tactical_eager_loading` plugin for the best
|
69
|
+
performance with JSONAPI::Serializers. I've [seen][6] it reduce complex
|
70
|
+
serializations by a factor of 100 (i.e. quite literally one query instead of
|
71
|
+
100).
|
72
|
+
|
73
|
+
If you want to use client-generated IDs, enable the `:update_primary_key`
|
74
|
+
plugin on the model and call `unrestrict_primary_key` in the model definition
|
75
|
+
to allow mass assignment (e.g. with `Sequel::Model#set_fields`).
|
76
|
+
|
77
|
+
If your model has foreign keys and you want to enforce a non-nullable
|
78
|
+
constraint at the application level, consider enabling the
|
79
|
+
`:validation_helpers` plugin on the model and using `validates_not_null` in
|
80
|
+
conjuction with the `validate!` helper described below:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class Bar < Sequel::Model
|
84
|
+
plugin :validation_helpers
|
85
|
+
|
86
|
+
def validate
|
87
|
+
super
|
88
|
+
validates_not_null :foo
|
89
|
+
end
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
See "Avoding Null Foreign Keys" in the [Sinja][1] documentation for more
|
94
|
+
information.
|
95
|
+
|
96
|
+
Finally, enable the `:pagination` extension on your connection (before
|
97
|
+
prepending Core) to enable pagination!
|
98
|
+
|
99
|
+
### Core
|
100
|
+
|
101
|
+
Prepend [Sinja::Sequel::Core](/extensions/sequel/lib/sinja/sequel/core.rb)
|
102
|
+
after registering Sinja:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
require 'sinja'
|
106
|
+
require 'sinja/sequel/core'
|
107
|
+
|
108
|
+
class MyApp < Sinatra::Base
|
109
|
+
register Sinja
|
110
|
+
|
111
|
+
helpers do
|
112
|
+
prepend Sinja::Sequel::Core
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
Note that you must use `prepend` (instead of including Sinja::Sequel::Core like
|
118
|
+
a normal module of Sinatra helpers) in order to ensure that the included
|
119
|
+
methods take precedence over Sinja's method stubs (e.g. `transaction`).
|
120
|
+
[This][4] will hopefully be fixed in a future version of Sinatra.
|
121
|
+
|
122
|
+
Prepending Core does the following to your application:
|
123
|
+
|
124
|
+
* Configures `conflict_`, `not_found_`, and `validation_exceptions`, and
|
125
|
+
`validation_formatter`.
|
126
|
+
* Defines a `database` helper that delegates to `Sequel::Model.db`.
|
127
|
+
* Defines a `transaction` helper that delegates to `database.transaction`.
|
128
|
+
* Defines a `validate!` helper that raises an error if `resource` is invalid
|
129
|
+
after a `create` or `update` action helper invocation.
|
130
|
+
* Defines a simple equality-based `filter` helper that passes the filter params
|
131
|
+
to `Sequel::Dataset#where`.
|
132
|
+
* Defines a `sort` helper that applies `Sequel.asc` and `Sequel.desc` to the
|
133
|
+
sort terms and passes them to `Sequel::Dataset#order`.
|
134
|
+
* Defines a `finalize` helper that simply calls `Sequel::Dataset#all`.
|
135
|
+
|
136
|
+
If the `:pagination` Sequel extension is loaded, it also does the following:
|
137
|
+
|
138
|
+
* Configures `page_using` for page number- and size-based pagination, with an
|
139
|
+
additional record count parameter to avoid repetitive `SELECT COUNT` queries
|
140
|
+
while paging.
|
141
|
+
* Defines a `page` helper that calls `Sequel::Dataset#paginate` and computes a
|
142
|
+
hash of page params that Sinja will use to construct the root pagination
|
143
|
+
links and add to the root metadata of the response.
|
144
|
+
|
145
|
+
You may override any of the installed helpers by defining your own. Please see
|
146
|
+
the [Sinja][1] documentation for more information about Sinja hooks and
|
147
|
+
configurables, and the [Sequel][2] documentation for more information about
|
148
|
+
Sequel plugins and features.
|
149
|
+
|
150
|
+
### Helpers
|
151
|
+
|
152
|
+
Include
|
153
|
+
[Sinja::Sequel::Helpers](/extensions/sequel/lib/sinja/sequel/helpers.rb) after
|
154
|
+
registering Sinja:
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
require 'sinja'
|
158
|
+
require 'sinja/sequel/helpers'
|
159
|
+
|
160
|
+
class MyApp < Sinatra::Base
|
161
|
+
register Sinja
|
162
|
+
|
163
|
+
helpers Sinja::Sequel::Helpers
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
This is the most common use-case. **Note that including Helpers will
|
168
|
+
automatically prepend Core!**
|
169
|
+
|
170
|
+
#### `next_pk`
|
171
|
+
|
172
|
+
A convenience method to always return the primary key of the resource and the
|
173
|
+
resource from your `create` action helpers. Simply use it instead of `next`!
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
create do |attr|
|
177
|
+
next_pk Foo.create(attr)
|
178
|
+
end
|
179
|
+
```
|
180
|
+
|
181
|
+
#### `add_missing`
|
182
|
+
|
183
|
+
Take the key of a Sequel \*_to_many association and an array of resource
|
184
|
+
identifier objects and add the "missing" records to the collection. Makes
|
185
|
+
writing your `merge` action helpers a breeze!
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
has_many :bars do
|
189
|
+
merge do |rios|
|
190
|
+
add_missing(:bars, rios)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
```
|
194
|
+
|
195
|
+
It will try to cast the ID of each resource identifier object by sending it the
|
196
|
+
`:to_i` method; pass in a third argument to specify a different method (e.g. if
|
197
|
+
the primary key of the `bars` table is a `varchar`, pass in `:to_s` instead).
|
198
|
+
|
199
|
+
This helper also takes an optional block that can be used to filter
|
200
|
+
subresources during processing. Simply return a truthy or falsey value from the
|
201
|
+
block (or raise an error to abort the entire transaction):
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
has_many :bars do
|
205
|
+
merge do |rios|
|
206
|
+
add_missing(:bars, rios) do |bar|
|
207
|
+
role?(:admin) || bar.owner == resource.owner
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
```
|
212
|
+
|
213
|
+
#### `remove_present`
|
214
|
+
|
215
|
+
Like `add_missing`, but removes the "present" records from the collection.
|
216
|
+
Makes writing your `subtract` action helpers a breeze!
|
217
|
+
|
218
|
+
#### `add_remove`
|
219
|
+
|
220
|
+
Like `add_missing` and `remove_present`, but performs an efficient delta
|
221
|
+
operation on the collection. Makes writing your `replace` action helpers a
|
222
|
+
breeze!
|
223
|
+
|
224
|
+
### Extension
|
225
|
+
|
226
|
+
Register [Sinja::Sequel](/extensions/sequel/lib/sinja/sequel.rb) after
|
227
|
+
registering Sinja:
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
require 'sinja'
|
231
|
+
require 'sinja/sequel'
|
232
|
+
|
233
|
+
class MyApp < Sinatra::Base
|
234
|
+
register Sinja
|
235
|
+
register Sinja::Sequel
|
236
|
+
end
|
237
|
+
```
|
238
|
+
|
239
|
+
**Note that registering the extension will automatically include Helpers!**
|
240
|
+
|
241
|
+
After registering the extension, the `resource`, `has_many`, and `has_one` DSL
|
242
|
+
keywords will generate basic action helpers. The default `create` action helper
|
243
|
+
does not support client-generated IDs. These action helpers can be subsequently
|
244
|
+
overridden, customized by setting action helper options (i.e. `:roles`) and/or
|
245
|
+
defining `before_<action>` hooks, or removed entirely with `remove_<action>`.
|
246
|
+
|
247
|
+
## Development
|
248
|
+
|
249
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
250
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
251
|
+
prompt that will allow you to experiment.
|
252
|
+
|
253
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
254
|
+
release a new version, update the version number in `version.rb`, and then run
|
255
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
256
|
+
git commits and tags, and push the `.gem` file to
|
257
|
+
[rubygems.org](https://rubygems.org).
|
258
|
+
|
259
|
+
## Contributing
|
260
|
+
|
261
|
+
Bug reports and pull requests are welcome on GitHub at
|
262
|
+
https://github.com/mwpastore/sinja.
|
263
|
+
|
264
|
+
## License
|
265
|
+
|
266
|
+
The gem is available as open source under the terms of the [MIT
|
267
|
+
License](http://opensource.org/licenses/MIT).
|
268
|
+
|
269
|
+
[1]: https://github.com/mwpastore/sinja
|
270
|
+
[2]: http://sequel.jeremyevans.net
|
271
|
+
[3]: https://github.com/mwpastore/sinja/tree/master/demo-app
|
272
|
+
[4]: https://github.com/sinatra/sinatra/issues/1213
|
273
|
+
[5]: https://github.com/fotinakis/jsonapi-serializers
|
274
|
+
[6]: https://github.com/fotinakis/jsonapi-serializers/pull/31#issuecomment-148193366
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'sinja-sequel'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require 'pry'
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/sinja-sequel.rb
ADDED
data/lib/sinja/sequel.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'sinja/sequel/helpers'
|
3
|
+
require 'sinja/sequel/version'
|
4
|
+
|
5
|
+
module Sinja
|
6
|
+
module Sequel
|
7
|
+
def self.registered(app)
|
8
|
+
app.helpers Helpers
|
9
|
+
end
|
10
|
+
|
11
|
+
def resource(res, try_convert=:to_i, &block)
|
12
|
+
klass = res.to_s.classify.constantize
|
13
|
+
|
14
|
+
super(res) do
|
15
|
+
register Resource
|
16
|
+
|
17
|
+
helpers do
|
18
|
+
def find(id)
|
19
|
+
klass[id.send(try_convert)]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
show
|
24
|
+
|
25
|
+
show_many do |ids|
|
26
|
+
klass.where(klass.primary_key=>ids.map(&try_convert)).all
|
27
|
+
end
|
28
|
+
|
29
|
+
index do
|
30
|
+
klass.dataset
|
31
|
+
end
|
32
|
+
|
33
|
+
create do |attr|
|
34
|
+
tmp = klass.new
|
35
|
+
if respond_to?(:settable_fields)
|
36
|
+
tmp.set_fields(attr, settable_fields)
|
37
|
+
else
|
38
|
+
tmp.set(attr)
|
39
|
+
end
|
40
|
+
tmp.save(:validate=>false)
|
41
|
+
next_pk tmp
|
42
|
+
end
|
43
|
+
|
44
|
+
update do |attr|
|
45
|
+
if respond_to?(:settable_fields)
|
46
|
+
resource.update_fields(attr, settable_fields, :validate=>false, :missing=>:skip)
|
47
|
+
else
|
48
|
+
resource.set(attr)
|
49
|
+
resource.save_changes(:validate=>false)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
destroy do
|
54
|
+
resource.destroy
|
55
|
+
end
|
56
|
+
|
57
|
+
instance_eval(&block) if block
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
module Resource
|
62
|
+
def has_one(rel, &block)
|
63
|
+
super(rel) do
|
64
|
+
pluck do
|
65
|
+
resource.send(rel)
|
66
|
+
end
|
67
|
+
|
68
|
+
prune(:sideload_on=>:update) do
|
69
|
+
resource.send("#{rel}=", nil)
|
70
|
+
resource.save_changes
|
71
|
+
end
|
72
|
+
|
73
|
+
graft(:sideload_on=>%i[create update]) do |rio|
|
74
|
+
klass = resource.class.association_reflection(rel).associated_class
|
75
|
+
resource.send("#{rel}=", klass.with_pk!(rio[:id]))
|
76
|
+
resource.save_changes(:validate=>!sideloaded?)
|
77
|
+
end
|
78
|
+
|
79
|
+
instance_eval(&block) if block
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def has_many(rel, &block)
|
84
|
+
super(rel) do
|
85
|
+
fetch do
|
86
|
+
resource.send("#{rel}_dataset")
|
87
|
+
end
|
88
|
+
|
89
|
+
clear(:sideload_on=>:update) do
|
90
|
+
resource.send("remove_all_#{rel}")
|
91
|
+
end
|
92
|
+
|
93
|
+
replace(:sideload_on=>:update) do |rios|
|
94
|
+
add_remove(rel, rios)
|
95
|
+
end
|
96
|
+
|
97
|
+
merge(:sideload_on=>:create) do |rios|
|
98
|
+
add_missing(rel, rios)
|
99
|
+
end
|
100
|
+
|
101
|
+
subtract do |rios|
|
102
|
+
remove_present(rel, rios)
|
103
|
+
end
|
104
|
+
|
105
|
+
instance_eval(&block) if block
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'forwardable'
|
3
|
+
require 'sequel'
|
4
|
+
|
5
|
+
module Sinja
|
6
|
+
module Sequel
|
7
|
+
module Core
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def self.prepended(base)
|
11
|
+
base.sinja do |c|
|
12
|
+
c.conflict_exceptions << ::Sequel::ConstraintViolation
|
13
|
+
c.not_found_exceptions << ::Sequel::NoMatchingRow
|
14
|
+
c.validation_exceptions << ::Sequel::ValidationFailed
|
15
|
+
c.validation_formatter = ->(e) { e.errors.keys.zip(e.errors.full_messages) }
|
16
|
+
end
|
17
|
+
|
18
|
+
base.include Pagination if ::Sequel::Database::EXTENSIONS.key?(:pagination)
|
19
|
+
end
|
20
|
+
|
21
|
+
def_delegator ::Sequel::Model, :db, :database
|
22
|
+
|
23
|
+
def_delegator :database, :transaction
|
24
|
+
|
25
|
+
define_method :filter, proc(&:where)
|
26
|
+
|
27
|
+
def sort(collection, fields)
|
28
|
+
collection.order(*fields.map { |k, v| ::Sequel.send(v, k) })
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method :finalize, proc(&:all)
|
32
|
+
|
33
|
+
def validate!
|
34
|
+
raise ::Sequel::ValidationFailed, resource unless resource.valid?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module Pagination
|
39
|
+
def self.included(base)
|
40
|
+
base.sinja { |c| c.page_using = {
|
41
|
+
:number=>1,
|
42
|
+
:size=>10,
|
43
|
+
:record_count=>nil
|
44
|
+
}}
|
45
|
+
end
|
46
|
+
|
47
|
+
def page(collection, opts)
|
48
|
+
collection = collection.dataset unless collection.respond_to?(:paginate)
|
49
|
+
|
50
|
+
opts = settings._sinja.page_using.merge(opts)
|
51
|
+
collection = collection.paginate opts[:number].to_i, opts[:size].to_i,
|
52
|
+
(opts[:record_count].to_i if opts[:record_count])
|
53
|
+
|
54
|
+
# Attributes common to all pagination links
|
55
|
+
base = {
|
56
|
+
:size=>collection.page_size,
|
57
|
+
:record_count=>collection.pagination_record_count
|
58
|
+
}
|
59
|
+
|
60
|
+
pagination = {
|
61
|
+
:first=>base.merge(:number=>1),
|
62
|
+
:self=>base.merge(:number=>collection.current_page),
|
63
|
+
:last=>base.merge(:number=>collection.page_count)
|
64
|
+
}
|
65
|
+
pagination[:next] = base.merge(:number=>collection.next_page) if collection.next_page
|
66
|
+
pagination[:prev] = base.merge(:number=>collection.prev_page) if collection.prev_page
|
67
|
+
|
68
|
+
return collection, pagination
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'sinja/sequel/core'
|
3
|
+
|
4
|
+
module Sinja
|
5
|
+
module Sequel
|
6
|
+
module Helpers
|
7
|
+
def self.included(base)
|
8
|
+
base.prepend Core
|
9
|
+
end
|
10
|
+
|
11
|
+
def next_pk(resource, opts={})
|
12
|
+
[resource.pk, resource, opts]
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_remove(association, rios, try_convert=:to_i)
|
16
|
+
meth_suffix = association.to_s.singularize
|
17
|
+
add_meth = "add_#{meth_suffix}".to_sym
|
18
|
+
remove_meth = "remove_#{meth_suffix}".to_sym
|
19
|
+
|
20
|
+
dataset = resource.send("#{association}_dataset")
|
21
|
+
klass = dataset.association_reflection.associated_class
|
22
|
+
|
23
|
+
# does not / will not work with composite primary keys
|
24
|
+
new_ids = rios.map { |rio| rio[:id].send(try_convert) }
|
25
|
+
transaction do
|
26
|
+
resource.lock!
|
27
|
+
old_ids = dataset.select_map(klass.primary_key)
|
28
|
+
in_common = old_ids & new_ids
|
29
|
+
|
30
|
+
(new_ids - in_common).each do |id|
|
31
|
+
subresource = klass.with_pk!(id)
|
32
|
+
resource.send(add_meth, subresource) \
|
33
|
+
unless block_given? && !yield(subresource)
|
34
|
+
end
|
35
|
+
|
36
|
+
(old_ids - in_common).each do |id|
|
37
|
+
subresource = klass.with_pk!(id)
|
38
|
+
resource.send(remove_meth, subresource) \
|
39
|
+
unless block_given? && !yield(subresource)
|
40
|
+
end
|
41
|
+
|
42
|
+
resource.reload
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_missing(*args, &block)
|
47
|
+
add_or_remove(:add, :-, *args, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
def remove_present(*args, &block)
|
51
|
+
add_or_remove(:remove, :&, *args, &block)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def add_or_remove(meth_prefix, operator, association, rios, try_convert=:to_i)
|
57
|
+
meth = "#{meth_prefix}_#{association.to_s.singularize}".to_sym
|
58
|
+
transaction do
|
59
|
+
resource.lock!
|
60
|
+
venn(operator, association, rios, try_convert) do |subresource|
|
61
|
+
resource.send(meth, subresource) \
|
62
|
+
unless block_given? && !yield(subresource)
|
63
|
+
end
|
64
|
+
resource.reload
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def venn(operator, association, rios, try_convert)
|
69
|
+
dataset = resource.send("#{association}_dataset")
|
70
|
+
klass = dataset.association_reflection.associated_class
|
71
|
+
# does not / will not work with composite primary keys
|
72
|
+
rios.map { |rio| rio[:id].send(try_convert) }
|
73
|
+
.send(operator, dataset.select_map(klass.primary_key))
|
74
|
+
.each { |id| yield klass.with_pk!(id) }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sinja/sequel/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'sinja-sequel'
|
8
|
+
spec.version = Sinja::Sequel::VERSION
|
9
|
+
spec.authors = ['Mike Pastore']
|
10
|
+
spec.email = ['mike@oobak.org']
|
11
|
+
|
12
|
+
spec.summary = 'Sequel-specific Helpers and DSL for Sinja'
|
13
|
+
spec.homepage = 'https://github.com/mwpastore/sinja/tree/master/extensions/sequel'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.require_paths = %w[lib]
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 2.3.0'
|
22
|
+
|
23
|
+
spec.add_dependency 'sequel', '~> 4.0'
|
24
|
+
spec.add_dependency 'sinja', '>= 1.2.0.pre2', '< 2'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
27
|
+
spec.add_development_dependency 'minitest', '~> 5.9'
|
28
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinja-sequel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Pastore
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
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: sinja
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.0.pre2
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '2'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.2.0.pre2
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.11'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.11'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: minitest
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '5.9'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '5.9'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rake
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '12.0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '12.0'
|
89
|
+
description:
|
90
|
+
email:
|
91
|
+
- mike@oobak.org
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- Gemfile
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- lib/sinatra/jsonapi/sequel.rb
|
103
|
+
- lib/sinja-sequel.rb
|
104
|
+
- lib/sinja/sequel.rb
|
105
|
+
- lib/sinja/sequel/core.rb
|
106
|
+
- lib/sinja/sequel/helpers.rb
|
107
|
+
- lib/sinja/sequel/version.rb
|
108
|
+
- sinja-sequel.gemspec
|
109
|
+
homepage: https://github.com/mwpastore/sinja/tree/master/extensions/sequel
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: 2.3.0
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.6.8
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Sequel-specific Helpers and DSL for Sinja
|
133
|
+
test_files: []
|