shrine 2.19.3 → 3.6.0

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.
Files changed (211) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +523 -41
  3. data/LICENSE.txt +1 -1
  4. data/README.md +83 -979
  5. data/doc/advantages.md +231 -204
  6. data/doc/attacher.md +304 -153
  7. data/doc/carrierwave.md +297 -226
  8. data/doc/changing_derivatives.md +308 -0
  9. data/doc/changing_location.md +103 -21
  10. data/doc/changing_storage.md +110 -0
  11. data/doc/creating_persistence_plugins.md +132 -0
  12. data/doc/creating_plugins.md +43 -23
  13. data/doc/creating_storages.md +19 -5
  14. data/doc/design.md +147 -97
  15. data/doc/direct_s3.md +38 -28
  16. data/doc/external/articles.md +63 -0
  17. data/doc/external/extensions.md +53 -0
  18. data/doc/external/misc.md +32 -0
  19. data/doc/getting_started.md +1156 -0
  20. data/doc/metadata.md +190 -109
  21. data/doc/multiple_files.md +93 -30
  22. data/doc/paperclip.md +384 -262
  23. data/doc/plugins/activerecord.md +177 -46
  24. data/doc/plugins/add_metadata.md +139 -38
  25. data/doc/plugins/atomic_helpers.md +217 -0
  26. data/doc/plugins/backgrounding.md +156 -98
  27. data/doc/plugins/cached_attachment_data.md +7 -5
  28. data/doc/plugins/column.md +121 -0
  29. data/doc/plugins/data_uri.md +23 -22
  30. data/doc/plugins/default_storage.md +36 -10
  31. data/doc/plugins/default_url.md +30 -13
  32. data/doc/plugins/delete_raw.md +4 -2
  33. data/doc/plugins/derivation_endpoint.md +186 -101
  34. data/doc/plugins/derivatives.md +839 -0
  35. data/doc/plugins/determine_mime_type.md +4 -2
  36. data/doc/plugins/download_endpoint.md +64 -8
  37. data/doc/plugins/dynamic_storage.md +5 -3
  38. data/doc/plugins/entity.md +263 -0
  39. data/doc/plugins/form_assign.md +55 -0
  40. data/doc/plugins/included.md +31 -8
  41. data/doc/plugins/infer_extension.md +21 -10
  42. data/doc/plugins/instrumentation.md +38 -16
  43. data/doc/plugins/keep_files.md +16 -17
  44. data/doc/plugins/metadata_attributes.md +42 -13
  45. data/doc/plugins/mirroring.md +118 -0
  46. data/doc/plugins/model.md +210 -0
  47. data/doc/plugins/module_include.md +4 -2
  48. data/doc/plugins/multi_cache.md +24 -0
  49. data/doc/plugins/persistence.md +101 -0
  50. data/doc/plugins/presign_endpoint.md +9 -4
  51. data/doc/plugins/pretty_location.md +16 -3
  52. data/doc/plugins/processing.md +4 -2
  53. data/doc/plugins/rack_file.md +8 -2
  54. data/doc/plugins/rack_response.md +6 -2
  55. data/doc/plugins/recache.md +4 -2
  56. data/doc/plugins/refresh_metadata.md +49 -9
  57. data/doc/plugins/remote_url.md +84 -47
  58. data/doc/plugins/remove_attachment.md +27 -6
  59. data/doc/plugins/remove_invalid.md +21 -6
  60. data/doc/plugins/restore_cached_data.md +11 -3
  61. data/doc/plugins/sequel.md +159 -35
  62. data/doc/plugins/signature.md +16 -5
  63. data/doc/plugins/store_dimensions.md +14 -2
  64. data/doc/plugins/tempfile.md +4 -2
  65. data/doc/plugins/type_predicates.md +96 -0
  66. data/doc/plugins/upload_endpoint.md +13 -13
  67. data/doc/plugins/upload_options.md +6 -4
  68. data/doc/plugins/{default_url_options.md → url_options.md} +9 -7
  69. data/doc/plugins/validation.md +97 -0
  70. data/doc/plugins/validation_helpers.md +16 -13
  71. data/doc/plugins/versions.md +15 -19
  72. data/doc/processing.md +438 -221
  73. data/doc/refile.md +188 -170
  74. data/doc/release_notes/1.0.0.md +4 -0
  75. data/doc/release_notes/1.1.0.md +6 -2
  76. data/doc/release_notes/1.2.0.md +4 -0
  77. data/doc/release_notes/1.3.0.md +4 -0
  78. data/doc/release_notes/1.4.0.md +4 -0
  79. data/doc/release_notes/1.4.1.md +4 -0
  80. data/doc/release_notes/1.4.2.md +4 -0
  81. data/doc/release_notes/2.0.0.md +4 -0
  82. data/doc/release_notes/2.0.1.md +4 -0
  83. data/doc/release_notes/2.1.0.md +5 -1
  84. data/doc/release_notes/2.1.1.md +4 -0
  85. data/doc/release_notes/2.10.0.md +4 -0
  86. data/doc/release_notes/2.10.1.md +4 -0
  87. data/doc/release_notes/2.11.0.md +4 -0
  88. data/doc/release_notes/2.12.0.md +4 -0
  89. data/doc/release_notes/2.13.0.md +4 -0
  90. data/doc/release_notes/2.14.0.md +5 -1
  91. data/doc/release_notes/2.15.0.md +11 -7
  92. data/doc/release_notes/2.16.0.md +4 -0
  93. data/doc/release_notes/2.17.0.md +4 -0
  94. data/doc/release_notes/2.18.0.md +4 -0
  95. data/doc/release_notes/2.19.0.md +6 -3
  96. data/doc/release_notes/2.2.0.md +4 -0
  97. data/doc/release_notes/2.3.0.md +4 -0
  98. data/doc/release_notes/2.3.1.md +4 -0
  99. data/doc/release_notes/2.4.0.md +4 -0
  100. data/doc/release_notes/2.4.1.md +4 -0
  101. data/doc/release_notes/2.5.0.md +4 -0
  102. data/doc/release_notes/2.6.0.md +4 -0
  103. data/doc/release_notes/2.6.1.md +4 -0
  104. data/doc/release_notes/2.7.0.md +4 -0
  105. data/doc/release_notes/2.8.0.md +4 -0
  106. data/doc/release_notes/2.9.0.md +4 -0
  107. data/doc/release_notes/3.0.0.md +981 -0
  108. data/doc/release_notes/3.0.1.md +22 -0
  109. data/doc/release_notes/3.1.0.md +73 -0
  110. data/doc/release_notes/3.2.0.md +96 -0
  111. data/doc/release_notes/3.2.1.md +31 -0
  112. data/doc/release_notes/3.2.2.md +14 -0
  113. data/doc/release_notes/3.3.0.md +105 -0
  114. data/doc/release_notes/3.4.0.md +35 -0
  115. data/doc/release_notes/3.5.0.md +63 -0
  116. data/doc/release_notes/3.6.0.md +23 -0
  117. data/doc/retrieving_uploads.md +5 -2
  118. data/doc/securing_uploads.md +60 -37
  119. data/doc/storage/file_system.md +20 -3
  120. data/doc/storage/memory.md +19 -0
  121. data/doc/storage/s3.md +122 -78
  122. data/doc/testing.md +141 -133
  123. data/doc/upgrading_to_3.md +708 -0
  124. data/doc/validation.md +54 -90
  125. data/lib/shrine/attacher.rb +292 -169
  126. data/lib/shrine/attachment.rb +13 -46
  127. data/lib/shrine/plugins/_persistence.rb +93 -0
  128. data/lib/shrine/plugins/activerecord.rb +77 -34
  129. data/lib/shrine/plugins/add_metadata.rb +25 -17
  130. data/lib/shrine/plugins/atomic_helpers.rb +119 -0
  131. data/lib/shrine/plugins/backgrounding.rb +77 -113
  132. data/lib/shrine/plugins/cached_attachment_data.rb +6 -15
  133. data/lib/shrine/plugins/column.rb +102 -0
  134. data/lib/shrine/plugins/data_uri.rb +38 -36
  135. data/lib/shrine/plugins/default_storage.rb +45 -15
  136. data/lib/shrine/plugins/default_url.rb +12 -24
  137. data/lib/shrine/plugins/default_url_options.rb +3 -30
  138. data/lib/shrine/plugins/delete_raw.rb +10 -16
  139. data/lib/shrine/plugins/derivation_endpoint.rb +130 -171
  140. data/lib/shrine/plugins/derivatives.rb +645 -0
  141. data/lib/shrine/plugins/determine_mime_type.rb +9 -21
  142. data/lib/shrine/plugins/download_endpoint.rb +118 -133
  143. data/lib/shrine/plugins/dynamic_storage.rb +5 -11
  144. data/lib/shrine/plugins/entity.rb +158 -0
  145. data/lib/shrine/plugins/form_assign.rb +108 -0
  146. data/lib/shrine/plugins/included.rb +6 -6
  147. data/lib/shrine/plugins/infer_extension.rb +17 -20
  148. data/lib/shrine/plugins/instrumentation.rb +59 -43
  149. data/lib/shrine/plugins/keep_files.rb +3 -15
  150. data/lib/shrine/plugins/metadata_attributes.rb +28 -19
  151. data/lib/shrine/plugins/mirroring.rb +142 -0
  152. data/lib/shrine/plugins/model.rb +160 -0
  153. data/lib/shrine/plugins/module_include.rb +3 -3
  154. data/lib/shrine/plugins/multi_cache.rb +27 -0
  155. data/lib/shrine/plugins/presign_endpoint.rb +27 -28
  156. data/lib/shrine/plugins/pretty_location.rb +15 -9
  157. data/lib/shrine/plugins/processing.rb +22 -9
  158. data/lib/shrine/plugins/rack_file.rb +2 -42
  159. data/lib/shrine/plugins/rack_response.rb +21 -10
  160. data/lib/shrine/plugins/recache.rb +6 -5
  161. data/lib/shrine/plugins/refresh_metadata.rb +13 -11
  162. data/lib/shrine/plugins/remote_url.rb +49 -49
  163. data/lib/shrine/plugins/remove_attachment.rb +12 -6
  164. data/lib/shrine/plugins/remove_invalid.rb +19 -8
  165. data/lib/shrine/plugins/restore_cached_data.rb +13 -7
  166. data/lib/shrine/plugins/sequel.rb +86 -36
  167. data/lib/shrine/plugins/signature.rb +10 -16
  168. data/lib/shrine/plugins/store_dimensions.rb +35 -40
  169. data/lib/shrine/plugins/tempfile.rb +1 -3
  170. data/lib/shrine/plugins/type_predicates.rb +113 -0
  171. data/lib/shrine/plugins/upload_endpoint.rb +28 -24
  172. data/lib/shrine/plugins/upload_options.rb +14 -15
  173. data/lib/shrine/plugins/url_options.rb +31 -0
  174. data/lib/shrine/plugins/validation.rb +80 -0
  175. data/lib/shrine/plugins/validation_helpers.rb +35 -58
  176. data/lib/shrine/plugins/versions.rb +107 -87
  177. data/lib/shrine/plugins.rb +22 -0
  178. data/lib/shrine/storage/file_system.rb +46 -64
  179. data/lib/shrine/storage/linter.rb +42 -7
  180. data/lib/shrine/storage/memory.rb +49 -0
  181. data/lib/shrine/storage/s3.rb +173 -160
  182. data/lib/shrine/uploaded_file.rb +32 -32
  183. data/lib/shrine/version.rb +3 -3
  184. data/lib/shrine.rb +87 -150
  185. data/shrine.gemspec +11 -12
  186. metadata +92 -82
  187. data/doc/migrating_storage.md +0 -76
  188. data/doc/plugins/backup.md +0 -31
  189. data/doc/plugins/copy.md +0 -24
  190. data/doc/plugins/delete_promoted.md +0 -12
  191. data/doc/plugins/direct_upload.md +0 -172
  192. data/doc/plugins/hooks.md +0 -58
  193. data/doc/plugins/logging.md +0 -42
  194. data/doc/plugins/migration_helpers.md +0 -60
  195. data/doc/plugins/moving.md +0 -19
  196. data/doc/plugins/multi_delete.md +0 -20
  197. data/doc/plugins/parallelize.md +0 -16
  198. data/doc/plugins/parsed_json.md +0 -23
  199. data/doc/regenerating_versions.md +0 -143
  200. data/lib/shrine/plugins/background_helpers.rb +0 -5
  201. data/lib/shrine/plugins/backup.rb +0 -90
  202. data/lib/shrine/plugins/copy.rb +0 -50
  203. data/lib/shrine/plugins/delete_promoted.rb +0 -20
  204. data/lib/shrine/plugins/direct_upload.rb +0 -217
  205. data/lib/shrine/plugins/hooks.rb +0 -90
  206. data/lib/shrine/plugins/logging.rb +0 -142
  207. data/lib/shrine/plugins/migration_helpers.rb +0 -70
  208. data/lib/shrine/plugins/moving.rb +0 -57
  209. data/lib/shrine/plugins/multi_delete.rb +0 -32
  210. data/lib/shrine/plugins/parallelize.rb +0 -78
  211. data/lib/shrine/plugins/parsed_json.rb +0 -29
@@ -2,30 +2,21 @@
2
2
 
3
3
  class Shrine
4
4
  module Plugins
5
- # Documentation lives in [doc/plugins/cached_attachment_data.md] on GitHub.
6
- #
7
- # [doc/plugins/cached_attachment_data.md]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/cached_attachment_data.md
5
+ # Documentation can be found on https://shrinerb.com/docs/plugins/cached_attachment_data
8
6
  module CachedAttachmentData
9
7
  module AttachmentMethods
10
- def initialize(*)
11
- super
12
-
13
- name = attachment_name
8
+ def define_model_methods(name)
9
+ super if defined?(super)
14
10
 
15
11
  define_method :"cached_#{name}_data" do
16
- send(:"#{name}_attacher").read_cached
17
- end
18
-
19
- define_method :"cached_#{name}_data=" do |value|
20
- Shrine.deprecation("Calling #cached_#{name}_data= is deprecated and will be removed in Shrine 3. You should use the original field name: `f.hidden_field :#{name}, value: record.cached_#{name}_data`.")
21
- send(:"#{name}_attacher").assign(value)
12
+ send(:"#{name}_attacher").cached_data
22
13
  end
23
14
  end
24
15
  end
25
16
 
26
17
  module AttacherMethods
27
- def read_cached
28
- get.to_json if cached? && changed?
18
+ def cached_data
19
+ file.to_json if cached? && changed?
29
20
  end
30
21
  end
31
22
  end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ class Shrine
6
+ module Plugins
7
+ # Documentation can be found on https://shrinerb.com/docs/plugins/column
8
+ module Column
9
+ def self.configure(uploader, **opts)
10
+ uploader.opts[:column] ||= { serializer: JsonSerializer }
11
+ uploader.opts[:column].merge!(opts)
12
+ end
13
+
14
+ module AttacherClassMethods
15
+ # Initializes the attacher from a data hash/string expected to come
16
+ # from a database record column.
17
+ #
18
+ # Attacher.from_column('{"id":"...","storage":"...","metadata":{...}}')
19
+ def from_column(data, **options)
20
+ attacher = new(**options)
21
+ attacher.load_column(data)
22
+ attacher
23
+ end
24
+ end
25
+
26
+ module AttacherMethods
27
+ # Column serializer object.
28
+ attr_reader :column_serializer
29
+
30
+ # Allows overriding the default column serializer.
31
+ def initialize(column_serializer: shrine_class.opts[:column][:serializer], **options)
32
+ super(**options)
33
+ @column_serializer = column_serializer
34
+ end
35
+
36
+ # Loads attachment from column data.
37
+ #
38
+ # attacher.file #=> nil
39
+ # attacher.load_column('{"id":"...","storage":"...","metadata":{...}}')
40
+ # attacher.file #=> #<Shrine::UploadedFile>
41
+ def load_column(data)
42
+ load_data(deserialize_column(data))
43
+ end
44
+
45
+ # Returns attacher data as a serialized string (JSON by default).
46
+ #
47
+ # attacher.column_data #=> '{"id":"...","storage":"...","metadata":{...}}'
48
+ def column_data
49
+ serialize_column(data)
50
+ end
51
+
52
+ private
53
+
54
+ # Converts the column data hash into a string (generates JSON by
55
+ # default).
56
+ #
57
+ # Attacher.serialize_column({ "id" => "...", "storage" => "...", "metadata" => { ... } })
58
+ # #=> '{"id":"...","storage":"...","metadata":{...}}'
59
+ #
60
+ # Attacher.serialize_column(nil)
61
+ # #=> nil
62
+ def serialize_column(data)
63
+ if column_serializer && data
64
+ column_serializer.dump(data)
65
+ else
66
+ data
67
+ end
68
+ end
69
+
70
+ # Converts the column data string into a hash (parses JSON by default).
71
+ #
72
+ # Attacher.deserialize_column('{"id":"...","storage":"...","metadata":{...}}')
73
+ # #=> { "id" => "...", "storage" => "...", "metadata" => { ... } }
74
+ #
75
+ # Attacher.deserialize_column(nil)
76
+ # #=> nil
77
+ def deserialize_column(data)
78
+ if column_serializer && data && !data.is_a?(Hash)
79
+ column_serializer.load(data)
80
+ else
81
+ data&.to_hash
82
+ end
83
+ end
84
+ end
85
+
86
+ # JSON.dump and JSON.load shouldn't be used with untrusted input, so we
87
+ # create this wrapper class which calls JSON.generate and JSON.parse
88
+ # instead.
89
+ class JsonSerializer
90
+ def self.dump(data)
91
+ JSON.generate(data)
92
+ end
93
+
94
+ def self.load(data)
95
+ JSON.parse(data)
96
+ end
97
+ end
98
+ end
99
+
100
+ register_plugin(:column, Column)
101
+ end
102
+ end
@@ -8,9 +8,7 @@ require "forwardable"
8
8
 
9
9
  class Shrine
10
10
  module Plugins
11
- # Documentation lives in [doc/plugins/data_uri.md] on GitHub.
12
- #
13
- # [doc/plugins/data_uri.md]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/data_uri.md
11
+ # Documentation can be found on https://shrinerb.com/docs/plugins/data_uri
14
12
  module DataUri
15
13
  class ParseError < Error; end
16
14
 
@@ -26,17 +24,29 @@ class Shrine
26
24
  }.inspect}"
27
25
  end
28
26
 
29
- def self.configure(uploader, opts = {})
30
- uploader.opts[:data_uri] ||= { log_subscriber: LOG_SUBSCRIBER }
31
- uploader.opts[:data_uri].merge!(opts)
27
+ def self.load_dependencies(uploader, *)
28
+ uploader.plugin :validation
29
+ end
32
30
 
33
- if uploader.opts[:data_uri][:filename]
34
- Shrine.deprecation("The :filename option is deprecated for the data_uri plugin, and will be removed in Shrine 3. Use the infer_extension plugin instead.")
35
- end
31
+ def self.configure(uploader, log_subscriber: LOG_SUBSCRIBER, **opts)
32
+ uploader.opts[:data_uri] ||= {}
33
+ uploader.opts[:data_uri].merge!(opts)
36
34
 
37
35
  # instrumentation plugin integration
38
- if uploader.respond_to?(:subscribe)
39
- uploader.subscribe(:data_uri, &uploader.opts[:data_uri][:log_subscriber])
36
+ uploader.subscribe(:data_uri, &log_subscriber) if uploader.respond_to?(:subscribe)
37
+ end
38
+
39
+ module AttachmentMethods
40
+ def define_model_methods(name)
41
+ super if defined?(super)
42
+
43
+ define_method :"#{name}_data_uri=" do |uri|
44
+ send(:"#{name}_attacher").data_uri = uri
45
+ end
46
+
47
+ define_method :"#{name}_data_uri" do
48
+ send(:"#{name}_attacher").data_uri
49
+ end
40
50
  end
41
51
  end
42
52
 
@@ -57,10 +67,10 @@ class Shrine
57
67
 
58
68
  private
59
69
 
70
+ # Creates an IO-like object from parsed data URI.
60
71
  def create_data_file(info, filename: nil)
61
72
  content_type = info[:content_type] || DEFAULT_CONTENT_TYPE
62
73
  content = info[:base64] ? Base64.decode64(info[:data]) : CGI.unescape(info[:data])
63
- filename = opts[:data_uri][:filename].call(content_type) if opts[:data_uri][:filename]
64
74
 
65
75
  data_file = Shrine::DataFile.new(content, content_type: content_type, filename: filename)
66
76
  info[:data].clear
@@ -68,6 +78,7 @@ class Shrine
68
78
  data_file
69
79
  end
70
80
 
81
+ # Parses the data URI string and returns parts.
71
82
  def parse_data_uri(uri)
72
83
  scanner = StringScanner.new(uri)
73
84
  scanner.scan(DATA_REGEXP) or raise ParseError, "data URI has invalid format"
@@ -87,20 +98,6 @@ class Shrine
87
98
  end
88
99
  end
89
100
 
90
- module AttachmentMethods
91
- def initialize(name, **options)
92
- super
93
-
94
- define_method :"#{name}_data_uri=" do |uri|
95
- send(:"#{name}_attacher").data_uri = uri
96
- end
97
-
98
- define_method :"#{name}_data_uri" do
99
- send(:"#{name}_attacher").data_uri
100
- end
101
- end
102
- end
103
-
104
101
  module AttacherMethods
105
102
  # Handles assignment of a data URI. If the regexp matches, it extracts
106
103
  # the content type, decodes it, wrappes it in a StringIO and assigns it.
@@ -110,23 +107,31 @@ class Shrine
110
107
  return if uri == "" || uri.nil?
111
108
 
112
109
  data_file = shrine_class.data_uri(uri)
113
- assign(data_file, **options)
110
+ attach_cached(data_file, **options)
114
111
  rescue ParseError => error
115
- message = shrine_class.opts[:data_uri][:error_message] || error.message
116
- message = message.call(uri) if message.respond_to?(:call)
117
- errors.replace [message]
118
- @data_uri = uri
112
+ errors.clear << data_uri_error_messsage(uri, error)
113
+ false
119
114
  end
120
115
 
121
- # Alias for #assign_data_uri.
116
+ # Used by `<name>_data_uri=` attachment method.
122
117
  def data_uri=(uri)
123
118
  assign_data_uri(uri)
119
+ @data_uri = uri
124
120
  end
125
121
 
126
- # Form builders require the reader as well.
122
+ # Used by `<name>_data_uri` attachment method.
127
123
  def data_uri
128
124
  @data_uri
129
125
  end
126
+
127
+ private
128
+
129
+ # Generates an error message for failed data URI parse.
130
+ def data_uri_error_messsage(uri, error)
131
+ message = shrine_class.opts[:data_uri][:error_message]
132
+ message = message.call(uri) if message.respond_to?(:call)
133
+ message || error.message
134
+ end
130
135
  end
131
136
 
132
137
  module FileMethods
@@ -169,7 +174,4 @@ class Shrine
169
174
  @io.string.clear # deallocate string
170
175
  end
171
176
  end
172
-
173
- Plugins::DataUri.const_set(:DataFile, DataFile)
174
- Plugins::DataUri.deprecate_constant(:DataFile)
175
177
  end
@@ -2,28 +2,58 @@
2
2
 
3
3
  class Shrine
4
4
  module Plugins
5
- # Documentation lives in [doc/plugins/default_storage.md] on GitHub.
6
- #
7
- # [doc/plugins/default_storage.md]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/default_storage.md
5
+ # Documentation can be found on https://shrinerb.com/docs/plugins/default_storage
8
6
  module DefaultStorage
9
- def self.configure(uploader, opts = {})
10
- uploader.opts[:default_storage_cache] = opts.fetch(:cache, uploader.opts[:default_storage_cache])
11
- uploader.opts[:default_storage_store] = opts.fetch(:store, uploader.opts[:default_storage_store])
7
+ def self.configure(uploader, **opts)
8
+ uploader.opts[:default_storage] ||= {}
9
+ uploader.opts[:default_storage].merge!(opts)
10
+ end
11
+
12
+ module AttacherClassMethods
13
+ def default_cache(value = nil, &block)
14
+ default_storage.merge!(cache: value || block)
15
+ end
16
+
17
+ def default_store(value = nil, &block)
18
+ default_storage.merge!(store: value || block)
19
+ end
20
+
21
+ private
22
+
23
+ def default_storage
24
+ shrine_class.opts[:default_storage]
25
+ end
12
26
  end
13
27
 
14
28
  module AttacherMethods
15
- def initialize(record, name, **options)
16
- if cache = shrine_class.opts[:default_storage_cache]
17
- cache = cache.call(record, name) if cache.respond_to?(:call)
18
- options[:cache] = cache
19
- end
29
+ def initialize(**options)
30
+ super(**shrine_class.opts[:default_storage], **options)
31
+ end
20
32
 
21
- if store = shrine_class.opts[:default_storage_store]
22
- store = store.call(record, name) if store.respond_to?(:call)
23
- options[:store] = store
33
+ def cache_key
34
+ if @cache.respond_to?(:call)
35
+ if @cache.arity == 2
36
+ Shrine.deprecation("Passing record & name argument to default storage block is deprecated and will be removed in Shrine 4. Use a block without arguments instead.")
37
+ @cache.call(record, name).to_sym
38
+ else
39
+ instance_exec(&@cache).to_sym
40
+ end
41
+ else
42
+ super
24
43
  end
44
+ end
25
45
 
26
- super
46
+ def store_key
47
+ if @store.respond_to?(:call)
48
+ if @store.arity == 2
49
+ Shrine.deprecation("Passing record & name argument to default storage block is deprecated and will be removed in Shrine 4. Use a block without arguments instead.")
50
+ @store.call(record, name).to_sym
51
+ else
52
+ instance_exec(&@store).to_sym
53
+ end
54
+ else
55
+ super
56
+ end
27
57
  end
28
58
  end
29
59
  end
@@ -2,22 +2,16 @@
2
2
 
3
3
  class Shrine
4
4
  module Plugins
5
- # Documentation lives in [doc/plugins/default_url.md] on GitHub.
6
- #
7
- # [doc/plugins/default_url.md]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/default_url.md
5
+ # Documentation can be found on https://shrinerb.com/docs/plugins/default_url
8
6
  module DefaultUrl
9
- def self.configure(uploader, opts = {}, &block)
10
- uploader.opts[:default_url_host] = opts.fetch(:host, uploader.opts[:default_url_host])
11
-
12
- if block
13
- uploader.opts[:default_url] = block
14
- Shrine.deprecation("Passing a block to default_url plugin is deprecated and will probably be removed in future versions of Shrine. Use `Attacher.default_url { ... }` instead.")
15
- end
7
+ def self.configure(uploader, **opts)
8
+ uploader.opts[:default_url] ||= {}
9
+ uploader.opts[:default_url].merge!(opts)
16
10
  end
17
11
 
18
12
  module AttacherClassMethods
19
13
  def default_url(&block)
20
- shrine_class.opts[:default_url_block] = block
14
+ shrine_class.opts[:default_url][:block] = block
21
15
  end
22
16
  end
23
17
 
@@ -29,25 +23,19 @@ class Shrine
29
23
  private
30
24
 
31
25
  def default_url(**options)
32
- url = if default_url_block
33
- instance_exec(options, &default_url_block)
34
- elsif shrine_class.opts[:default_url]
35
- shrine_class.opts[:default_url].call(context.merge(options){|k, old, new| old})
36
- end
37
-
38
- if default_url_host
39
- [default_url_host, url].join
40
- else
41
- url
42
- end
26
+ return unless default_url_block
27
+
28
+ url = instance_exec(**options, &default_url_block)
29
+
30
+ [*default_url_host, url].join
43
31
  end
44
32
 
45
33
  def default_url_block
46
- shrine_class.opts[:default_url_block]
34
+ shrine_class.opts[:default_url][:block]
47
35
  end
48
36
 
49
37
  def default_url_host
50
- shrine_class.opts[:default_url_host]
38
+ shrine_class.opts[:default_url][:host]
51
39
  end
52
40
  end
53
41
  end
@@ -1,34 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Shrine
4
- module Plugins
5
- # Documentation lives in [doc/plugins/default_url_options.md] on GitHub.
6
- #
7
- # [doc/plugins/default_url_options.md]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/default_url_options.md
8
- module DefaultUrlOptions
9
- def self.configure(uploader, options = {})
10
- uploader.opts[:default_url_options] ||= {}
11
- uploader.opts[:default_url_options].merge!(options)
12
- end
3
+ Shrine.deprecation("The default_url_options plugin has been renamed to url_options, so `plugin :default_url_options` should be replaced with `plugin :url_options`. The default_url_options alias will be removed in Shrine 4.")
13
4
 
14
- module FileMethods
15
- def url(**options)
16
- default_options = default_url_options
17
- default_options = default_options.call(self, options) if default_options.respond_to?(:call)
18
- default_options ||= {}
5
+ require "shrine/plugins/url_options"
19
6
 
20
- super(default_options.merge(options))
21
- end
22
-
23
- private
24
-
25
- def default_url_options
26
- options = shrine_class.opts[:default_url_options]
27
- options[storage_key.to_sym]
28
- end
29
- end
30
- end
31
-
32
- register_plugin(:default_url_options, DefaultUrlOptions)
33
- end
34
- end
7
+ Shrine::Plugins.register_plugin(:default_url_options, Shrine::Plugins::UrlOptions)
@@ -1,33 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ Shrine.deprecation("The delete_raw plugin is deprecated and will be removed in Shrine 4. If you were using it with versions plugin, use the new derivatives plugin instead.")
4
+
3
5
  class Shrine
4
6
  module Plugins
5
- # Documentation lives in [doc/plugins/delete_raw.md] on GitHub.
6
- #
7
- # [doc/plugins/delete_raw.md]: https://github.com/shrinerb/shrine/blob/master/doc/plugins/delete_raw.md
7
+ # Documentation can be found on https://shrinerb.com/docs/plugins/delete_raw
8
8
  module DeleteRaw
9
- def self.configure(uploader, opts = {})
10
- uploader.opts[:delete_raw_storages] = opts.fetch(:storages, uploader.opts[:delete_raw_storages])
9
+ def self.configure(uploader, **opts)
10
+ uploader.opts[:delete_raw] ||= {}
11
+ uploader.opts[:delete_raw].merge!(opts)
11
12
  end
12
13
 
13
14
  module InstanceMethods
14
15
  private
15
16
 
16
17
  # Deletes the file that was uploaded, unless it's an UploadedFile.
17
- def copy(io, context)
18
- super
19
- if io.respond_to?(:path) && io.path && delete_raw? && context[:delete] != false
20
- begin
21
- File.delete(io.path)
22
- rescue Errno::ENOENT
23
- # file might already be deleted by the moving plugin
24
- end
25
- end
18
+ def _upload(io, delete: delete_raw?, **options)
19
+ super(io, delete: delete, **options)
26
20
  end
27
21
 
28
22
  def delete_raw?
29
- opts[:delete_raw_storages].nil? ||
30
- opts[:delete_raw_storages].include?(storage_key)
23
+ opts[:delete_raw][:storages].nil? ||
24
+ opts[:delete_raw][:storages].include?(storage_key)
31
25
  end
32
26
  end
33
27
  end