Commit Graph

33 Commits

Author SHA1 Message Date
dependabot[bot]
f4203909db Bump rubocop from 1.56.4 to 1.61.0
This version fixes false negatives for Lint/SymbolConversion when using
string interpolation, for Style/RedundantArgument when using the safe
navigation operator, for Style/RedundantParentheses when logical
operators are involved and for Style/RedundantReturn with lambda ending
with return. We're applying the new rules.

Bumps [rubocop](https://github.com/rubocop/rubocop) from 1.56.4 to 1.61.0.
- [Release notes](https://github.com/rubocop/rubocop/releases)
- [Changelog](https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rubocop/rubocop/compare/v1.56.4...v1.61.0)

---
updated-dependencies:
- dependency-name: rubocop
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-04-02 16:31:10 +02:00
taitus
78ee45630b Remove metadata from images
Currently in the application we never show the original image, we always
show one of its variants.

This change removes the metadata of both the variants and the original
version so that if at some point we decide to show the original version,
we will no longer have to remember to remove the metadata.
2023-06-12 14:30:00 +02:00
Javi Martín
a72572eb64 Add image_processing gem dependency
It's now used by default to handle image variants. We were getting a
warning:

DEPRECATION WARNING: Generating image variants will require the
image_processing gem in Rails 6.1. Please add `gem 'image_processing',
'~> 1.2'` to your Gemfile.

Note `mini_magick` is required in order to use the `analyze` method [1].
Since we use it in our image (and site customization image) validations,
we're still keeping the explicit dependency in our Gemfile.

[1] https://guides.rubyonrails.org/v6.0/active_storage_overview.html#analyzing-files
2022-08-24 14:36:49 +02: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
091abfc944 Use Active Storage to render attachments
This way we fix a bug we mentioned in commit 930bb753c 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.
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
f7b83319c6 Expire cache when adding documents and images
Proposals and budget investments were not correctly updated when adding,
removing or modifying their documents and images.
2021-09-14 18:34:31 +02:00
Javi Martín
e01940c166 Move image/document attachments code to a concern
This way we reduce some of the duplication in these classes.
2021-09-11 17:05:00 +02:00
Javi Martín
b52ceb2c78 Move attachable methods from helpers to models
We were using helper methods inside the model; we might as well include
them in the model and use them from anywhere else.

Note we're using a different logic for images and documents methods.
That's because for images the logic was defined in the helper methods,
but for documents the logic is defined in the Documentable concern. In
the past, different documentable classes allowed different content
types, while imageable classes have always allowed the same content
types.

I'm not sure which method is better; for now, I'm leaving it the way it
was (except for the fact that we're removing the helper methods).
2021-09-11 17:05:00 +02:00
Javi Martín
4d8842c0d4 Remove unneeded helpers inclusion
We aren't using any methods from these helpers in these models.
2021-09-11 17:05:00 +02:00
Javi Martín
2c76f265f8 Add and apply Style/NegatedIfElseCondition rule
This rule was added in Rubocop 1.2.0, and will make developers who hate
negative conditions particularly happy.
2021-09-03 11:49:53 +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
d938077813 Remove empty lines around if and when body
Unfortunately, there doesn't seem to be a Rubocop rule for this one.
I've found these empty lines thanks to Rufo.
2019-10-24 17:56:03 +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
Julian Herrero
8e0bbf54f6 Replace harcoded images and documents settings 2019-06-04 11:50:09 +02:00
Julian Herrero
6e88031537 Fix several rubocop warnings
Metrics/LineLength: Line is too long.
RSpec/InstanceVariable: Use let instead of an instance variable.
Layout/TrailingBlankLines: Final newline missing.
Style/StringLiterals: Prefer double-quoted strings.
2019-04-17 17:40:56 +02:00
Angel Perez
01c1ac2b10 Replace all occurrences of ActiveRecord::Base with ApplicationRecord 2019-04-17 17:40:56 +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
kreopelle
14a1eeb44f Change spelling for constant to TITLE_LENGTH_RANGE, instead of TITLE_LEGHT_RANGE 2018-10-10 21:02:55 -04:00
rgarcia
a48e5f6df9 Skip image size validation for widget cards 2018-05-28 18:17:26 +02:00
Bertocq
ce0a7f6fad Rubocop autocorrections 2017-10-17 22:00:00 +02:00
Senén Rodero Rodríguez
e58feb7d6d Fix for remote filesytem paperclip configuration. 2017-09-26 13:57:14 +02:00
Senén Rodero Rodríguez
eb8ab83ac9 Remove cached files after complete uploads. 2017-09-26 13:57:13 +02:00
Senén Rodero Rodríguez
0a86727c6a Move method 'recover_image_from_cache' from imageable controllers to image. 2017-09-26 13:57:13 +02:00
Senén Rodero Rodríguez
fe7b0edb38 Adjust filename obfuscation to be able to remove cached uploads 2017-09-26 13:56:06 +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
baf9ce90a9 Obfuscate documentable and imageable file names. 2017-09-26 13:56:06 +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
Senén Rodero Rodríguez
bb57c1a7f5 Validate image dimensions only for valid attachment content types 2017-09-26 13:55:03 +02:00
Senén Rodero Rodríguez
6f71da07ee Duplicate documentable code and rename for imageable 2017-09-26 13:55:03 +02:00
Bertocq
614ff79ba1 WIP 2017-09-26 13:53:43 +02:00