Commit Graph

19 Commits

Author SHA1 Message Date
Javi Martín
efc46fe6c8 Add Performance/StringIdentifierArgument rule
It was added in rubocop-performance 1.13.0. We were already applying it
in most places.

We aren't adding it for performance reasons but in order to make the
code more consistent.
2023-01-11 16:05:20 +01:00
Javi Martín
ffc14e499a Upgrade to Rails 6.0
All the code in the `bin/` and the `config/` folders has been generated
running `rake app:update`. The only exception is the code in
`config/application.rb` where we've excluded the engines that Rails 6.0
has added, since we don't use them.

There are a few changes in Active Storage which aren't compatible with
the code we were using until now.

Since the method to assign an attachment in ActiveStorage has changed
and is incompatible with the hack we used to allow assigning `nil`
attachments, and since ActiveStorage now supports assigning `nil`
attachments, we're removing the mentioned hack. This makes the
HasAttachment module redundant, so we're removing it.

Another change in ActiveStorage is files are no longer saved before
saving the `ActiveStorage::Attachment` record. This means we need to
manually upload the file when using direct uploads. We also have to
change the width and height validations we used for images; however,
doing so results in very complex code, and we currently have to write
that code for both images and site customization images.

So, for now, we're just uploading the file before checking its
dimensions. Not ideal, though. We might use active_storage_validations
in the future to fix this issue (when they support a proc/lambda, as
mentioned in commit 600f5c35e).

We also need to update a couple of tests due to a small change in
response headers. Now the content disposition returns something like:

```
attachment; filename="budget_investments.csv"; filename*=UTF-8''budget_investments.csv
```

So we're updating regular expression we use to check the filename.

Finally, Rails 6.0.1 changed the way the host is set in integration
tests [1] and so both `Capybara.app_host` and `Capybara.default_host`
were ignored when generating URLs in the relationable examples. The only
way I've found to make it work is to explicitely assign the host to the
integration session. Rails 6.1 will change this setup again, so maybe
then we can remove this hack.

[1] https://github.com/rails/rails/pull/36283/commits/fe00711e9
2022-08-24 14:33:02 +02:00
Javi Martín
7212657c02 Remove Paperclip and use just Active Storage 2022-02-23 18:43:48 +01:00
Javi Martín
e0e35298d5 Use Active Storage to handle cached attachments
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.
2022-02-23 18:21:38 +01:00
Javi Martín
1290e2ecd3 Store files with both Paperclip and ActiveStorage
In order to migrate existing files from Paperclip to ActiveStorage, we
need Paperclip to find out the files associated to existing database
records. So we can't simply replace Paperclip with ActiveStorage.

That's why it's usually recommended [1] to first run the migration and
then replace Paperclip with ActiveStorage using two consecutive
deployments.

However, in our case we can't rely on two consecutive deployments
because we have to make an easy process so existing CONSUL installations
don't run into any issues. We can't just release version 1.4.0 and 1.5.0
and day and ask everyone to upgrade twice on the same day.

Instead, we're following a different plan:

* We're going to provide a Rake task (which will require Paperclip) to
  migrate existing files
* We still use Paperclip to generate link and image tags
* New files are handled using both Paperclip and ActiveStorage; that
  way, when we make the switch, we won't have to migrate them, and in
  the meantime they'll be accessible thanks to Paperclip
* After we make the switch, we'll update the `name` column in the active
  storage attachments tables in order to remove the `storage_` prefix

Regarding our handling of new files, the exception are cached
attachments. Since those attachments are temporary files used while
submitting a form and we have to delete them afterwards, we're only
handling them with Paperclip. We'll handle these ones in version 1.5.0.

Note the task creating the dev seeds was failing after these changes
with an `ActiveStorage::IntegrityError` exception because we were
opening some files without closing them. If the same file was attached
twice, it failed the second time.

We're solving it by closing the files with `File.open` and a block. Even
though we didn't get any errors, we're doing the same thing in the
`Attachable` concern because it's a good practice to close files after
we're done with them.

Also note we have to change the CKEditor Active Storage code so it's
compatible with Paperclip. In this case, I haven't been able to write a
test to confirm the attachment exists; I was getting the same
`ActiveStorage::IntegrityError` mentioned above.

Finally, we're updating the site customization image controller to use
`update` so the image and the attachment are updated within the same
transaction. This is also what we do in most controllers.

[1] https://www.youtube.com/watch?v=tZ_WNUytO9o
2021-09-24 13:39:15 +02:00
Javi Martín
930bb753c5 Use a rake task to delete cached attachments
Our previous system to delete cached attachments didn't work for
documents because the `custom_hash_data` is different for files created
from a file and files created from cached attachments.

When creating a document attachment, the name of the file is taken into
account to calculate the hash. Let's say the original file name is
"logo.pdf", and the generated hash is "123456". The cached attachment
will be "123456.pdf", so the generated hash using the cached attachment
will be something different, like "28af3". So the file that will be
removed will be "28af3.pdf", and not "123456.pdf", which will still be
present.

Furthermore, there are times where users choose a file and then they
close the browser or go to a different page. In those cases, we weren't
deleting the cached attachments either.

So we're adding a rake task to delete these files once a day. This way
we can simplify the logic we were using to destroy cached attachments.

Note there's related a bug in documents: when editing a record (for
example, a proposal), if the title of the document changes, its hash
changes, and so it will be impossible to generate a link to that
document. Changing the way this hash is generated is not an option
because it would break links to existing files. We'll try to fix it when
moving to Active Storage.
2021-07-30 17:30:11 +02:00
Javi Martín
db97f9d08c Add and apply rubocop rules for empty lines
We were very inconsistent regarding these rules.

Personally I prefer no empty lines around blocks, clases, etc... as
recommended by the Ruby style guide [1], and they're the default values
in rubocop, so those are the settings I'm applying.

The exception is the `private` access modifier, since we were leaving
empty lines around it most of the time. That's the default rubocop rule
as well. Personally I don't have a strong preference about this one.


[1] https://rubystyle.guide/#empty-lines-around-bodies
2019-10-24 17:11:47 +02:00
Javi Martín
47b2c42a1d Apply IndentationConsistency rubocop rule 2019-09-10 20:02:15 +02:00
rgarcia
b2b418284d Fix ActiveModel::Errors#[]= deprecation warning
DEPRECATION WARNING: ActiveModel::Errors#[]= is deprecated and will be
removed in Rails 5.1. Use model.errors.add(:attachment, "content type
image/png does not match any of accepted content types pdf") instead.
(called from validate_attachment_content_type at
/home/travis/build/consul/consul/app/models/document.rb:92)
2019-04-17 17:40:55 +02:00
Bertocq
ce9d5ec022 Cleanup bad indentation on direct upload model 2018-01-03 17:38:44 +01:00
Bertocq
ce0a7f6fad Rubocop autocorrections 2017-10-17 22:00:00 +02:00
rgarcia
23682fadd8 fixes document specs 2017-10-06 17:50:41 +02:00
rgarcia
9a31c97a20 fixes image specs 2017-10-06 14:01:03 +02:00
rgarcia
a15d1a4020 resolves conflict when uploading pdfs to answers 2017-10-06 02:52:06 +02:00
rgarcia
06a1b96fcd clean up 2017-10-04 23:56:23 +02:00
rgarcia
1cf9bc4ad1 adds images to anwers 2017-10-04 23:56:09 +02:00
Senén Rodero Rodríguez
661bdda41f Stronger direct upload model specs 2017-09-26 13:56:06 +02:00
Senén Rodero Rodríguez
826385f659 Extract ajax upload and destroy_upload actions from images and documents controllers and place them at new controllers to manage direct uploads 2017-09-26 13:55:28 +02:00
Senén Rodero Rodríguez
c6dabedb4a Add missing image model spec. Add shared specs to check image validations at any imageable model 2017-09-26 13:55:03 +02:00