Commit Graph

549 Commits

Author SHA1 Message Date
taitus
d1693f06fc Keep After=network.target in Puma systemd unit
We keep After=network.target instead of the new default
After=syslog.target network.target introduced in capistrano3-puma 6.0.0.

Our Puma service doesn't depend on syslog availability because it writes
logs directly to files using:

> StandardOutput=append:...
> StandardError=append:...

So the syslog.target dependency is unnecessary in our case.
2025-06-02 15:38:34 +02:00
taitus
b99d2e43bb Keep SyslogIdentifier=puma for compatibility
We keep using SyslogIdentifier=puma instead of the default
value from capistrano3-puma 6.0.0, which is based on
puma_service_unit_name (usually "<app>_puma_<env>").

This avoids introducing a new config variable in the installer
and is enough for our current use case, where we don't run
multiple environments on the same server.
2025-06-02 15:38:34 +02:00
taitus
7712e7a3cf Set WatchdogSec=0 in Puma systemd unit template
The original template sets WatchdogSec=10, which causes systemd to kill
Puma if it remains silent for more than 10 seconds.

This commit changes WatchdogSec to 0 in our custom puma.service.erb,
disabling the watchdog feature. This prevents unwanted restarts and
avoids errors like:
> consul_puma_staging.service: Failed with result 'watchdog'
2025-06-02 15:38:34 +02:00
taitus
034ecfeacd Copy Puma systemd unit template from capistrano3-puma
This commit copies the default puma.service.erb template from the
capistrano3-puma gem into lib/capistrano/templates. This allows us to
customize the generated systemd unit file during deploy.

Note that we are also removing the `:puma_conf` variable from `config/deploy.rb`,
as the new ExecStart line in the systemd template (based on capistrano3-puma 6.0.0)
does not rely on a separate Puma config file. The command now directly invokes:

  ExecStart=<%= expanded_bundle_command %> exec puma -e <%= fetch(:puma_env) %>

This replaces the older format used in 5.2.0:

  ExecStart=<%= expanded_bundle_command %> exec --keep-file-descriptors puma -C <%= fetch(:puma_conf) %>

which required explicitly setting the Puma config path.
2025-06-02 15:33:36 +02:00
Javi Martín
d7c373509a Remove tasks to upgrade to version 2.2
Note that, while we're no longer including them as part of the
`execute_release_2.2.0_tasks` task, we're keeping the tasks to remove
duplicate poll voters and poll options just in case there are some
unexpected issues when adding a unique database index while upgrading to
version 2.3.0. We'll remove them in version 2.4.0.
2025-01-08 16:47:57 +01:00
Javi Martín
fb0c087f95 Add and apply Rails/WhereRange rubocop rule
This rule was added in rubocop-rails 2.25.0. Applying it allows us to
simplify the code a little bit. For example, now there's no need to
specify the `proposals` table in proposal scopes, which was actually
causing a bug in the `Legislation::Proposal` model, which was using the
`proposals` table instead of the `legislation_proposals` table (but,
since we don't use this scope, it didn't affect the application).
2024-07-05 17:11:29 +02:00
Javi Martín
5dbd2ede14 Delete duplicate records in different languages 2024-06-27 15:22:02 +02:00
Javi Martín
58f88d6805 Add task to add option_id to existing answers
Note: to avoid confusion, "answer" will mean a row in the poll_answers
table and "choice" will mean whatever is in the "answer" column of that
table (I'm applying the same convention in the code of the task).

In order make this task perform reasonably on installations with
millions of votes, we're using `update_all` to update all the answers
with the same choice at once. In order to do that, we first need to
check the existing choices and what are the possible option_ids for
those choices.

Note that, in order for this task to work, we need to remote the
duplicate answers first. Otherwise, we will run into a RecordNotUnique
exception when trying to add the same option_id to two duplicate
answers.

So we're making this task depend on the one that removes duplicate
answers. That means we no longer need to specify the task to remove
duplicate answers in the release tasks; it will automatically be
executed when running the task to add an option_id.
2024-06-27 15:05:56 +02:00
Javi Martín
d2ec73e92c Add task to delete duplicate poll answers 2024-06-26 20:20:24 +02:00
Javi Martín
b327275d18 Add a log file to track deleted duplicate records
It might be interesting in some cases to check the information related
to those records.
2024-06-26 15:41:44 +02:00
Javi Martín
b013a5b1b6 Add task to delete duplicate voters
Note that, since poll answers belong to a user and not to a voter, we
aren't doing anything regarding poll answers. This is a separate topic
that might be dealt with in a separate pull request.

Also note that, since there are no records belonging to poll voters, and
poll voters don't use `acts_as_paranoia` and don't have any callbacks on
destroy, it doesn't really matter whether we call `destroy!` or
`delete`. We're using `delete` so there are no unintended side-effects
that might affect voters with the same `user_id` and `poll_id` on
Consul Democracy installations customizing this behavior.
2024-06-26 15:41:44 +02:00
Javi Martín
10d93a04d3 Clear Rails cache when upgrading Consul Democracy
We use caching in our application in two different ways:

1. Rails.cache.fetch in models/controllers/libraries
2. Fragment caching in the views

When using Rails.cache.fetch, we usually set an expiration date or
provide a precise way to invalidate it. If the code changes and the
information stored in the cache is different from what the new code
would return, it's usually not a big deal because the cache will expire
in an hour or a day. Until commit a4461a1a5, the statistics were an
exception to this rule, but that's no longer the case. The actual
exception to this rule are the i18n translations, but the code caching
them is very simple and hasn't changed for more than three years (when
it was written for the first time).

When using fragment caching, on the other hand, Rails automatically
invalidates the cache when the associated _view code_ changes. That is,
if a view contains cache, the view renders a partial, and the partial
changes, the cache is correctly invalidated. The cache isn't invalidated
when the code in helpers, models or controllers change, though, which
the Rails team considers a compromise solution.

However, we've been moving view partials (and even views) to components,
and the cache isn't invalidated when a component changes (it doesn't
matter whether the component Ruby file or the component ERB file
changes). That means that the cache will keep rendering the HTML
generated by the old code.

So, now, we're clearing the cache when upgrading to a new version of
Consul Democracy, as part of the release tasks. That way, institutions
upgrading to a new version don't have to worry about possible issues
with the cache due to the new code not being executed.

I was thinking of adding it to a Capistrano task, but that would mean
that every time people deploy new code, even if it's a hotfix that
doesn't affect the cache at all, the cache would be cleared, which could
be inconvenient. Doing it in a release, that usually changes thousands
of lines of code, sounds like a good compromise.
2024-06-17 14:48:34 +02:00
Javi Martín
a4461a1a56 Expire the stats cache once per day
When we first started caching the stats, generating them was a process
that took several minutes, so we never expired the cache.

However, there have been cases where we run into issues where the stats
shown on the screen were outdated. That's why we introduced a task to
manually expire the cache.

But now, generating the stats only takes a few seconds, so we can
automatically expire them every day, remove all the logic needed to
manually expire them, and get rid of most of the issues related to the
cache being outdated.

We're expiring them every day because it's the same day we were doing in
public stats (which we removed in commit 631b48f58), only we're using
`expires_at:` to set the expiration time, in order to simplify the code.

Note that, in the test, we're using `travel_to(time)` so the test passes
even when it starts an instant before midnight. We aren't using
`:with_frozen_time` because, in similar cases (although not in this
case, but I'm not sure whether that's intentional), `travel_to` shows
this error:

> Calling `travel_to` with a block, when we have previously already made
> a call to `travel_to`, can lead to confusing time stubbing.
2024-05-17 20:11:16 +02:00
Javi Martín
a5646fcdb3 Remove Cron job to generate stats
Since now generating stats (assuming the results aren't in the cache)
only takes a few seconds even when there are a hundred thousand
participants, as opposed to the several minutes it took to generate them
when we introduced the Cron job, we can simply generate the stats during
the first request to the stats page.

Note that, in order to avoid creating a temporary table when the stats
are cached, we're making sure we only create this table when we need to.
Otherwise, we could spend up to 1 second on every request to the stats
page creating a table that isn't going to be used.

Also note we're using an instance variable to check whether we're
creating a table; I tried to use `table_exists?`, but it didn't work. I
wonder whether `table_exists?` doesn't detect temporary tables.
2024-05-17 16:08:08 +02:00
Javi Martín
144d1d8d05 Add a task to mask existing IPs collected with Ahoy
According to the README [1]:

> To mask previously collected IPs, use:
> Ahoy::Visit.find_each do |visit|
>   visit.update_column :ip, Ahoy.mask_ip(visit.ip)
> end

We're adapting the code with our version, since we use the `Visit` model
instead of the `Ahoy::Visit` model.

[1] https://github.com/ankane/ahoy/blob/v5.0.2/README.md#ip-masking
2024-05-13 14:59:30 +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
1cf529b134 Make Devise find the strategy class automatically
Since we're already setting `wordpress_oauth2` using the `option :name`
command in the `OmniAuth::Strategies::Wordpress` class, Devise can
automatically find the strategy. However, it wasn't working because we
were passing a string instead of a symbol.
2024-04-11 19:08:02 +02:00
Javi Martín
d8faa4f4d0 Follow Zeitwerk conventions for file structure
Even though we don't load this file with Zeitwerk, we're doing it for
consistency.

If we tried to load this file using Zeitwerk, without this change we'd
get an error:

```
NameError: uninitialized constant OmniauthWordpress
```
2024-04-11 19:08:02 +02:00
Javi Martín
cb477149c4 Move lib folder inside the app folder
The purpose of the lib folder is to have code that doesn't necessary
belong in the application but can be shared with other applications.

However, we don't have other applications and, if we did, the way to
share code between them would be using a gem or even a git submodule.

So having both the `app/` and the `lib/` folders is confusing IMHO, and
it causes unnecessary problems with autoloading.

So we're moving the `lib/` folder to `app/lib/`. Originally, some of
these files were in the `app/services/` folder and then they were moved
to the `lib/` folder. We're using `app/lib/` instead of `app/services/`
so the upgrade is less confusing.

There's an exception, though. The `OmniAuth::Strategies::Wordpress`
class needs to be available in the Devise initializer. Since this is an
initializer and trying to autoload a class here will be problematic when
switching to Zeitwerk, we'll keep the `require` clause on top of the
Devise initializer in order to load the file and so it will be loaded
even if it isn't in the autoload paths anymore.
2024-04-11 19:08:01 +02:00
Javi Martín
913b93aea7 Fix DocumentParser being included for all objects 2024-04-11 19:08:01 +02:00
Javi Martín
f8c97b9bb9 Remove monkey-patch of the Numeric class
This monkey-patch doesn't seem to be working with Zeitwerk, and we were
only using it in one place, so the easiest way to solve the problem is
to remove it.

Note that, in the process, we're changing the operation so `* 100`
appears before the division, so it's consistent with other places where
we do similar things (like the `supports_percentage` method in the
proposals helper).
2024-04-11 19:08:01 +02:00
Javi Martín
d19d341622 Remove unused lib/assets folder
We use vendor/assets and app/assets; the purpose of lib/assets isn't
that clear, though. According to the Rails guides:

> lib/assets is for your own libraries' code that doesn't really fit
> into the scope of the application or those libraries which are shared
> across applications.

So it must be something for companies having several Rails applications,
which isn't our case. Furthermore, this text has been removed from the
Rails guides for version 7.1, so this folder might be a legacy folder.
2024-04-11 19:08:01 +02:00
Javi Martín
90f753af98 Remove tasks to upgrade to version 2.0.0
These tasks have already been executed.
2024-02-13 18:11:24 +01:00
dependabot[bot]
21d39bac62 Bump rubocop-rails from 2.20.2 to 2.21.2
Bumps [rubocop-rails](https://github.com/rubocop/rubocop-rails) from 2.20.2 to 2.21.2.
- [Release notes](https://github.com/rubocop/rubocop-rails/releases)
- [Changelog](https://github.com/rubocop/rubocop-rails/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rubocop/rubocop-rails/compare/v2.20.2...v2.21.2)

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

Note version 2.21.0 relaxes the default `Include` path for
`Rails/FindEach`, and so this version can find and correct offenses
outside the `app/models/` folder [1].

Also note this version replaces `unless something.include?` with `if
something.exclude?`; since we don't use the `exclude?` method anywhere,
we're removing the `include?` method from the list of methods checked by
this cop.

Finally, the Rails/HttpStatus method now returns a false positive when
rendering a dashboard partial and passing the `status` variable. In
order to avoid this issue, we could change the name of the local
variable or move the partial to a component, but for now we're simply
excluding these files for this cop.

[1] https://github.com/rubocop/rubocop-rails/pull/1059/commits/0066b3505

Signed-off-by: dependabot[bot] <support@github.com>
2023-11-20 14:22:09 +01:00
Senén Rodero Rodríguez
9112d2d73b Include a timestamp in every authentication logger message 2023-10-25 10:13:04 +02:00
Senén Rodero Rodríguez
b7073691f1 Log successful and failed login attempts in a separate log file
We log the login parameter and the request IP address.

Quoting the ENS:

> [op.acc.5.r5.1] Se registrarán los accesos con éxito y los fallidos.
2023-10-25 10:13:03 +02:00
Javi Martín
827bb125b2 Explicitly open markdown links in the same window
We were already opening them in the same window because we were
accidentall sanitizing the `target` attribute, but now we're making the
point more explicit.
2023-10-24 16:41:03 +02:00
Javi Martín
cdf859621e Allow links in forms to open in new tabs
We used to open these links in new tabs, but accidentally stopped doing
so in commit 75a28fafc.

While, in general, automatically opening a link in a new tab/window is a
bad idea, the exception comes when people are filling in a form and
there are links to pages that contain information which will help them
fill in a form.

There are mainly two advantages of this approach. First, it makes less
likely for people to accidentally lose the information they were filling
in. And, second, having both the form and a help page open at the same
time can make it easier to fill in the form.

However, opening these links in new tabs also has disadvantages, like
taking control away from people or making it harder to navigate through
pages when using a mobile phone.

So this is a compromise solution.
2023-10-23 18:19:48 +02:00
Javi Martín
f87d4b589d Add and apply Naming/BlockForwarding rubocop rule
This syntax has been added in Ruby 3.1.

Not using a variable name might not be very descriptive, but it's just
as descriptive as using "block" as a variable name. Using just `&` we
get the same amount of information than using `&block`: that we're
passing a block.

We're still using `&action` in `around_action` methods because here we
aren't using a generic name for the variable, so (at least for now) we
aren't running this cop on controllers using `around_action`.
2023-09-12 15:17:28 +02:00
Javi Martín
28aafbd4bc Add and apply Style/InvertibleUnlessCondition rule
This rule was added in rubocop 1.44.0. It's useful to avoid accidental
`unless !condition` clauses.

Note we aren't replacing `unless zero?` with `if nonzero?` because we
never use `nonzero?`; using it sounds like `if !zero?`.

Replacing `unless any?` with `if none?` is only consistent if we also replace
`unless present?` with `if blank?`, so we're also adding this case. For
consistency, we're also replacing `unless blank?` with `if present?`.

We're also simplifying code dealing with `> 0` conditions in order to
make the code (hopefully) easier to understand.

Also for consistency, we're enabling the `Style/InverseMethods` rule,
which follows a similar idea.
2023-09-07 19:14:03 +02:00
Javi Martín
a1439d0790 Apply Layout/LineLength rubocop rule
Note we're excluding a few files:

* Configuration files that weren't generated by us
* Migration files that weren't generated by us
* The Gemfile, since it includes an important comment that must be on
  the same line as the gem declaration
* The Budget::Stats class, since the heading statistics are a mess and
  having shorter lines would require a lot of refactoring
2023-08-30 14:46:35 +02:00
Javi Martín
8898c30f55 Rename AvailableLocales.available_locales method
I'm not sure whether we should rename the class instead. I'm renaming
the method because renaming the class would require more changes.
2023-08-30 14:46:35 +02:00
Javi Martín
36c3ba6601 Extract variable in manager authenticator method 2023-08-30 14:46:35 +02:00
Javi Martín
8b13daad95 Add and apply rules for multi-line hashes
For the HashAlignment rule, we're using the default `key` style (keys
are aligned and values aren't) instead of the `table` style (both keys
and values are aligned) because, even if we used both in the
application, we used the `key` style a lot more. Furthermore, the
`table` style looks strange in places where there are both very long and
very short keys and sometimes we weren't even consistent with the
`table` style, aligning some keys without aligning other keys.

Ideally we could align hashes to "either key or table", so developers
can decide whether keeping the symmetry of the code is worth it in a
case-per-case basis, but Rubocop doesn't allow this option.
2023-08-18 14:56:16 +02:00
Javi Martín
d137df67bf Fix release version number in rake tasks
When we added the tasks, we thought the new version was going to be
version 1.6.0, but in the end we're renaming it to version 2.0.0.
2023-07-13 17:48:26 +02:00
Javi Martín
a668ecd1a8 Merge pull request #5136 from Meet-Democracy/Legislation-draft-render-markdown-tables
Render markdown tables in legislation draft
2023-06-30 00:09:33 +02:00
Javi Martín
af618eaa45 Extract markdown helper logic to a class
This way it'll be easier for other Consul installations to overwrite
parts of the code, like the default options.
2023-06-29 20:48:01 +02:00
Karim Semmoud
3faaa8521d Render markdown tables in legislation draft
* Add Tables option to Redcarpet in Legislation draft

* Allow table tags in Admin Legislation Sanitizer

* Add Test to render markdown tables in Legislation drafts

* Add Test for Admin Legislation Sanitizer

We include test for image, table and h1 to h6 tags and additional tests to strengthen the allowed and disallowed parameters

* Add Table from markdown test in System and Factories

* Add test to render  tables for admin user

* Remove comment line about Redcarpet options

* Edit custom css for legislation draft table to make it responsive
2023-06-29 20:48:01 +02:00
Senén Rodero Rodríguez
83b5965821 Fix Serbian locale code
I accidentally duplicated the locale code within
commit 3c3ff65.
2023-06-27 18:06:19 +02:00
Senén Rodero Rodríguez
3c3ff65be1 Update search dictionaries
Since the creation of this list 3 years ago we added more languages to the
application and PostgreSQL added more dictionaries too.
2023-06-23 12:53:43 +02:00
Senén Rodero Rodríguez
1d7c821935 Sort languages alphabetically
So it's easier to know where to add new ones when needed.
2023-06-23 12:53:41 +02:00
taitus
306e7356c3 Allow translate locales that need to be mapping
It has been detected that for the :pt-BR, :zh-CN and :zh-TW locales,
the translate button was being displayed, but when requesting the
translation, the remote translation validation failed due to:

'''
validates :locale, inclusion: { in: ->(_) {
     RemoteTranslations::Microsoft::AvailableLocales.available_locales }}
'''

That available_locales method did not contemplate these 3 languages
in the format used by the application.

To solve this problem the api response is mapped to return all
locales in the format expected by the application.

Add remote translation model test to ensure that a remote translation
is valid when its locale is pt-BR.

Co-Authored-By: Javi Martín <35156+javierm@users.noreply.github.com>
2023-03-15 15:52:51 +01:00
taitus
c64b49b128 Change gem from TranslatorText to BingTranslator
TranslatorText isn't compatible with Ruby 3, so we need to use a
different gem.

The 'translator-text' gem response was an array of one or more objects.
Now with the 'bing_translator' gem the response is an array with one or
several translated texts.

We remove the concept of object in the code. And we also remove the
"create_response" method from the specs since it is no longer necessary
to emulate that object and we can simply use arrays with texts to emulate
the new response.
2023-03-09 06:00:41 +01:00
Javi Martín
63d0e316cf Replace instance variable usage with a method
We usually use this approach because methods are easier to override and
stub.
2023-03-09 05:50:27 +01:00
Javi Martín
b030a198a3 Include SentencesParser inside the right class
We were including it in the Object class, making its methods
available everywhere.
2023-03-09 05:50:16 +01:00
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
edd47877c2 Extract methods to get tenant subfolder/file paths
We were using the same logic in many different places, so we're
simplifying the code. I'm not convinced about the method names, though,
so we might change them in the future.

Note using this method for the default tenant in the `TenantDiskService`
class resulted in a `//` in the path, which is probably harmless but
very ugly and it also generates a different key than the one we got
until now. I've added an extra test to make sure that isn't the case.
2022-11-11 01:41:14 +01:00
Eduardo Vilar
0ea61b9b61 Allow different omniauth settings per tenant
Co-Authored-By: Javi Martín <javim@elretirao.net>
2022-11-11 01:39:30 +01:00
Javi Martín
18f1d5c1a3 Allow different remote translation keys per tenant
Note we don't need to update the tests; the tests themselves help us
confirm that `Rails.application.secrets` and `Tenant.current_secrets`
return the same object on single-tenant applications.
2022-11-11 01:39:29 +01:00
Javi Martín
338f4929ca Allow different manager auth settings per tenant 2022-11-11 01:39:29 +01:00