sinja-sequel 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +11 -6
- data/lib/sinja/sequel/core.rb +13 -5
- data/lib/sinja/sequel/helpers.rb +20 -8
- data/lib/sinja/sequel/version.rb +1 -1
- data/lib/sinja/sequel.rb +14 -20
- data/sinja-sequel.gemspec +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb30aa642fe1f0894e0269a008db57334f870081
|
4
|
+
data.tar.gz: d1a3ede80eda81247c2040bf481317500f109961
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd5663290dbeb54f64ff993eca18f42ba0f062c2662c9438f50f0f8853adfbf4055c20dc8c208a1e4b1d44228436deaac50e60155330b1ae210861bea37aa085
|
7
|
+
data.tar.gz: 9e3e3a475b167c0b8421b2945d8ff0abdb3ead59baed29c942bb0a78b9609cee3a6a143199931cc7c1eec39cca1d6a55f54a62189dcc2e13bdcb65dc4efc8003
|
data/README.md
CHANGED
@@ -238,6 +238,10 @@ Like `add_missing` and `remove_present`, but performs an efficient delta
|
|
238
238
|
operation on the collection. Makes writing your `replace` action helpers a
|
239
239
|
breeze!
|
240
240
|
|
241
|
+
An optional block passed to this method will be used to filter both adds and
|
242
|
+
removes. To use different filters for the two operations, pass a hash of
|
243
|
+
callables (with keys `:add` and/or `:remove`).
|
244
|
+
|
241
245
|
### Extension
|
242
246
|
|
243
247
|
Register [Sinja::Sequel](/lib/sinja/sequel.rb) after
|
@@ -262,12 +266,9 @@ end
|
|
262
266
|
After registering the extension, the `resource`, `has_many`, and `has_one` DSL
|
263
267
|
keywords will generate basic action helpers.
|
264
268
|
|
265
|
-
* `resource` and `has_many` take an optional second argument
|
266
|
-
method to use to cast the ID of the resource or
|
267
|
-
object (`:to_i` by default).
|
268
|
-
|
269
|
-
* Optionally define a `settable_fields` helper in each resource that returns an
|
270
|
-
array of symbols to pass to `Sequel::Model#set_fields`.
|
269
|
+
* `resource` and `has_many`, and `has_one` take an optional second argument
|
270
|
+
that specifies the method to use to cast the ID of the resource or resource
|
271
|
+
identifier object(s) (`:to_i` by default).
|
271
272
|
|
272
273
|
* The generated action helpers will be unrestricted by default.
|
273
274
|
|
@@ -319,6 +320,10 @@ DB = Sequel.connect ENV['DB_URL']
|
|
319
320
|
OTHER_DB = Sequel.connect ENV['OTHER_DB_URL']
|
320
321
|
OTHER_DB.extension :pagination
|
321
322
|
|
323
|
+
# Sequel::Model.db now points to DB, which does not support pagination, so
|
324
|
+
# pagination will not be automatically enabled. We'll point Sinja::Sequel at
|
325
|
+
# OTHER_DB instead, and manually enable pagination...
|
326
|
+
|
322
327
|
class MyApp < Sinatra::Base
|
323
328
|
register Sinja
|
324
329
|
register Sinja::Sequel
|
data/lib/sinja/sequel/core.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'forwardable'
|
3
|
-
require 'set'
|
4
3
|
|
5
4
|
require 'sequel'
|
6
5
|
|
@@ -17,13 +16,22 @@ module Sinja
|
|
17
16
|
c.not_found_exceptions << ::Sequel::NoMatchingRow
|
18
17
|
c.validation_exceptions << ::Sequel::ValidationFailed
|
19
18
|
c.validation_formatter = proc do |e|
|
20
|
-
|
21
|
-
|
22
|
-
.
|
19
|
+
typeof = e.model.class.associations
|
20
|
+
.map { |k| [k, :relationships] }.to_h
|
21
|
+
.tap { |h| h.default = :attributes }
|
22
|
+
|
23
|
+
e.errors.flat_map do |ee|
|
24
|
+
next [[nil, ee]] if ee.is_a?(::Sequel::LiteralString)
|
25
|
+
|
26
|
+
key, messages = *ee
|
27
|
+
Array(messages).map do |message|
|
28
|
+
[key, "#{key} #{message}", typeof[key]]
|
29
|
+
end
|
30
|
+
end
|
23
31
|
end
|
24
32
|
end
|
25
33
|
|
26
|
-
base.prepend
|
34
|
+
base.prepend(Pagination) if ::Sequel::Model.db.dataset.respond_to?(:paginate)
|
27
35
|
end
|
28
36
|
|
29
37
|
def self.included(_)
|
data/lib/sinja/sequel/helpers.rb
CHANGED
@@ -1,22 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
2
4
|
require 'sinja/sequel/core'
|
3
5
|
|
4
6
|
module Sinja
|
5
7
|
module Sequel
|
6
8
|
module Helpers
|
7
9
|
def self.included(base)
|
8
|
-
base.prepend
|
10
|
+
base.prepend(Core)
|
11
|
+
end
|
12
|
+
|
13
|
+
def class_for_type(type)
|
14
|
+
@klass ||= {}
|
15
|
+
@klass[type] ||= type.underscore.classify.constantize
|
9
16
|
end
|
10
17
|
|
11
18
|
def next_pk(resource, opts={})
|
12
19
|
[resource.pk, resource, opts]
|
13
20
|
end
|
14
21
|
|
15
|
-
def add_remove(association, rios, try_convert=:to_i)
|
22
|
+
def add_remove(association, rios, try_convert=:to_i, **filters, &block)
|
16
23
|
meth_suffix = association.to_s.singularize
|
17
24
|
add_meth = "add_#{meth_suffix}".to_sym
|
18
25
|
remove_meth = "remove_#{meth_suffix}".to_sym
|
19
26
|
|
27
|
+
if block
|
28
|
+
filters[:add] ||= block
|
29
|
+
filters[:remove] ||= block
|
30
|
+
end
|
31
|
+
|
20
32
|
dataset = resource.send("#{association}_dataset")
|
21
33
|
klass = dataset.association_reflection.associated_class
|
22
34
|
|
@@ -29,14 +41,14 @@ module Sinja
|
|
29
41
|
|
30
42
|
(new_ids - ids_in_common).each do |id|
|
31
43
|
subresource = klass.with_pk!(id)
|
32
|
-
|
33
|
-
|
44
|
+
next if filters[:add] && !filters[:add].(subresource)
|
45
|
+
resource.send(add_meth, subresource)
|
34
46
|
end
|
35
47
|
|
36
48
|
(old_ids - ids_in_common).each do |id|
|
37
49
|
subresource = klass.with_pk!(id)
|
38
|
-
|
39
|
-
|
50
|
+
next if filters[:remove] && !filters[:remove].(subresource)
|
51
|
+
resource.send(remove_meth, subresource)
|
40
52
|
end
|
41
53
|
|
42
54
|
resource.reload
|
@@ -58,8 +70,8 @@ module Sinja
|
|
58
70
|
transaction do
|
59
71
|
resource.lock!
|
60
72
|
venn(operator, association, rios, try_convert) do |subresource|
|
61
|
-
|
62
|
-
|
73
|
+
next if block_given? && !yield(subresource)
|
74
|
+
resource.send(meth, subresource)
|
63
75
|
end
|
64
76
|
resource.reload
|
65
77
|
end
|
data/lib/sinja/sequel/version.rb
CHANGED
data/lib/sinja/sequel.rb
CHANGED
@@ -8,26 +8,28 @@ module Sinja
|
|
8
8
|
app.helpers Helpers
|
9
9
|
end
|
10
10
|
|
11
|
-
def resource(res, try_convert=:to_i, &block)
|
12
|
-
|
13
|
-
|
14
|
-
super(res) do
|
11
|
+
def resource(res, try_convert=:to_i, **opts, &block)
|
12
|
+
super(res, **opts) do
|
15
13
|
register Resource
|
16
14
|
|
17
15
|
helpers do
|
18
|
-
|
16
|
+
def dataset
|
19
17
|
klass.dataset
|
20
18
|
end
|
21
19
|
|
20
|
+
def klass
|
21
|
+
class_for_type(data[:type])
|
22
|
+
end
|
23
|
+
|
22
24
|
define_method(:find) do |id|
|
23
|
-
dataset
|
25
|
+
dataset.with_pk(id.send(try_convert))
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
27
29
|
show
|
28
30
|
|
29
31
|
show_many do |ids|
|
30
|
-
dataset.
|
32
|
+
dataset.where_all(klass.primary_key=>ids.map!(&try_convert))
|
31
33
|
end
|
32
34
|
|
33
35
|
index do
|
@@ -36,22 +38,14 @@ module Sinja
|
|
36
38
|
|
37
39
|
create do |attr|
|
38
40
|
tmp = klass.new
|
39
|
-
|
40
|
-
tmp.set_fields(attr, settable_fields)
|
41
|
-
else
|
42
|
-
tmp.set(attr)
|
43
|
-
end
|
41
|
+
tmp.set(attr)
|
44
42
|
tmp.save(:validate=>false)
|
45
43
|
next_pk tmp
|
46
44
|
end
|
47
45
|
|
48
46
|
update do |attr|
|
49
|
-
|
50
|
-
|
51
|
-
else
|
52
|
-
resource.set(attr)
|
53
|
-
resource.save_changes(:validate=>false)
|
54
|
-
end
|
47
|
+
resource.set(attr)
|
48
|
+
resource.save_changes(:validate=>false)
|
55
49
|
end
|
56
50
|
|
57
51
|
destroy do
|
@@ -63,7 +57,7 @@ module Sinja
|
|
63
57
|
end
|
64
58
|
|
65
59
|
module Resource
|
66
|
-
def has_one(rel, &block)
|
60
|
+
def has_one(rel, try_convert=:to_i, &block)
|
67
61
|
super(rel) do
|
68
62
|
pluck do
|
69
63
|
resource.send(rel)
|
@@ -76,7 +70,7 @@ module Sinja
|
|
76
70
|
|
77
71
|
graft(:sideload_on=>%i[create update]) do |rio|
|
78
72
|
klass = resource.class.association_reflection(rel).associated_class
|
79
|
-
resource.send("#{rel}=", klass.with_pk!(rio[:id]))
|
73
|
+
resource.send("#{rel}=", klass.with_pk!(rio[:id].send(try_convert)))
|
80
74
|
resource.save_changes(:validate=>!sideloaded?)
|
81
75
|
end
|
82
76
|
|
data/sinja-sequel.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.required_ruby_version = '>= 2.3.0'
|
22
22
|
|
23
|
-
spec.add_dependency 'sequel', '~> 4.
|
23
|
+
spec.add_dependency 'sequel', '~> 4.44'
|
24
24
|
spec.add_dependency 'sinja', '~> 1.2'
|
25
25
|
|
26
26
|
spec.add_development_dependency 'bundler', '~> 1.11'
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Pastore
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '4.
|
19
|
+
version: '4.44'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '4.
|
26
|
+
version: '4.44'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sinja
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.6.
|
125
|
+
rubygems_version: 2.6.10
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: Sequel-specific Helpers and DSL for Sinja
|