sinja-sequel 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb30aa642fe1f0894e0269a008db57334f870081
4
- data.tar.gz: d1a3ede80eda81247c2040bf481317500f109961
3
+ metadata.gz: 32bb578a115bf6a9131d63b7cad0c4a88c4a39a8
4
+ data.tar.gz: f24e404c4e24ded1f117b044c6c2240fb7b5d27e
5
5
  SHA512:
6
- metadata.gz: fd5663290dbeb54f64ff993eca18f42ba0f062c2662c9438f50f0f8853adfbf4055c20dc8c208a1e4b1d44228436deaac50e60155330b1ae210861bea37aa085
7
- data.tar.gz: 9e3e3a475b167c0b8421b2945d8ff0abdb3ead59baed29c942bb0a78b9609cee3a6a143199931cc7c1eec39cca1d6a55f54a62189dcc2e13bdcb65dc4efc8003
6
+ metadata.gz: af4a7ad263d1a02a67f2c91f7775c5237e40b1d996043180226251968d1d7e57cfb64a01dc83befa535df9193c709fdc7a81a8322571fa39d6920944403ed730
7
+ data.tar.gz: 0bad99425681c3625a336c0c605246c008fe8c060894d24d291f863dbf00529c24ed36b5b41d11f55426029dc9626f6a505bc487945b8a8759cc0fbf8ffe145c
data/README.md CHANGED
@@ -261,7 +261,8 @@ class MyApp < Sinatra::Base
261
261
  end
262
262
  ```
263
263
 
264
- **Note that registering the extension will automatically include Helpers!**
264
+ **Note that registering the extension will automatically include Helpers and
265
+ prepend Core!**
265
266
 
266
267
  After registering the extension, the `resource`, `has_many`, and `has_one` DSL
267
268
  keywords will generate basic action helpers.
data/lib/sinja/sequel.rb CHANGED
@@ -9,20 +9,20 @@ module Sinja
9
9
  end
10
10
 
11
11
  def resource(res, try_convert=:to_i, **opts, &block)
12
+ klass = opts.fetch(:class) { res.to_s.classify.constantize }
13
+
12
14
  super(res, **opts) do
13
15
  register Resource
14
16
 
15
17
  helpers do
16
- def dataset
18
+ define_method(:default_dataset) do
17
19
  klass.dataset
18
20
  end
19
21
 
20
- def klass
21
- class_for_type(data[:type])
22
- end
22
+ alias_method :dataset, :default_dataset
23
23
 
24
24
  define_method(:find) do |id|
25
- dataset.with_pk(id.send(try_convert))
25
+ dataset.with_pk(proc(&try_convert).(id))
26
26
  end
27
27
  end
28
28
 
@@ -37,10 +37,7 @@ module Sinja
37
37
  end
38
38
 
39
39
  create do |attr|
40
- tmp = klass.new
41
- tmp.set(attr)
42
- tmp.save(:validate=>false)
43
- next_pk tmp
40
+ next_pk klass.new(attr)
44
41
  end
45
42
 
46
43
  update do |attr|
@@ -65,12 +62,12 @@ module Sinja
65
62
 
66
63
  prune(:sideload_on=>:update) do
67
64
  resource.send("#{rel}=", nil)
68
- resource.save_changes
65
+ resource.save_changes(:validate=>!sideloaded?)
69
66
  end
70
67
 
71
68
  graft(:sideload_on=>%i[create update]) do |rio|
72
69
  klass = resource.class.association_reflection(rel).associated_class
73
- resource.send("#{rel}=", klass.with_pk!(rio[:id].send(try_convert)))
70
+ resource.send("#{rel}=", klass.with_pk!(proc(&try_convert).(rio[:id])))
74
71
  resource.save_changes(:validate=>!sideloaded?)
75
72
  end
76
73
 
@@ -11,11 +11,22 @@ module Sinja
11
11
  extend Forwardable
12
12
 
13
13
  def self.prepended(base)
14
- base.sinja do |c|
14
+ base.sinja.configure do |c|
15
15
  c.conflict_exceptions << ::Sequel::ConstraintViolation
16
16
  c.not_found_exceptions << ::Sequel::NoMatchingRow
17
17
  c.validation_exceptions << ::Sequel::ValidationFailed
18
18
  c.validation_formatter = proc do |e|
19
+ e.errors.keys.each do |column|
20
+ if assocs = e.model.class.autoreloading_associations[column]
21
+ # copy errors attached to a FK column to the relevant association
22
+ assocs.each do |assoc|
23
+ e.errors[assoc] = e.errors[column]
24
+ end
25
+
26
+ e.errors.delete(column)
27
+ end
28
+ end
29
+
19
30
  typeof = e.model.class.associations
20
31
  .map { |k| [k, :relationships] }.to_h
21
32
  .tap { |h| h.default = :attributes }
@@ -42,17 +53,32 @@ module Sinja
42
53
 
43
54
  def_delegator :database, :transaction
44
55
 
45
- define_method :filter, proc(&:where)
56
+ define_method :filter, &:where
46
57
 
47
58
  def sort(collection, fields)
48
59
  collection.order(*fields.map { |k, v| ::Sequel.send(v, k) })
49
60
  end
50
61
 
51
- define_method :finalize, proc(&:all)
62
+ define_method :finalize, &:all
52
63
 
53
64
  def validate!
54
65
  raise ::Sequel::ValidationFailed, resource unless resource.valid?
55
66
  end
67
+
68
+ def after_create
69
+ resource.save(:validate=>false)
70
+ resource.pk
71
+ end
72
+
73
+ def serialize_linkage(*, **options)
74
+ options[:skip_collection_check] = true
75
+ super
76
+ end
77
+
78
+ def serialize_model(*, **options)
79
+ options[:skip_collection_check] = true
80
+ super
81
+ end
56
82
  end
57
83
  end
58
84
  end
@@ -1,6 +1,4 @@
1
1
  # frozen_string_literal: true
2
- require 'active_support/inflector'
3
-
4
2
  require 'sinja/sequel/core'
5
3
 
6
4
  module Sinja
@@ -10,11 +8,6 @@ module Sinja
10
8
  base.prepend(Core)
11
9
  end
12
10
 
13
- def class_for_type(type)
14
- @klass ||= {}
15
- @klass[type] ||= type.underscore.classify.constantize
16
- end
17
-
18
11
  def next_pk(resource, opts={})
19
12
  [resource.pk, resource, opts]
20
13
  end
@@ -33,7 +26,7 @@ module Sinja
33
26
  klass = dataset.association_reflection.associated_class
34
27
 
35
28
  # does not / will not work with composite primary keys
36
- new_ids = rios.map { |rio| rio[:id].send(try_convert) }
29
+ new_ids = rios.map { |rio| proc(&try_convert).(rio[:id]) }
37
30
  transaction do
38
31
  resource.lock!
39
32
  old_ids = dataset.select_map(klass.primary_key)
@@ -81,7 +74,7 @@ module Sinja
81
74
  dataset = resource.send("#{association}_dataset")
82
75
  klass = dataset.association_reflection.associated_class
83
76
  # does not / will not work with composite primary keys
84
- rios.map { |rio| rio[:id].send(try_convert) }
77
+ rios.map { |rio| proc(&try_convert).(rio[:id]) }
85
78
  .send(operator, dataset.select_map(klass.primary_key))
86
79
  .each { |id| yield klass.with_pk!(id) }
87
80
  end
@@ -3,7 +3,7 @@ module Sinja
3
3
  module Sequel
4
4
  module Pagination
5
5
  def self.prepended(base)
6
- base.sinja { |c| c.page_using = {
6
+ base.sinja.configure { |c| c.page_using = {
7
7
  :number=>1,
8
8
  :size=>10,
9
9
  :record_count=>nil
@@ -15,10 +15,13 @@ module Sinja
15
15
  end
16
16
 
17
17
  def page(collection, opts)
18
- collection = collection.dataset unless collection.respond_to?(:paginate)
18
+ return collection, {} unless collection.respond_to?(:paginate) ||
19
+ collection.respond_to?(:dataset) && (collection = collection.dataset).respond_to?(:paginate)
19
20
 
20
21
  opts = settings._sinja.page_using.merge(opts)
21
- collection = collection.paginate opts[:number].to_i, opts[:size].to_i,
22
+ collection = collection.paginate \
23
+ opts[:number].to_i,
24
+ opts[:size].to_i,
22
25
  (opts[:record_count].to_i if opts[:record_count])
23
26
 
24
27
  # Attributes common to all pagination links
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module Sinja
3
3
  module Sequel
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.7'
5
5
  end
6
6
  end
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.44'
23
+ spec.add_dependency 'sequel', '>= 4.49', '< 6'
24
24
  spec.add_dependency 'sinja', '~> 1.2'
25
25
 
26
26
  spec.add_development_dependency 'bundler', '~> 1.11'
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinja-sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
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-03-08 00:00:00.000000000 Z
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.49'
20
+ - - "<"
18
21
  - !ruby/object:Gem::Version
19
- version: '4.44'
22
+ version: '6'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.49'
30
+ - - "<"
25
31
  - !ruby/object:Gem::Version
26
- version: '4.44'
32
+ version: '6'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: sinja
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -122,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
128
  version: '0'
123
129
  requirements: []
124
130
  rubyforge_project:
125
- rubygems_version: 2.6.10
131
+ rubygems_version: 2.6.13
126
132
  signing_key:
127
133
  specification_version: 4
128
134
  summary: Sequel-specific Helpers and DSL for Sinja