shrine 3.0.0.rc → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -66
  3. data/README.md +39 -1061
  4. data/doc/advantages.md +151 -148
  5. data/doc/attacher.md +12 -30
  6. data/doc/carrierwave.md +150 -115
  7. data/doc/changing_derivatives.md +5 -11
  8. data/doc/changing_location.md +8 -5
  9. data/doc/changing_storage.md +5 -2
  10. data/doc/creating_persistence_plugins.md +9 -6
  11. data/doc/creating_plugins.md +42 -22
  12. data/doc/creating_storages.md +4 -1
  13. data/doc/design.md +7 -5
  14. data/doc/direct_s3.md +9 -4
  15. data/doc/external/articles.md +50 -0
  16. data/doc/external/extensions.md +46 -0
  17. data/doc/external/misc.md +17 -0
  18. data/doc/getting_started.md +1038 -0
  19. data/doc/metadata.md +5 -3
  20. data/doc/multiple_files.md +55 -29
  21. data/doc/paperclip.md +206 -163
  22. data/doc/plugins/activerecord.md +26 -6
  23. data/doc/plugins/add_metadata.md +4 -2
  24. data/doc/plugins/atomic_helpers.md +4 -2
  25. data/doc/plugins/backgrounding.md +83 -44
  26. data/doc/plugins/cached_attachment_data.md +4 -2
  27. data/doc/plugins/column.md +4 -2
  28. data/doc/plugins/data_uri.md +10 -6
  29. data/doc/plugins/default_storage.md +5 -3
  30. data/doc/plugins/default_url.md +4 -2
  31. data/doc/plugins/delete_raw.md +4 -2
  32. data/doc/plugins/derivation_endpoint.md +63 -39
  33. data/doc/plugins/derivatives.md +13 -50
  34. data/doc/plugins/determine_mime_type.md +6 -4
  35. data/doc/plugins/download_endpoint.md +6 -3
  36. data/doc/plugins/dynamic_storage.md +4 -2
  37. data/doc/plugins/entity.md +6 -4
  38. data/doc/plugins/form_assign.md +4 -2
  39. data/doc/plugins/included.md +4 -2
  40. data/doc/plugins/infer_extension.md +6 -4
  41. data/doc/plugins/instrumentation.md +5 -3
  42. data/doc/plugins/keep_files.md +9 -2
  43. data/doc/plugins/metadata_attributes.md +5 -3
  44. data/doc/plugins/mirroring.md +4 -2
  45. data/doc/plugins/model.md +6 -4
  46. data/doc/plugins/module_include.md +4 -2
  47. data/doc/plugins/multi_cache.md +4 -2
  48. data/doc/plugins/persistence.md +5 -3
  49. data/doc/plugins/presign_endpoint.md +6 -2
  50. data/doc/plugins/pretty_location.md +5 -3
  51. data/doc/plugins/processing.md +4 -2
  52. data/doc/plugins/rack_file.md +8 -2
  53. data/doc/plugins/rack_response.md +6 -2
  54. data/doc/plugins/recache.md +4 -2
  55. data/doc/plugins/refresh_metadata.md +5 -3
  56. data/doc/plugins/remote_url.md +26 -5
  57. data/doc/plugins/remove_attachment.md +4 -2
  58. data/doc/plugins/remove_invalid.md +10 -2
  59. data/doc/plugins/restore_cached_data.md +9 -3
  60. data/doc/plugins/sequel.md +26 -6
  61. data/doc/plugins/signature.md +6 -4
  62. data/doc/plugins/store_dimensions.md +6 -4
  63. data/doc/plugins/tempfile.md +4 -2
  64. data/doc/plugins/upload_endpoint.md +6 -2
  65. data/doc/plugins/upload_options.md +6 -4
  66. data/doc/plugins/url_options.md +4 -2
  67. data/doc/plugins/validation.md +7 -3
  68. data/doc/plugins/validation_helpers.md +13 -10
  69. data/doc/plugins/versions.md +4 -8
  70. data/doc/processing.md +27 -9
  71. data/doc/refile.md +119 -127
  72. data/doc/release_notes/1.0.0.md +4 -0
  73. data/doc/release_notes/1.1.0.md +4 -0
  74. data/doc/release_notes/1.2.0.md +4 -0
  75. data/doc/release_notes/1.3.0.md +4 -0
  76. data/doc/release_notes/1.4.0.md +4 -0
  77. data/doc/release_notes/1.4.1.md +4 -0
  78. data/doc/release_notes/1.4.2.md +4 -0
  79. data/doc/release_notes/2.0.0.md +4 -0
  80. data/doc/release_notes/2.0.1.md +4 -0
  81. data/doc/release_notes/2.1.0.md +4 -0
  82. data/doc/release_notes/2.1.1.md +4 -0
  83. data/doc/release_notes/2.10.0.md +4 -0
  84. data/doc/release_notes/2.10.1.md +4 -0
  85. data/doc/release_notes/2.11.0.md +4 -0
  86. data/doc/release_notes/2.12.0.md +4 -0
  87. data/doc/release_notes/2.13.0.md +4 -0
  88. data/doc/release_notes/2.14.0.md +5 -1
  89. data/doc/release_notes/2.15.0.md +10 -6
  90. data/doc/release_notes/2.16.0.md +4 -0
  91. data/doc/release_notes/2.17.0.md +4 -0
  92. data/doc/release_notes/2.18.0.md +4 -0
  93. data/doc/release_notes/2.19.0.md +7 -4
  94. data/doc/release_notes/2.2.0.md +4 -0
  95. data/doc/release_notes/2.3.0.md +4 -0
  96. data/doc/release_notes/2.3.1.md +4 -0
  97. data/doc/release_notes/2.4.0.md +4 -0
  98. data/doc/release_notes/2.4.1.md +4 -0
  99. data/doc/release_notes/2.5.0.md +4 -0
  100. data/doc/release_notes/2.6.0.md +4 -0
  101. data/doc/release_notes/2.6.1.md +4 -0
  102. data/doc/release_notes/2.7.0.md +4 -0
  103. data/doc/release_notes/2.8.0.md +4 -0
  104. data/doc/release_notes/2.9.0.md +4 -0
  105. data/doc/release_notes/3.0.0.md +120 -38
  106. data/doc/retrieving_uploads.md +4 -1
  107. data/doc/securing_uploads.md +4 -1
  108. data/doc/storage/file_system.md +12 -4
  109. data/doc/storage/s3.md +4 -2
  110. data/doc/testing.md +27 -41
  111. data/doc/upgrading_to_3.md +105 -26
  112. data/doc/validation.md +8 -6
  113. data/lib/shrine/attacher.rb +2 -2
  114. data/lib/shrine/attachment.rb +7 -10
  115. data/lib/shrine/plugins/activerecord.rb +10 -10
  116. data/lib/shrine/plugins/add_metadata.rb +1 -3
  117. data/lib/shrine/plugins/atomic_helpers.rb +6 -8
  118. data/lib/shrine/plugins/backgrounding.rb +4 -6
  119. data/lib/shrine/plugins/cached_attachment_data.rb +1 -3
  120. data/lib/shrine/plugins/column.rb +2 -4
  121. data/lib/shrine/plugins/data_uri.rb +1 -3
  122. data/lib/shrine/plugins/default_storage.rb +1 -3
  123. data/lib/shrine/plugins/default_url.rb +1 -3
  124. data/lib/shrine/plugins/delete_raw.rb +1 -3
  125. data/lib/shrine/plugins/derivation_endpoint.rb +3 -4
  126. data/lib/shrine/plugins/derivatives.rb +2 -4
  127. data/lib/shrine/plugins/determine_mime_type.rb +1 -3
  128. data/lib/shrine/plugins/download_endpoint.rb +1 -3
  129. data/lib/shrine/plugins/dynamic_storage.rb +1 -3
  130. data/lib/shrine/plugins/entity.rb +25 -9
  131. data/lib/shrine/plugins/form_assign.rb +1 -3
  132. data/lib/shrine/plugins/included.rb +1 -3
  133. data/lib/shrine/plugins/infer_extension.rb +1 -3
  134. data/lib/shrine/plugins/instrumentation.rb +1 -3
  135. data/lib/shrine/plugins/keep_files.rb +1 -3
  136. data/lib/shrine/plugins/metadata_attributes.rb +1 -3
  137. data/lib/shrine/plugins/mirroring.rb +2 -1
  138. data/lib/shrine/plugins/model.rb +2 -4
  139. data/lib/shrine/plugins/module_include.rb +1 -3
  140. data/lib/shrine/plugins/multi_cache.rb +3 -3
  141. data/lib/shrine/plugins/presign_endpoint.rb +1 -3
  142. data/lib/shrine/plugins/pretty_location.rb +1 -3
  143. data/lib/shrine/plugins/processing.rb +1 -3
  144. data/lib/shrine/plugins/rack_file.rb +1 -3
  145. data/lib/shrine/plugins/rack_response.rb +1 -3
  146. data/lib/shrine/plugins/recache.rb +1 -3
  147. data/lib/shrine/plugins/refresh_metadata.rb +1 -3
  148. data/lib/shrine/plugins/remote_url.rb +1 -3
  149. data/lib/shrine/plugins/remove_attachment.rb +1 -3
  150. data/lib/shrine/plugins/remove_invalid.rb +1 -3
  151. data/lib/shrine/plugins/restore_cached_data.rb +1 -3
  152. data/lib/shrine/plugins/sequel.rb +10 -12
  153. data/lib/shrine/plugins/signature.rb +1 -3
  154. data/lib/shrine/plugins/store_dimensions.rb +1 -3
  155. data/lib/shrine/plugins/tempfile.rb +1 -3
  156. data/lib/shrine/plugins/upload_endpoint.rb +1 -3
  157. data/lib/shrine/plugins/upload_options.rb +1 -3
  158. data/lib/shrine/plugins/url_options.rb +1 -3
  159. data/lib/shrine/plugins/validation.rb +1 -3
  160. data/lib/shrine/plugins/validation_helpers.rb +1 -3
  161. data/lib/shrine/plugins/versions.rb +1 -3
  162. data/lib/shrine/storage/file_system.rb +1 -1
  163. data/lib/shrine/storage/linter.rb +1 -1
  164. data/lib/shrine/storage/memory.rb +2 -1
  165. data/lib/shrine/storage/s3.rb +3 -3
  166. data/lib/shrine/version.rb +1 -1
  167. metadata +8 -4
@@ -1,4 +1,7 @@
1
- # Retrieving Uploads
1
+ ---
2
+ id: retrieving-uploads
3
+ title: Retrieving Uploads
4
+ ---
2
5
 
3
6
  Uploaded file content is typically retrieved from the storage using a
4
7
  `Shrine::UploadedFile` object. This guide explains the various methods of
@@ -1,4 +1,7 @@
1
- # Securing uploads
1
+ ---
2
+ id: securing-uploads
3
+ title: Securing uploads
4
+ ---
2
5
 
3
6
  Shrine does a lot to make your file uploads secure, but there are still a lot
4
7
  of security measures that could be added by the user on the application's side.
@@ -1,4 +1,7 @@
1
- # Shrine::Storage::FileSystem
1
+ ---
2
+ id: file-system
3
+ title: Shrine::Storage::FileSystem
4
+ ---
2
5
 
3
6
  The FileSystem storage handles uploads to the filesystem, and it is most
4
7
  commonly initialized with a "base" folder and a "prefix":
@@ -64,7 +67,12 @@ File.exist?(file.path) #=> false
64
67
  ```
65
68
 
66
69
  If you want to make this option default, you can use the
67
- [`upload_options`][upload_options] plugin.
70
+ [`upload_options`][upload_options] plugin, provided that both `:cache` and
71
+ `:store` storages are `FileSystem`):
72
+
73
+ ```rb
74
+ plugin :upload_options, cache: { move: true }, store: { move: true }
75
+ ```
68
76
 
69
77
  ## Path
70
78
 
@@ -80,7 +88,7 @@ If you want to delete all files in some directory, you can use
80
88
  `FileSystem#delete_prefixed`:
81
89
 
82
90
  ```rb
83
- storage.delete_prefixed("some_directory") # deletes all files in "some_directory/"
91
+ storage.delete_prefixed("some_directory/") # deletes all files in "some_directory/"
84
92
  ```
85
93
 
86
94
  ## Clearing cache
@@ -120,4 +128,4 @@ also means that deploying the app can cancel someone's uploading if you're
120
128
  using backgrounding. Also, by default you cannot generate URLs to files in the
121
129
  "tmp" directory, but you can with the `download_endpoint` plugin.
122
130
 
123
- [upload_options]: /doc/plugins/upload_options.md#readme
131
+ [upload_options]: https://shrinerb.com/docs/plugins/upload_options
@@ -1,4 +1,6 @@
1
- # Shrine::Storage::S3
1
+ ---
2
+ title: Shrine::Storage::S3
3
+ ---
2
4
 
3
5
  The S3 storage handles uploads to Amazon S3 service, using the [aws-sdk-s3]
4
6
  gem:
@@ -265,7 +267,7 @@ If you want to delete all objects in some prefix, you can use
265
267
  `S3#delete_prefixed`:
266
268
 
267
269
  ```rb
268
- s3.delete_prefixed("some_prefix") # deletes all objects in "some_prefix/"
270
+ s3.delete_prefixed("some_prefix/") # deletes all objects in "some_prefix/"
269
271
  ```
270
272
 
271
273
  ## Clearing cache
@@ -1,4 +1,6 @@
1
- # Testing with Shrine
1
+ ---
2
+ title: Testing with Shrine
3
+ ---
2
4
 
3
5
  The goal of this guide is to provide some useful tips for testing file
4
6
  attachments implemented with Shrine in your application.
@@ -99,7 +101,8 @@ We want to keep our tests fast, so when we're setting up files for tests, we
99
101
  want to avoid expensive operations such as file processing and metadata
100
102
  extraction.
101
103
 
102
- We can start by creating a method which would generate fake attachment data:
104
+ We can create a helper method that will create attached file data for us, and
105
+ use that with our factories/fixtures.
103
106
 
104
107
  ```rb
105
108
  module TestData
@@ -134,24 +137,19 @@ module TestData
134
137
  end
135
138
  end
136
139
  ```
137
- ```rb
138
- TestData.image_data #=> '{"id":"...","storage":"...","metadata":{...},"derivatives":{...}}'
139
- ```
140
-
141
- With [factory_bot] you can then assign the test attachment data like this:
142
-
140
+ <!--DOCUSAURUS_CODE_TABS-->
141
+ <!--FactoryBot-->
143
142
  ```rb
144
143
  factory :photo do
145
144
  image_data { TestData.image_data }
146
145
  end
147
146
  ```
148
-
149
- With [Rails' YAML fixtures][fixtures] it would look like this:
150
-
147
+ <!--Rails YAML fixtures-->
151
148
  ```erb
152
149
  photo:
153
150
  image_data: <%= TestData.image_data %>
154
151
  ```
152
+ <!--END_DOCUSAURUS_CODE_TABS-->
155
153
 
156
154
  ## Unit tests
157
155
 
@@ -182,35 +180,26 @@ end
182
180
  ## Acceptance tests
183
181
 
184
182
  In acceptance tests you're testing your app end-to-end, and you likely want to
185
- also test file attachments here. There are a variety of libraries that you
186
- might be using for your acceptance tests.
187
-
188
- ### Capybara
189
-
190
- If you're testing with the [Capybara] acceptance test framework, you can use
191
- [`#attach_file`] to select a file from your filesystem in the form:
183
+ also test file attachments here. Here are examples for some common use cases:
192
184
 
185
+ <!--DOCUSAURUS_CODE_TABS-->
186
+ <!--Capybara-->
193
187
  ```rb
194
188
  attach_file("#image-field", "test/files/image.jpg")
195
189
  ```
196
-
197
- ### Rack::Test
198
-
199
- Regular routing tests in Rails use [Rack::Test], in which case you can create
200
- `Rack::Test::UploadedFile` objects and pass them as form parameters:
201
-
190
+ <!--Rack::Test-->
202
191
  ```rb
203
- post "/photos", photo: { image: Rack::Test::UploadedFile.new("test/files/image.jpg", "image/jpeg") }
192
+ post "/photos", photo: {
193
+ image: Rack::Test::UploadedFile.new("test/files/image.jpg", "image/jpeg")
194
+ }
204
195
  ```
205
-
206
- ### Rack::TestApp
207
-
208
- With [Rack::TestApp] you can create multipart file upload requests by using the
209
- `:multipart` option and passing a `File` object:
210
-
196
+ <!--Rack::TestApp-->
211
197
  ```rb
212
- http.post "/photos", multipart: {"photo[image]" => File.open("test/files/image.jpg")}
198
+ app.post "/photos", multipart: {
199
+ "photo[image]" => File.open("test/files/image.jpg")
200
+ }
213
201
  ```
202
+ <!--END_DOCUSAURUS_CODE_TABS-->
214
203
 
215
204
  ## Background jobs
216
205
 
@@ -218,19 +207,21 @@ If you're using background jobs with Shrine, you probably want to make them
218
207
  synchronous in tests. See your backgrounding library docs for how to make jobs
219
208
  synchronous.
220
209
 
210
+ <!--DOCUSAURUS_CODE_TABS-->
211
+ <!--ActiveJob-->
221
212
  ```rb
222
- # ActiveJob
223
213
  ActiveJob::Base.queue_adapter = :inline
224
214
  ```
215
+ <!--Sidekiq-->
225
216
  ```rb
226
- # Sidekiq
227
217
  require "sidekiq/testing"
228
218
  Sidekiq::Testing.inline!
229
219
  ```
220
+ <!--SuckerPunch-->
230
221
  ```rb
231
- # SuckerPunch
232
222
  require "sucker_punch/testing/inline"
233
223
  ```
224
+ <!--END_DOCUSAURUS_CODE_TABS-->
234
225
 
235
226
  ## Processing
236
227
 
@@ -261,12 +252,7 @@ end
261
252
  ```
262
253
 
263
254
  [DatabaseCleaner]: https://github.com/DatabaseCleaner/database_cleaner
264
- [factory_bot]: https://github.com/thoughtbot/factory_bot
265
- [fixtures]: https://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures
266
- [Capybara]: https://github.com/jnicklas/capybara
267
255
  [`#attach_file`]: http://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions#attach_file-instance_method
268
- [Rack::Test]: https://github.com/brynary/rack-test
269
- [Rack::TestApp]: https://github.com/kwatch/rack-test_app
270
256
  [aws-sdk-ruby stubs]: http://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/ClientStubs.html
271
257
  [MinIO]: https://min.io/
272
- [derivatives]: /doc/plugins/derivatives.md#readme
258
+ [derivatives]: https://shrinerb.com/docs/plugins/derivatives
@@ -1,17 +1,10 @@
1
- # Upgrading to Shrine 3.x
1
+ ---
2
+ id: upgrading-to-3
3
+ title: Upgrading to Shrine 3.x
4
+ ---
2
5
 
3
6
  This guide provides instructions for upgrading Shrine in your apps to version
4
- 3.x. We will cover the following areas:
5
-
6
- * [Attacher](#attacher)
7
- * [Backgrounding](#backgrounding)
8
- * [Versions](#versions)
9
- * [Miscellaneous](#miscellaneous)
10
- - [Logging](#logging)
11
- - [Backup](#backup)
12
- - [Copy](#copy)
13
- - [Moving](#moving)
14
- - [Memory](#memory)
7
+ 3.x. If you're looking for a full list of changes, see the [3.0 release notes].
15
8
 
16
9
  ## Attacher
17
10
 
@@ -25,7 +18,7 @@ attacher without a model:
25
18
 
26
19
  ```rb
27
20
  attacher = Shrine::Attacher.new
28
- # ...
21
+ #=> #<Shrine::Attacher>
29
22
 
30
23
  attacher = Shrine::Attacher.new(photo, :image)
31
24
  # ~> ArgumentError: invalid number of arguments
@@ -337,6 +330,22 @@ else
337
330
  end
338
331
  ```
339
332
 
333
+ If you have multiple places where you need to generate derivatives, and want it
334
+ to happen automatically like it did with the `versions` plugin, you can
335
+ override `Attacher#promote` to call `Attacher#create_derivatives` before
336
+ promotion:
337
+
338
+ ```rb
339
+ class Shrine::Attacher
340
+ def promote(*)
341
+ create_derivatives
342
+ super
343
+ end
344
+ end
345
+ ```
346
+
347
+ ### Accessing derivatives
348
+
340
349
  The derivative URLs are accessed in the same way as versions:
341
350
 
342
351
  ```rb
@@ -495,12 +504,43 @@ end
495
504
  However, it doesn't implement any other URL fallbacks that the `versions`
496
505
  plugin has for missing derivatives.
497
506
 
498
- ## Miscellaneous
507
+ ## Other
508
+
509
+ ### Processing
510
+
511
+ The `processing` plugin has been deprecated over the new
512
+ [`derivatives`][derivatives] plugin. If you were modifying the original file:
513
+
514
+ ```rb
515
+ class MyUploader < Shrine
516
+ plugin :processing
517
+
518
+ process(:store) do |io, context|
519
+ ImageProcessing::MiniMagick
520
+ .source(io.download)
521
+ .resize_to_limit!(1600, 1600)
522
+ end
523
+ end
524
+ ```
525
+
526
+ you should now add the processed file as a derivative:
527
+
528
+ ```rb
529
+ class MyUploader < Shrine
530
+ plugin :derivatives
531
+
532
+ Attacher.derivatives_processor do |original|
533
+ magick = ImageProcessing::MiniMagick.source(original)
534
+
535
+ { normalized: magick.resize_to_limit!(1600, 1600) }
536
+ end
537
+ end
538
+ ```
499
539
 
500
540
  ### Logging
501
541
 
502
542
  The `logging` plugin has been removed in favour of the
503
- [`instrumentation`][instrumentation] plugin. You can replace
543
+ [`instrumentation`][instrumentation] plugin. You can replace code like
504
544
 
505
545
  ```rb
506
546
  Shrine.plugin :logging, logger: Rails.logger
@@ -517,7 +557,7 @@ Shrine.plugin :instrumentation
517
557
  ### Backup
518
558
 
519
559
  The `backup` plugin has been removed in favour of the new
520
- [`mirroring`][mirroring] plugin. You can replace
560
+ [`mirroring`][mirroring] plugin. You can replace code like
521
561
 
522
562
  ```rb
523
563
  Shrine.plugin :backup, storage: :backup_store
@@ -531,7 +571,8 @@ Shrine.plugin :mirroring, mirror: { store: :backup_store }
531
571
 
532
572
  ### Copy
533
573
 
534
- The `copy` plugin has been removed. You can replace
574
+ The `copy` plugin has been removed as its behaviour can now be achieved easily.
575
+ You can replace code like
535
576
 
536
577
  ```rb
537
578
  Shrine.plugin :copy
@@ -550,16 +591,54 @@ attacher.add_derivatives other_attacher.derivatives # if using derivatives
550
591
  ### Moving
551
592
 
552
593
  The `moving` plugin has been removed in favour of the `:move` option for
553
- `FileSystem#upload`. You can set up moving in basic places with the
554
- `upload_options` plugin:
594
+ `FileSystem#upload`. You can set this option as default using the
595
+ `upload_options` plugin (the example assumes both `:cache` and `:store` are
596
+ FileSystem storages):
597
+
598
+ ```rb
599
+ Shrine.plugin :upload_options, cache: { move: true }, store: { move: true }
600
+ ```
601
+
602
+ ### Parsed JSON
603
+
604
+ The `parsed_json` plugin has been removed as it's now the default behaviour.
605
+
606
+ ```rb
607
+ # this now works by default
608
+ photo.image = { "id" => "d7e54d6ef2.jpg", "storage" => "cache", "metadata" => { ... } }
609
+ ```
610
+
611
+ ### Module Include
612
+
613
+ The `module_include` plugin has been deprecated over overriding core classes
614
+ directly. You can replace code like
615
+
616
+ ```rb
617
+ class MyUploader < Shrine
618
+ plugin :module_include
619
+
620
+ file_methods do
621
+ def image?
622
+ mime_type.start_with?("image")
623
+ end
624
+ end
625
+ end
626
+ ```
627
+
628
+ with
555
629
 
556
630
  ```rb
557
- Shrine.plugin :upload_options,
558
- cache: -> (io, action: nil, **) { { move: true } if action == :cache },
559
- store: -> (io, action: nil, **) { { move: true } if action == :store }
631
+ class MyUploader < Shrine
632
+ class UploadedFile
633
+ def image?
634
+ mime_type.start_with?("image")
635
+ end
636
+ end
637
+ end
560
638
  ```
561
639
 
562
- [model]: /doc/plugins/model.md#readme
563
- [derivatives]: /doc/plugins/derivatives.md#readme
564
- [instrumentation]: /doc/plugins/instrumentation.md#readme
565
- [mirroring]: /doc/plugins/mirroring.md#readme
640
+ [3.0 release notes]: https://shrinerb.com/docs/release_notes/3.0.0
641
+ [model]: https://shrinerb.com/docs/plugins/model
642
+ [derivatives]: https://shrinerb.com/docs/plugins/derivatives
643
+ [instrumentation]: https://shrinerb.com/docs/plugins/instrumentation
644
+ [mirroring]: https://shrinerb.com/docs/plugins/mirroring
@@ -1,4 +1,6 @@
1
- # File Validation
1
+ ---
2
+ title: File Validation
3
+ ---
2
4
 
3
5
  Shrine allows validating assigned files using the [`validation`][validation]
4
6
  plugin. Validation code is defined inside an `Attacher.validate` block:
@@ -15,9 +17,9 @@ end
15
17
  ```
16
18
 
17
19
  The validation block is run when a new file is assigned, and any validation
18
- errors are stored in `Shrine::Attacher#errors`. ORM plugins like `sequel` and
19
- `activerecord` will automatically merge these validation errors into the
20
- `#errors` hash on the model instance.
20
+ errors are stored in `Shrine::Attacher#errors`. Persistence plugins like
21
+ `sequel` and `activerecord` will automatically merge these validation errors
22
+ into the `#errors` hash on the model instance.
21
23
 
22
24
  ```rb
23
25
  photo = Photo.new
@@ -105,5 +107,5 @@ you can have it automatically removed and deleted by loading the
105
107
  Shrine.plugin :remove_invalid # remove and delete files that failed validation
106
108
  ```
107
109
 
108
- [validation]: /doc/plugins/validation.md#readme
109
- [validation_helpers]: /doc/plugins/validation_helpers.md#readme
110
+ [validation]: https://shrinerb.com/docs/plugins/validation
111
+ [validation_helpers]: https://shrinerb.com/docs/plugins/validation_helpers
@@ -154,7 +154,7 @@ class Shrine
154
154
  # attacher.promote_cached
155
155
  # attacher.stored? #=> true
156
156
  def promote_cached(**options)
157
- promote(action: :store, **options) if promote?
157
+ promote(**options) if promote?
158
158
  end
159
159
 
160
160
  # Uploads current file to permanent storage and sets the stored file.
@@ -163,7 +163,7 @@ class Shrine
163
163
  # attacher.promote
164
164
  # attacher.stored? #=> true
165
165
  def promote(storage: store_key, **options)
166
- set upload(file, storage, **options)
166
+ set upload(file, storage, action: :store, **options)
167
167
  end
168
168
 
169
169
  # Delegates to `Shrine.upload`, passing the #context.
@@ -18,6 +18,13 @@ class Shrine
18
18
  def inspect
19
19
  "#{shrine_class.inspect}::Attachment"
20
20
  end
21
+
22
+ # Shorthand for `Attachment.new`.
23
+ #
24
+ # Shrine::Attachment[:image]
25
+ def [](*args)
26
+ new(*args)
27
+ end
21
28
  end
22
29
 
23
30
  module InstanceMethods
@@ -29,16 +36,6 @@ class Shrine
29
36
  @options = options
30
37
  end
31
38
 
32
- def included(klass)
33
- super
34
-
35
- attachment = self
36
-
37
- klass.send(:define_singleton_method, :"#{@name}_attacher") do |**options|
38
- attachment.shrine_class::Attacher.new(**attachment.options, **options)
39
- end
40
- end
41
-
42
39
  # Returns name of the attachment this module provides.
43
40
  def attachment_name
44
41
  @name