-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove Paperclip and use just Active Storage #4600
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1007e76
to
fbcc669
Compare
479bebf
to
09c08f8
Compare
f4f7b75
to
b1b0766
Compare
445b713
to
feebd88
Compare
d4894b0
to
03a086c
Compare
03a086c
to
a48f3e8
Compare
Senen
approved these changes
Feb 23, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good to me!
I did the following checks for testing these changes on a server running the production environment:
- Reproduce all the errors mentioned in the objectives section in the current master branch
- Deploy the branch
active_storage
- Run the rake task to migrate attachments from paperclip to active_storage
- Check, one by one, all the errors mentioned in the objectives section are solved.
- Check the file_validators validations by uploading wrong sized and too heavy images and documents
- Check the ActiveStorage integration with AWS S3 works fine (including the Ckeditor specific integration)
a48f3e8
to
b3e3333
Compare
For now, we'll merge it with the |
This way we fix a bug we mentioned in commit 930bb75 which caused links to documents to be broken when editing their title because the title was used to generate the URL of the document. Note we're still using Paperclip to render cached attachments because this is the only case where we store files with just Paperclip and not Active Storage. With Active Storage, we render attachments just like any other resource, using `polymorphic_path`. Paperclip included the `url` method in the model; since the model doesn't have access to the request parameters (like the host), this was inconvenient because it wasn't possible to generate absolute URLs with Paperclip. In order to simplify the code and make it similar to the way we used Paperclip, we're adding a `variant` method accepting the name of a variant and returning the variant.
This fixes a few issues we've had for years. First, when attaching an image and then sending a form with validation errors, the image preview would not be rendered when the form was displayed once again. Now it's rendered as expected. Second, when attaching an image, removing it, and attaching a new one, browsers were displaying the image preview of the first one. That's because Paperclip generated the same URL from both files (as they both had the same hash data and prefix). Browsers usually cache images and render the cached image when getting the same URL. Since now we're storing each image in a different Blob, the images have different URLs and so the preview of the second one is correctly displayed. Finally, when users downloaded a document, they were getting files with a very long hexadecimal hash as filename. Now they get the original filename.
Just like we did with regular attachments, we're moving the logic to generate URLs out of the model. Note we're changing the `image_path_for` helper method in order to return a `polymorphic_path` because sometimes it's used in combination with `favicon_link_tag`, and `favicon_link_tag` doesn't automatically generate a polymorphic URL when given an `ActiveStorage::Attachment` record.
The same way we're handling images.
Since we're going to remove Paperclip and Active Storage doesn't provide any validations, we have to either write our own validation rules or use a different gem. We're using the file_validators gem instead of the `active_storage_validations` gem because the latter doesn't support proc/lambda objects in size and content type definitions. We need to use them because in our case these values depend on settings stored in the database.
The code is based on what's generated using CKEditor's code generator. We're doing one minor change to the `Ckeditor::Backend::ActiveStorage` module; we're assigning the data in a `before_validation` instead of a `before_save` callback. Validations with `file_validations` didn't work otherwise; it looks like this backend was written with `active_storage_validations` in mind [1]. Note we don't need to update the `name` column in the attachments table because, when using Active Storage, CKEditor uses both `data` (as attribute accessor) and `storage_data` (as attachment attribute). [1] https://github.com/galetahub/ckeditor/blob/f9e48420ccb6dc/lib/generators/ckeditor/templates/active_record/active_storage/ckeditor/picture.rb#L4
We were getting an error when browsing the server if one file had been deleted.
af1ff53
to
303442c
Compare
We were using custom rules because of some issues with Paperclip. These rules work fine, but since we're already using the file_validators gem, we might as well simplify the code a little bit.
This way we don't have to write `"spec/fixtures/files"` every time. Note this method isn't included in factories. We could include it like so: ``` FactoryBot::SyntaxRunner.class_eval do include ActiveSupport::Testing::FileFixtures self.file_fixture_path = RSpec.configuration.file_fixture_path end ``` However, I'm not sure about the possible side effects, and since we only use attachments in a few factories, there isn't much gain in applying the monkey-patch.
303442c
to
979766f
Compare
We were using this hack in order to allow `File.new` attachments in tests files. However, we can use the `fixture_file_upload` helper instead. Just like it happened with `file_fixture`, this helper method doesn't work in fixtures, so in this case we're using `Rack::Test::UploadedFile` instead.
979766f
to
8eea6f5
Compare
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
References
active_storage_validations
instead offile_validators
Objectives
Release Notes
TODO Explain we've removed Paperclip but there might be references to paperclip attachments in any existing content (for example, custom pages) and so Paperclip attachments shouldn't be removed (or removed with care).
TODO Strongly emphasize that after upgrading it'll be necessary to execute the release tasks; othewise there'll be 500 errors on every page displaying an attached image or document.