shrine 3.0.0.alpha → 3.0.0.beta

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.

Potentially problematic release.


This version of shrine might be problematic. Click here for more details.

Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -2
  3. data/README.md +1 -1
  4. data/doc/creating_persistence_plugins.md +20 -63
  5. data/doc/plugins/activerecord.md +13 -106
  6. data/doc/plugins/add_metadata.md +31 -9
  7. data/doc/plugins/atomic_helpers.md +33 -9
  8. data/doc/plugins/cached_attachment_data.md +3 -3
  9. data/doc/plugins/data_uri.md +16 -19
  10. data/doc/plugins/default_storage.md +32 -8
  11. data/doc/plugins/default_url.md +21 -9
  12. data/doc/plugins/derivation_endpoint.md +48 -0
  13. data/doc/plugins/derivatives.md +74 -63
  14. data/doc/plugins/entity.md +27 -2
  15. data/doc/plugins/included.md +8 -7
  16. data/doc/plugins/metadata_attributes.md +20 -5
  17. data/doc/plugins/model.md +22 -2
  18. data/doc/plugins/persistence.md +89 -0
  19. data/doc/plugins/remote_url.md +41 -45
  20. data/doc/plugins/remove_attachment.md +23 -4
  21. data/doc/plugins/remove_invalid.md +3 -4
  22. data/doc/plugins/restore_cached_data.md +3 -1
  23. data/doc/plugins/sequel.md +13 -105
  24. data/doc/plugins/signature.md +3 -3
  25. data/lib/shrine/attachment.rb +11 -1
  26. data/lib/shrine/plugins/_persistence.rb +69 -0
  27. data/lib/shrine/plugins/activerecord.rb +31 -81
  28. data/lib/shrine/plugins/atomic_helpers.rb +13 -3
  29. data/lib/shrine/plugins/backgrounding.rb +8 -8
  30. data/lib/shrine/plugins/cached_attachment_data.rb +2 -6
  31. data/lib/shrine/plugins/data_uri.rb +2 -6
  32. data/lib/shrine/plugins/default_storage.rb +28 -2
  33. data/lib/shrine/plugins/derivation_endpoint.rb +3 -9
  34. data/lib/shrine/plugins/derivatives.rb +26 -17
  35. data/lib/shrine/plugins/entity.rb +24 -16
  36. data/lib/shrine/plugins/included.rb +1 -0
  37. data/lib/shrine/plugins/infer_extension.rb +2 -0
  38. data/lib/shrine/plugins/metadata_attributes.rb +18 -8
  39. data/lib/shrine/plugins/model.rb +35 -14
  40. data/lib/shrine/plugins/remote_url.rb +2 -6
  41. data/lib/shrine/plugins/remove_attachment.rb +2 -6
  42. data/lib/shrine/plugins/remove_invalid.rb +10 -6
  43. data/lib/shrine/plugins/sequel.rb +31 -78
  44. data/lib/shrine/plugins/upload_options.rb +2 -2
  45. data/lib/shrine/plugins/validation.rb +1 -11
  46. data/lib/shrine/version.rb +1 -1
  47. metadata +4 -2
@@ -10,7 +10,7 @@ class Shrine
10
10
  module Sequel
11
11
  def self.load_dependencies(uploader, **)
12
12
  uploader.plugin :model
13
- uploader.plugin :atomic_helpers
13
+ uploader.plugin :_persistence, plugin: self
14
14
  end
15
15
 
16
16
  def self.configure(uploader, **opts)
@@ -24,16 +24,16 @@ class Shrine
24
24
 
25
25
  return unless model < ::Sequel::Model
26
26
 
27
- name = attachment_name
27
+ name = @name
28
28
 
29
29
  if shrine_class.opts[:sequel][:validations]
30
+ # add validation plugin integration
30
31
  define_method :validate do
31
32
  super()
32
- # validation plugin integration
33
- if send(:"#{name}_attacher").respond_to?(:errors)
34
- send(:"#{name}_attacher").errors.each do |message|
35
- errors.add(name, *message)
36
- end
33
+ return unless send(:"#{name}_attacher").respond_to?(:errors)
34
+
35
+ send(:"#{name}_attacher").errors.each do |message|
36
+ errors.add(name, *message)
37
37
  end
38
38
  end
39
39
  end
@@ -51,7 +51,7 @@ class Shrine
51
51
  if send(:"#{name}_attacher").changed?
52
52
  db.after_commit do
53
53
  send(:"#{name}_attacher").finalize
54
- send(:"#{name}_attacher").sequel_persist
54
+ send(:"#{name}_attacher").persist
55
55
  end
56
56
  end
57
57
  end
@@ -77,78 +77,13 @@ class Shrine
77
77
  end
78
78
 
79
79
  module AttacherMethods
80
- # Promotes cached file to permanent storage in an atomic way. It's
81
- # intended to be called from a background job.
82
- #
83
- # attacher.assign(file)
84
- # attacher.cached? #=> true
85
- #
86
- # # ... in background job ...
87
- #
88
- # attacher.atomic_promote
89
- # attacher.stored? #=> true
90
- #
91
- # It accepts `:reload` and `:persist` strategies:
92
- #
93
- # attacher.atomic_promote(reload: :lock) # uses database locking (default)
94
- # attacher.atomic_promote(reload: :fetch) # reloads with no locking
95
- # attacher.atomic_promote(reload: ->(&b){}) # custom reloader
96
- # attacher.atomic_promote(reload: false) # skips reloading
97
- #
98
- # attacher.atomic_promote(persist: :save) # persists stored file (default)
99
- # attacher.atomic_promote(persist: ->{}) # custom persister
100
- # attacher.atomic_promote(persist: false) # skips persistence
101
- def sequel_atomic_promote(**options, &block)
102
- abstract_atomic_promote(sequel_strategies(**options), &block)
103
- end
104
- alias atomic_promote sequel_atomic_promote
105
-
106
- # Persist the the record only if the attachment hasn't changed.
107
- # Optionally yields reloaded attacher to the block before persisting.
108
- # It's intended to be called from a background job.
80
+ # The _persistence plugin defines the following methods:
109
81
  #
110
- # # ... in background job ...
111
- #
112
- # attacher.file.metadata["foo"] = "bar"
113
- # attacher.write
114
- #
115
- # attacher.atomic_persist
116
- def sequel_atomic_persist(*args, **options, &block)
117
- abstract_atomic_persist(*args, sequel_strategies(**options), &block)
118
- end
119
- alias atomic_persist sequel_atomic_persist
120
-
121
- # Called in the `after_commit` callback after finalization.
122
- def sequel_persist
123
- sequel_save
124
- end
125
- alias persist sequel_persist
126
-
82
+ # * #persist (calls #sequel_persist and #sequel?)
83
+ # * #atomic_persist (calls #sequel_lock, #sequel_persist and #sequel?)
84
+ # * #atomic_promote (calls #sequel_lock, #sequel_persist and #sequel?)
127
85
  private
128
86
 
129
- # Resolves strategies for atomic promotion and persistence.
130
- def sequel_strategies(reload: :lock, persist: :save, **options)
131
- reload = method(:"sequel_#{reload}") if reload.is_a?(Symbol)
132
- persist = method(:"sequel_#{persist}") if persist.is_a?(Symbol)
133
-
134
- { reload: reload, persist: persist, **options }
135
- end
136
-
137
- # Implements the "fetch" reload strategy for #sequel_promote.
138
- def sequel_fetch
139
- yield record.dup.refresh
140
- end
141
-
142
- # Implements the "lock" reload strategy for #sequel_promote.
143
- def sequel_lock
144
- record.db.transaction { yield record.dup.lock! }
145
- end
146
-
147
- # Implements the "save" persist strategy for #sequel_promote.
148
- def sequel_save
149
- record.save_changes(validate: false)
150
- end
151
-
152
87
  # Sequel JSON column attribute with `pg_json` Sequel extension loaded
153
88
  # returns a `Sequel::Postgres::JSONHashBase` object will be returned,
154
89
  # which we convert into a Hash.
@@ -165,11 +100,29 @@ class Shrine
165
100
 
166
101
  # Returns true if the data attribute represents a JSON or JSONB column.
167
102
  def sequel_json_column?
168
- return false unless record.is_a?(::Sequel::Model)
103
+ return false unless sequel?
169
104
  return false unless column = record.class.db_schema[attribute]
170
105
 
171
106
  [:json, :jsonb].include?(column[:type])
172
107
  end
108
+
109
+ # Saves changes to the model instance, skipping validations. Used by
110
+ # the _persistence plugin.
111
+ def sequel_persist
112
+ record.save_changes(validate: false)
113
+ end
114
+
115
+ # Locks the database row and yields the reloaded record. Used by the
116
+ # _persistence plugin.
117
+ def sequel_reload
118
+ record.db.transaction { yield record.dup.lock! }
119
+ end
120
+
121
+ # Returns whether the record is a Sequel model. Used by the
122
+ # _persistence plugin.
123
+ def sequel?
124
+ record.is_a?(::Sequel::Model)
125
+ end
173
126
  end
174
127
  end
175
128
 
@@ -12,14 +12,14 @@ class Shrine
12
12
  end
13
13
 
14
14
  module InstanceMethods
15
+ private
16
+
15
17
  def _upload(io, **options)
16
18
  upload_options = get_upload_options(io, options)
17
19
 
18
20
  super(io, **options, upload_options: upload_options)
19
21
  end
20
22
 
21
- private
22
-
23
23
  def get_upload_options(io, options)
24
24
  upload_options = opts[:upload_options][storage_key] || {}
25
25
  upload_options = upload_options.call(io, options) if upload_options.respond_to?(:call)
@@ -28,16 +28,6 @@ class Shrine
28
28
  @errors = []
29
29
  end
30
30
 
31
- # Registers options that will be passed to validation.
32
- def validate_options(options = nil)
33
- if options
34
- @validate_options ||= {}
35
- @validate_options.merge!(options)
36
- else
37
- defined?(@validate_options) ? @validate_options : {}
38
- end
39
- end
40
-
41
31
  # Leaves out :validate option when calling `Shrine.upload`.
42
32
  def upload(*args, validate: nil, **options)
43
33
  super(*args, **options)
@@ -72,7 +62,7 @@ class Shrine
72
62
  if method(:validate_block).arity.zero?
73
63
  validate_block
74
64
  else
75
- validate_block(**validate_options, **options)
65
+ validate_block(**options)
76
66
  end
77
67
  end
78
68
 
@@ -9,7 +9,7 @@ class Shrine
9
9
  MAJOR = 3
10
10
  MINOR = 0
11
11
  TINY = 0
12
- PRE = "alpha"
12
+ PRE = "beta"
13
13
 
14
14
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
15
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.alpha
4
+ version: 3.0.0.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-19 00:00:00.000000000 Z
11
+ date: 2019-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: down
@@ -400,6 +400,7 @@ files:
400
400
  - doc/plugins/metadata_attributes.md
401
401
  - doc/plugins/model.md
402
402
  - doc/plugins/module_include.md
403
+ - doc/plugins/persistence.md
403
404
  - doc/plugins/presign_endpoint.md
404
405
  - doc/plugins/pretty_location.md
405
406
  - doc/plugins/processing.md
@@ -466,6 +467,7 @@ files:
466
467
  - lib/shrine/attacher.rb
467
468
  - lib/shrine/attachment.rb
468
469
  - lib/shrine/plugins.rb
470
+ - lib/shrine/plugins/_persistence.rb
469
471
  - lib/shrine/plugins/_urlsafe_serialization.rb
470
472
  - lib/shrine/plugins/activerecord.rb
471
473
  - lib/shrine/plugins/add_metadata.rb