Commit Graph

20065 Commits

Author SHA1 Message Date
Javi Martín
62aad851bf Use icons as links to edit content blocks
Just like we do with the rest of the tables in the admin section.
2024-04-17 16:59:14 +02:00
Javi Martín
6a2ee921de Ask confirmation to delete content blocks from the edit page
We were already doing that when deleting content blocks from the index
page, and we also ask for confirmation in almost every page in the admin
section.
2024-04-17 16:44:10 +02:00
Javi Martín
5a7021396e Use a button to destroy content blocks from the edit page
We were already using button to destroy content blocks from the content
blocks index.

As mentioned in commits 5311daadf and bb958daf0, using links combined
with JavaScript to generate POST (or, in this case, DELETE) requests to
the server has a few issues.
2024-04-17 16:44:10 +02:00
Javi Martín
d050c04bb0 Use a button to destroy poll question answer images
As mentioned in commits 5311daadf and bb958daf0, using links combined
with JavaScript to generate POST (or, in this case, DELETE) requests to
the server has a few issues.

Note that the AJAX response stopped working after replacing the link
with a button. Not sure about the reason, but, since this is one of the
very few places where we use AJAX calls to delete content, the easiest
solution is to stop using AJAX and be consistent with what we do in the
rest of the admin section.
2024-04-17 16:44:10 +02:00
Javi Martín
53d85d6431 Use a button to destroy officials
As mentioned in commits 5311daadf and bb958daf0, using links combined
with JavaScript to generate POST (or, in this case, DELETE) requests to
the server has a few issues.
2024-04-17 16:44:10 +02:00
Javi Martín
5876738369 Ask confirmation to delete drafts and questions
This is similar to what we do in almost every other page of the admin
section.
2024-04-17 16:44:10 +02:00
Javi Martín
ecad046a99 Use buttons to destroy drafts and questions
As mentioned in commits 5311daadf and bb958daf0, using links combined
with JavaScript to generate POST (or, in this case, DELETE) requests to
the server has a few issues.
2024-04-17 16:44:09 +02:00
Javi Martín
20d3725709 Add missing test to delete a draft version
We weren't testing this action anywhere.
2024-04-17 16:44:09 +02:00
Javi Martín
cb3bea8eec Simplify code to ask for send newsletter confirmation
Using the standard `confirm` parameter, we can remove all the custom
code we added to do the same thing.

Since the code is similar, we're doing the same when asking for
confirmation to send notifications.
2024-04-17 16:44:09 +02:00
Javi Martín
52ec55970b Use buttons to send notifications and newsletters
As mentioned in commits 5311daadf and bb958daf0, using links combined
with JavaScript to generate POST requests to the server has a few
issues.
2024-04-17 16:44:09 +02:00
Javi Martín
09321f41a7 Merge pull request #5492 from consuldemocracy/move_tenant_service_to_load_path
Move custom ActiveStorage service to $LOAD_PATH
2024-04-17 16:29:20 +02:00
Javi Martín
118c2bf5e0 Move custom ActiveStorage service to $LOAD_PATH
We moved this file to `app/lib/` in commit cb477149c so it would be in
the autoload_paths. However, this class is loaded by ActiveStorage, with
the following method:

```
def resolve(class_name)
  require "active_storage/service/#{class_name.to_s.underscore}_service"
  ActiveStorage::Service.const_get(:"#{class_name.camelize}Service")
rescue LoadError
  raise "Missing service adapter for #{class_name.inspect}"
end
``

So this file needs to be in the $LOAD_PATH, or else ActiveStorage won't
be able to load it when we disable the `add_autoload_paths_to_load_path`
option, which is the default in Rails 7.1 [1].

Moving it to the `lib` folder solves the issue; as mentioned in the
guide to upgrade to Rails 7.1 [2]:

> The lib directory is not affected by this flag, it is added to
> $LOAD_PATH always.

However, we were also referencing this class in the `Tenant` model,
meaning we needed to autoload it as well somehow. So, instead of
directly referencing this class, we're using `respond_to?` in the Tenant
model.

We're changing the test so it fails when the code calls
`is_a?(ActiveStorage::Service::TenantDiskService)`. We need to change
the active storage configurations in the test because, otherwise, the
moment `ActiveStorage::Blob` is loaded, the `TenantDiskService` class is
also loaded, meaning the test will pass when using `is_a?`.

Note that, since this class isn't in the autoload paths anymore, we need
to add a `require` in the tests. We could add an initializer to require
it; we're not doing it in order to be consistent with what ActiveStorage
does: it only loads the service that's going to be used in the current
Rails environment. If somebody changed their production environment in
order to use (for example), S3, and we added an initializer to require
the TenantDiskService, we would still load the TenantDiskService even if
it isn't going to be used.

[1] https://guides.rubyonrails.org/v7.1/configuring.html#config-add-autoload-paths-to-load-path
[2] https://guides.rubyonrails.org/v7.1/upgrading_ruby_on_rails.html#autoloaded-paths-are-no-longer-in-$load-path
2024-04-17 15:18:41 +02:00
Javi Martín
e6ff76d1a4 Merge pull request #5408 from consuldemocracy/dependabot/bundler/pg-1.5.6
Bump pg from 1.4.3 to 1.5.6
2024-04-17 00:50:49 +02:00
dependabot[bot]
128f42c1f5 Bump pg from 1.4.3 to 1.5.6
Dependabot couldn't find the original pull request head commit, b1903b83e07773d28deac423d987df47eada3af1.
2024-04-16 22:07:18 +00:00
Javi Martín
ce7acbbff7 Extract method to get the tenant root storage
This way we simplify the code a little bit and we create a method unique
to the `TenantDiskService` class, which can be used to check whether
we're using this class without using `is_a?` or similar.
2024-04-16 20:52:37 +02:00
Javi Martín
faf765b5c6 Merge pull request #5465 from consuldemocracy/rails7.0
Upgrade to Rails 7.0
2024-04-16 19:43:56 +02:00
Javi Martín
492b2281b0 Use Rails 7.0 defaults and overwrite them
We're keeping the old `apply_stylesheet_media_default` option behavior
because removing `media="screen"` from our stylesheets would completely
break our `print` stylesheet, which would now load the default the
styles defined in `application.css`.

We're also keeping the old `:mini_magick` option to process images so
existing installations don't have to install libvips on their server. We
might change it in the future.
2024-04-15 15:39:28 +02:00
Javi Martín
726d8a8935 Disable Rails' deprecated to_s override
This option was deprecated in Rails 7.0 and removed in Rails 7.1 [1]. It
doesn't really affect us because we weren't using `to_s` with a
parameter anywhere in the application.

The Rubocop rule Rails/ToSWithArgument can be used to detect these cases
but, since we've never used them, and adding them now would cause the
application to crash and so it'll be obvious we've done something wrong,
I don't think it's necessary to add the rule.

[1] https://github.com/rails/rails/commit/e420c3380
2024-04-15 15:39:28 +02:00
Javi Martín
a8047a96c8 Include a hidden field in multiple file inputs
This doesn't really affect us because we don'thave any multiple file
inputs in the application, but we're enabling it because it's the new
default configuration option.
2024-04-15 15:39:28 +02:00
Javi Martín
77977bd8fe Return Content-Type header without modification
Setting it to `true` was deprecated in Rails 7.0 and the option was was
removed in Rails 7.1, so in Rails 7.1 applications it isn't possible to
set it to `true` [1]. So we're setting it to `false` now.

[1] https://github.com/rails/rails/commit/689b27773
2024-04-15 15:39:28 +02:00
Javi Martín
077faa2ad0 Move cookies serializer option to Rails 7.0 file
We're moving it here so it's easier to remove it when we start using the
default Rails 7.0 options and will no longer need to specify it.
2024-04-15 15:39:28 +02:00
Javi Martín
001eee3d6c Use Rails 7.0 cache format
According to the Rails configuration guide [1], with this format, Rails
serializes cache entries more efficiently. Most importantly:

> All formats are backward and forward compatible, meaning cache entries
> written in one format can be read when using another format. This
> behavior makes it easy to migrate between formats without invalidating
> the entire cache.

[1] https://guides.rubyonrails.org/v7.1/configuring.html#config-active-support-cache-format-version
2024-04-15 15:39:28 +02:00
Javi Martín
d846fdad39 Use the new default headers
The only change between these headers and the ones sent by Rails 6.1
application is that now the `X-XSS-Protection` header is set to zero. As
mentioned in the pull request introducing the change [1]:

> This header has been deprecated and the XSS auditor it triggered has
> been removed from all major modern browsers (in favour of Content
> Security Policy) that implemented this header to begin with (Firefox
> never did).

[1] Pull request 41769 in https://github.com/rails/rails
2024-04-15 15:39:28 +02:00
Javi Martín
47331061a8 Use RFC 4122 namespaced UUIDs
This configuration option disappeared in Rails 7.1 [1] (meaning it isn't
possible to set it to `false` in a Rails 7.1 application). Since it's
going to be our only option when upgrading to Rails 7.1, we're already
activating it now.

[1] https://github.com/rails/rails/commit/7b4affc78
2024-04-15 15:39:28 +02:00
Javi Martín
e1e5efe34b Wrap parameters by default in ActionController
As mentioned in the Rails configuration documentation [1] (note the link
points to the configuration guide for Rails 7.1, but only because the
documentation for this option wasn't as good in the configuration guide
for Rails 7.0; the behavior hasn't changed between these two versions),
this was done in the `wrap_parameters` initializer but now it can be
done using a new default configuration option.

[1] https://guides.rubyonrails.org/v7.1/configuring.html#config-action-controller-wrap-parameters-by-default
2024-04-15 15:39:28 +02:00
Javi Martín
cc628f0363 Raise an exception on open redirects
This way we'll add an extra layer of protection from attacks that might
cause our application to redirect to an external host.

There's one place where we're allowing redirects to external hosts,
though: administrators can link external resources in notifications, and
we're redirecting to them after marking the notification as read.

Since the tests for the remote translations controller were
(accidentally) using an external redirect, we're updating them to use a
relative URL.
2024-04-15 15:39:28 +02:00
Javi Martín
9b4525ac71 Disable ActiveRecord partial inserts
As mentioned in the Rails pull request [1], the main reason for partial
inserts is no longer relevant thanks to the `ignored_columns` method
(which we haven't even needed so far).

I don't have a preference regarding this setting; we're enabling it in
order to reduce the number of settings we customize.

[1] Pull request 42769 in https://github.com/rails/rails
2024-04-15 15:39:28 +02:00
Javi Martín
0376da80e7 Verify foreign keys for fixtures
It doesn't really affect us because we don't use fixtures, so we're just
using the new default value.
2024-04-15 15:39:28 +02:00
Javi Martín
961f4a9cd2 Enable automatic inverse_of infering
It looks like we can't really benefit from this rule because usually we
need to specify the option anyway (maybe `user has_many :comments` is
one of the few exceptions). We might make some changes in the code when
Rubocop changes its Rails/InverseOf rule so it doesn't report this case
when using Rails 7, but, until then, we aren't changing anything so we
don't have to deal with false positives in Rubocop.
2024-04-15 15:39:28 +02:00
Javi Martín
14015948b9 Use new defaults for ActiveStorage video previews
Since we don't generate video previews, this change doesn't affect us.
2024-04-15 15:39:28 +02:00
Javi Martín
904a175541 Enable ActionMailer SMTP timeouts
Before Rails 7.0 was released, neither the Mail gem or Rails were
providing a default timeout for SMTP, so there was a risk of processes
being stuck while sending emails.

That's no longer the case, though; we're using version 2.8.x of the Mail
gem, which already provides a default timeout [2].

Since the default timeout provided by the Mail gem is the same as the
default timeout provided by Rails 7.0, it doesn't matter whether we
enable this option. We're enabling because it's easier to just use the
default 7.0 configuration.

[1] Issue 41244 in https://github.com/rails/rails
[2] Pull request 1427 in https://github.com/mikel/mail
2024-04-15 15:39:28 +02:00
Javi Martín
855cd609ad Call Rails executor wrap around test cases
Not sure whether this affects us since we use RSpec; in any case, if it
affects us, it seems like a good idea, although we'll have to watch
whether some tests start failing more often.
2024-04-15 15:39:28 +02:00
Javi Martín
87fa2b1fbb Don't override ActiveSupport::TimeWithZone.name
We aren't getting any warnings when running our test suite, which means
that gems that depended on this method (like graphql [1]) have already
added compatibility for this case.

[1] Pull request 3774 in https://github.com/rmosolgo/graphql-ruby/
2024-04-15 15:39:28 +02:00
Javi Martín
49754bca2e Use SHA256 as digest class
This is similar to what we did in commit 00a5dc921 when upgrading to
Rails 5.2. Quoting from that commit:

> Note this change will cause all fragment caching to expire. We
> consider it acceptable considering the page where caching is most
> important (stats) is barely affected by this change, since this change
> only affects the view, and the time-consuming operations are cached in
> the model.
>
> Comments are actually affected, though, and pages with thousands of
> comments might take a few extra seconds to load the first time they're
> accessed after this change. We don't think this is going to be an
> issue on existing CONSUL installations.
2024-04-15 15:39:28 +02:00
Javi Martín
b3f5705121 Use SHA256 to encrypt messages and cookies
Note that enabling this options means all encrypted messages and cookies
generated the application become invalid, so we're adding a cookie
rotator in order to keep sessions from expiring when upgrading the
application, as recommended in the "Upgrading Ruby on Rails" guideline
[1].

Since we haven't seen any Consul Democracy applications using encrypted
messages and these messages become invalid with this change, we're also
removing the pre-Rails 5.2 encryption to authenticate messages
(AES-256-CBC) and switching to the default one since Rails 5.2
(AES-256-GCM). Since the configured encryption is used by the cookie
rotator initializer (through the ActiveSupport::MessageEncryptor.key_len
method), at first I thought this might affect the cookie rotator, but it
doesn't: upgrading works as expected, and existing sessions are still
active.

I'm adding a comment to remove the initializer once all cookies have
been migrated. I've added "Rails 7.1" in the comment because we usually
check for these comments when upgrading Rails, but we rarely check for
them when after releasing new versions of Consul Democracy.

[1] https://guides.rubyonrails.org/v7.0/upgrading_ruby_on_rails.html#key-generator-digest-class-changing-to-use-sha256
2024-04-15 15:39:28 +02:00
Javi Martín
f384451d9f Always generate <button> tags with button_to
In Rails 6.1 and earlier, `button_to` generated a <button> tag when it
received the content as a block, but an <input> tag when receiving
the content as the first parameter.

That's why we were using blocks with `button_to` most of the time; for
starters, <button> tags accept pseudocontent and so are easier to style.

In Rails 7.0, `button_to` always generates a <button> tag [1], so we're
simplifying the code what uses `button_to`, passing the content as a
first parameter instead of passing it as a block.

[1] https://guides.rubyonrails.org/v7.1/configuring.html#config-action-view-button-to-generates-button-tag
2024-04-15 15:39:28 +02:00
Javi Martín
8596f1539f Upgrade to Rails 7.0
The config.file_watcher option still exists but it's no longer included
in the default environtment file. Since we don't use it, we're removing
it.

The config.assets.assets.debug option is no longer true by default [1],
so it isn't included anymore.

The config.active_support.deprecation option is now omitted on
production in favor of config.active_support.report_deprecations, which
is false by default. I think it's OK to keep it this way, since we check
deprecations in the development and test environments but never on
production environments.

As mentioned in the Rails upgrade guide, sprockets-rails is no longer a
rails dependency and we need to explicitly include it in our Gemfile.

The behavior of queries trying to find an invalid enum value has changed
[2], so we're updating the tests accordingly.

The `favicon_link_tag` method has removed the deprecated `shortcut`
link type [3], so we're updating the tests accordingly.

The method `raw_filter` in ActiveSupport callbacks has been renamed to
`filter` [4], so we're updating the code accordingly.

[1] https://github.com/rails/rails/commit/adec7e7ba87e3
[2] https://github.com/rails/rails/commit/b68f0954
[3] Pull request 43850 in https://github.com/rails/rails
[4] Pull request 41598 in https://github.com/rails/rails
2024-04-15 15:39:23 +02:00
Javi Martín
9dff6aa201 Merge pull request #5476 from consuldemocracy/puma_daemon_leftouts
Remove task to stop the puma daemon
2024-04-12 13:21:30 +02:00
Javi Martín
282e6030a4 Merge pull request #5475 from consuldemocracy/fix_gitlab_ci
Install Node packages before compiling assets in Gitlab CI
2024-04-12 13:20:46 +02:00
Javi Martín
0902fde19d Install Node packages before compiling assets in Gitlab CI
The asset compilation was failing because it needs the packages that are
installed with `npm clean-install`.

We're now installing the Node packages after installing the gems, like
we do in Github Actions. For this, we need to install the `npm` package.
2024-04-11 21:35:10 +02:00
Javi Martín
02b1725eaf Remove task to stop the puma daemon
This task was necessary when updating to version 2.1.0, when we
integrated Puma with Systemd. Now that all Consul Democracy
installations are using Systemd, we no longer need it.
2024-04-11 21:34:56 +02:00
Javi Martín
5f8db67cc0 Make Apartment compatible with Rails 7
The `database:` argument from the `connected_to` method was deprecated
in Rails 6.1 [1] and removed in Rails 7.0 [2]. The ros-apartement gem
has already introduced fixes for this issue [3][4], but there has been
no release including these fixes.

So we're applying the fix in our code.

Note that, since Apartment already overwrites the `connected_to` method
by creating a `connected_to_with_tenant` method, we're calling the
`connected_to_without_tenant` method inside the patch, which is
equivalent to ActiveRecord's original `connected_to`.

[1] Pull request 37874 in https://github.com/rails/rails
[2] Pull request 40530 in https://github.com/rails/rails/pull
[3] Pull request 194 in https://github.com/rails-on-services/apartment
[4] Pull request 243 in https://github.com/rails-on-services/apartment
2024-04-11 20:28:23 +02:00
Javi Martín
b520cb8f24 Merge pull request #5425 from consuldemocracy/zeitwerk
Use Zeitwerk to autoload files
2024-04-11 20:26:44 +02:00
Javi Martín
dbacd7fbfa Replace byebug with the debug gem included in Ruby
Byebug hasn't been maintained for years, and it isn't fully compatible
with Zeitwerk [1]. On the other hand, Ruby includes the debug gem since
version 3.1.0. We tried to start using at after commit e74eff217, but
couldn't do so because our CI was hanging forever in a test related to
machine learning, with the message:

> DEBUGGER: Attaching after process X fork to child process Y

(Note this message appeared with debug 1.6.3 but not with the version
we're currently using.)

So we're changing the debug gem fork mode in the test so it doesn't hang
anymore when running our CI. We tried to change the test so it wouldn't
call `Process.fork`, but this required changing the code, and since
there are no tests checking machine learning behavior with real scripts,
we aren't sure whether these script would keep working after changing
the code.

[1] Issue 564 in https://github.com/deivid-rodriguez/byebug
2024-04-11 20:04:19 +02:00
Javi Martín
e8195c201d Avoid warnings during initialization
These warnings appear in the logs in the development environment, and,
with Rails 7, the application will crash. When running the tests, they
would appear in the standard error ouput if we set `config.cache_classes
= false` in the test environment but, since that isn't the case, they
don't.

To reproduce these warnings (or the lack of them), start a Rails console
in development and check the log/development.log file.
2024-04-11 19:08:02 +02:00
Javi Martín
59f84deca1 Eager load the test environment like in Rails 7
Quoting from pull request 43508 in the Rails repository [1]:

> When you are running test locally, most of the time you run only a
> subset, so it's better to load as little code as possible to have a
> faster time to first test result.
>
> But when you are on CI, it's usually much preferable to eager load the
> whole application because you will likely need all the code anyway,
> and even if the test suite is split across runners, it's preferable to
> load all the code to ensure any codefile that may have side effects is
> loaded.
>
> This also ensure that if some autoloaded constants are not properly
> tested on CI, at least they'll be loaded and obvious errors (e.g.
> SyntaxError) will be caught on CI rather than during deploy.

[1] https://github.com/rails/rails/commit/db0ee287eed
2024-04-11 19:08:02 +02:00
Javi Martín
6552e3197d Use load instead of require_dependency in custom files
While using `require_dependency` to load original Consul Democracy code
from custom code works with the classic autoloader, this method was
never meant to be used this way. With zeitwerk, the code (apparently)
works in the test, development and production environments, but there's
one important gotcha: changing any `.rb` file in development will
require restarting the rails server since the application will crash
when reloading.

Quoting zeitwerk's author Xavier Noria, whom we've contacted while
looking for a solution:

> With the classic autoloader, when the Setting constant is autoloaded,
> the autoloader searched the autoload paths, found setting.rb in
> app/models/custom, and loaded it. With zeitwerk, the autoloader scans
> the folders in order and defines an autoload (Module#autoload) in
> Object so Ruby autoloads Setting with app/models/custom/settings.rb.
> Later, when app/models/setting.rb is found, it's ignored since there's
> already an autoload for Setting.
>
> That means the first file is managed by the autoloaders, while the
> second is not.
>
> So require_dependency worked, but it was pure luck, since the purpose
> of require_dependency is forcing the load of files managed by the
> autoloaders and, as we've seen, app/models/settings.rb isn't one of
> them.
>
> With your current pattern for custom files, the best solution is using
> Kernel#load.

So we're using `load` instead of `require_dependency`. Note that, with
`load`, we need to add the `.rb` extension to the required file, and we
no longer have to convert the Pathname to a string with `to_s`.
2024-04-11 19:08:02 +02:00
Javi Martín
5f24ee9121 Use Zeitwerk instead of the classic autoloader
In Rails 6.1, the classic autoloader is deprecated.

We were getting an error because we were using `autoload` in the
ActiveStorage plugin for CKEditor:

expected file app/lib/ckeditor/backend/active_storage.rb to define
constant Ckeditor::Backend::ActiveStorage

So we're removing the line causing the error.

Finally, we can now restore all the tests that that failed sometimes
with the classic autoloader and that we modified in commits 2af1fc72f
and 8ba37b295.
2024-04-11 19:08:02 +02:00
Javi Martín
004684efe6 Use a string to define the class used by Audited
This is possible since Audited 5.2.0, and will make it easier to start
using Zeitwerk because it won't reference a constant in an initializer.
2024-04-11 19:08:02 +02:00
Javi Martín
919755f328 Use the whole module name for SentencesParser
In order for `include SentencesParser` to work with Zeitwerk, we'd have
to change the code slightly, so it follows Ruby conventions to resolve
constants:

```
module RemoteTranslations::Microsoft
  class Client
    include SentencesParser

    # (...)
  end
end
```

This would mean changing the indentation of the whole file. While we can
do that, changing the indentation of a file makes it harder to use
commands like `git blame` or `git log` with the file, so we're doing the
change the easy way.
2024-04-11 19:08:02 +02:00