Commit Graph

284 Commits

Author SHA1 Message Date
Javi Martín
d90b799342 Add RSpec/UndescriptiveLiteralsDescription rule
This rule was added in rubocop-rspec 2.29.0. We were already following
it.
2024-07-21 22:25:45 +02:00
Javi Martín
dea317c614 Add RSpec/EmptyOutput rubocop rule
This rule was added in rubocop-rspec 2.29.0. While we never use the
`output` matcher, it might actually be a good idea to use it in tests of
take tasks, to check the logger output.
2024-07-21 22:25:45 +02:00
dependabot[bot]
cb227e41dc Bump rubocop-rspec from 2.27.0 to 3.0.1
In this version, the RSpec Rails cops have been extracted to a new
`rubocop-rspec_rails` gem [1], and the `RSpec/Capybara/FeatureMethods`
has been removed in favor of the `RSpec/Dialect` cop.

Bumps [rubocop-rspec](https://github.com/rubocop/rubocop-rspec) from 2.27.0 to 3.0.1.
- [Release notes](https://github.com/rubocop/rubocop-rspec/releases)
- [Changelog](https://github.com/rubocop/rubocop-rspec/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rubocop/rubocop-rspec/compare/v2.27.0...v3.0.1)

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

Signed-off-by: dependabot[bot] <support@github.com>

[1] https://docs.rubocop.org/rubocop-rspec/upgrade_to_version_3.html
[2] https://docs.rubocop.org/rubocop-rspec/cops_rspec.html#rspecdialect
2024-07-21 22:25:43 +02:00
Javi Martín
16315e14d2 Add and apply Style/SuperArguments rubocop rule
This rule was added in rubocop 1.64.0.

For clarity, in order to make it obvious that we're modifying the object
we received, we're excluding the Ahoy initializer, whose code was copied
from the Ahoy documentation.

We're also changing the `Types::BaseObject` class so we don't use a
variable with the same name as the parameter and we don't get a false
positive for this rule.
2024-07-09 11:23:02 +02:00
Javi Martín
46dc4a3163 Add and apply Style/MapIntoArray rubocop rule
This rule was added in rubocop 1.63.0.
2024-07-09 11:23:02 +02: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
995f7036de Run every linter separately in Github Actions
The main reason for this change is that Pronto doesn't run in pull
requests opened by external contributors, and we haven't found how to
fix this issue.

Another reason is that Pronto only detects issues in the lines where the
changes are done, but sometimes we introduce issues related to other
lines and those aren't detected by Pronto. Also, when adding or changing
Rubocop rules, or when we update Rubocop, Pronto doesn't check whether
those rules are applied in every Ruby and ERB file, and we sometimes
forget to do so (particularly in ERB files).

So, from now, the linters will check all the code in every pull request.

Note we're now excluding the `vendor` folder in our linters because the
`setup-ruby` action actions generates a `vendor/bundle/` folder with all
our gem dependencies, and we don't want to check those files.
2024-06-18 19:11:55 +02:00
Javi Martín
647121d13e Allow different locales per tenant
Note that, currently, we take these settings from the database but we
don't provide a way to edit them through the admin interface, so the
locales must be manually introduced through a Rails console.

While we did consider using a comma-separated list, we're using spaces
in order to be consistent with the way we store the allowed content
types settings.

The `enabled_locales` nomenclature, which contrasts with
`available_locales`, is probably subconsciously based on similar
patterns like the one Nginx uses to enable sites.

Note that we aren't using `Setting.enabled_locales` in the globalize
initializer when setting the fallbacks. This means the following test
(which we could add to the shared globalizable examples) would fail:

```
it "Falls back to an enabled locale if the fallback is not enabled" do
  Setting["locales.default"] = "en"
  Setting["locales.enabled"] = "fr en"
  allow(I18n.fallbacks).to receive(:[]).and_return([:fr, :es])
  Globalize.set_fallbacks_to_all_available_locales

  I18n.with_locale(:fr) do
    expect(record.send(attribute)).to eq "In English"
  end
end
```

The reason is that the code making this test pass could be:

```
def Globalize.set_fallbacks_to_all_available_locales
  Globalize.fallbacks = I18n.available_locales.index_with do |locale|
    ((I18n.fallbacks[locale] & Setting.enabled_locales) + Setting.enabled_locales).uniq
  end
end
```

However, this would make it impossible to run `rake db:migrate` on new
applications because the initializer would try to load the `Setting`
model but the `settings` table wouldn't exist at that point.

Besides, this is a really rare case that IMHO we don't need to support.
For this scenario, an installation would have to enable a locale, create
records with contents in that locale, then disable that locale and have
that locale as a fallback for a language where content for that record
wasn't created. If that happened, it would be solved by creating content
for that record in every enabled language.
2024-06-05 16:10:56 +02:00
Javi Martín
96ae69fe93 Use a GDPR-compliant configuration for Ahoy
As mentioned in Ahoy's README [1]:

> Ahoy provides a number of options to help with GDPR compliance.
> Update config/initializers/ahoy.rb with:
>
> class Ahoy::Store < Ahoy::DatabaseStore
>   def authenticate(data)
>     # disables automatic linking of visits and users
>   end
> end
>
> Ahoy.mask_ips = true
> Ahoy.cookies = :none

As also mentioned in the README:

> If Ahoy was installed before v5, add an index before making this
> change.
> (...)
> For Active Record, create a migration with:
> add_index :ahoy_visits, [:visitor_token, :started_at]

However, the `visitor_token` doesn't exist in our table, since we
generated the `visits` table when Ahoy used the `visitor_id` column. So
we're using this column for the index.

Note we also need to change the `visit` method, since otherwise we get
an exception [2]. As mentioned on the issue reporting the exception:

> you'll need to copy the latest version of that method and adapt it to
> your model. I believe you'll want to replace:
>
> where(visit_token: ahoy.visit_token) with
> where(id: ensure_uuid(ahoy.visit_token))
>
> where(visitor_token: ahoy.visitor_token) with
> where(visitor_id: ensure_uuid(ahoy.visitor_token))

So we're copying the latest version of that method and changing it
accordingly.

[1] https://github.com/ankane/ahoy/blob/v5.0.2/README.md
[2] Issue 549 in https://github.com/ankane/ahoy
2024-05-09 14:56:25 +02:00
Javi Martín
6502852743 Update initializer reference in rubocop config file
We renamed the initializer in commit 528e59ce2.
2024-04-26 03:26:10 +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
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
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
fdfdbcbd0d Add and apply Style/ArgumentsForwarding rule
We were using generic names like `args` and `options` which don't really
add anything to `*` or `**` because Ruby required us to.

That's no longer the case in Ruby 3.2, so we can simplify the code a
bit.
2024-04-11 17:59:40 +02:00
Javi Martín
0ece087132 Add Style/SingleLineDoEndBlock rubocop rule
This rule was added in Rubocop version 1.57.0. Even if we were always
following it, we think it's useful.

Note this rule doesn't detect the fact that you can still write
multiline lambdas using `{}` as delimiters instead of `do...end`, which
we fixed in the previous commit. But at least it detects the opposite
case.
2024-04-02 17:01:52 +02:00
dependabot[bot]
68285aae06 Bump rubocop-performance from 1.19.1 to 1.20.2
Note we're removing the Performance/StringIdentifierArgument rule
because now it also replaces methods in interpolation, and we don't
particularly prefer using `send(:"#{method}_name")` over
`send("#{method}_name)`. We actually use the latter about two thirds of
the time.

We'll add this rule again if it ever offers the option to ignore the
cases where interpolation is used, although it's highly doubtful that'll
ever happen because this rule is meant for (insignificant) performance
gains and not for code clarity.

Bumps [rubocop-performance](https://github.com/rubocop/rubocop-performance) from 1.19.1 to 1.20.2.
- [Release notes](https://github.com/rubocop/rubocop-performance/releases)
- [Changelog](https://github.com/rubocop/rubocop-performance/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rubocop/rubocop-performance/compare/v1.19.1...v1.20.2)

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

Signed-off-by: dependabot[bot] <support@github.com>
2024-03-23 02:43:24 +01:00
Javi Martín
d0fae3377e Add and apply Rails/WhereMissing rubocop rule
So now we know where to use the `where.missing` method which was
introduced in Rails 6.1.

Note this rule didn't detect all cases where the new method can be used.
2024-03-18 16:05:07 +01:00
Javi Martín
86cf674d0c Add Rails/DeprecatedActiveModelErrorsMethods rule
Even though we're already applying this rule since commit 08b12a78f,
it's very useful to have it so we don't accidentally introduce code that
won't work with Rails 7.

After upgrading to Rails 7, this rule will no longer be necessary, since
the code using the deprecated syntax will not work and so we'll notice
it immediately.
2024-03-18 16:05:07 +01:00
dependabot[bot]
5772e1b14e Bump rubocop-capybara from 2.19.0 to 2.20.0
Note that this version changed the default enforced styles for
ClickLinkOrButtonStyle and NegationMatcher, so we're now specifying
them.

Bumps [rubocop-capybara](https://github.com/rubocop/rubocop-capybara) from 2.19.0 to 2.20.0.
- [Release notes](https://github.com/rubocop/rubocop-capybara/releases)
- [Changelog](https://github.com/rubocop/rubocop-capybara/blob/main/CHANGELOG.md)
- [Commits](https://github.com/rubocop/rubocop-capybara/compare/v2.19.0...v2.20.0)

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

Signed-off-by: dependabot[bot] <support@github.com>
2024-03-01 18:38:13 +01:00
Javi Martín
9972066b0a Merge pull request #5271 from consuldemocracy/dependabot/bundler/rubocop-rails-2.21.2
Bump rubocop-rails from 2.20.2 to 2.21.2
2023-11-20 17:54:30 +01:00
Javi Martín
0aee568977 Add and apply Rails/RedundantActiveRecordAllMethod
This rule was introduced in rubocop-rails 2.21.0.
2023-11-20 14:22:12 +01:00
Javi Martín
522eb6cfa3 Add and apply Rails/SelectMap rule
This rule was introduced in rubocop-rails 2.21.0. Using `pluck` is
easier to read.
2023-11-20 14:22:12 +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
Javi Martín
5e7b5ccfd3 Add and apply Capybara/ClickLinkOrButtonStyle rule
This rule was added in rubocop-capybara 2.19.0. We were following it
most of the time.
2023-11-08 14:18:16 +01:00
Javi Martín
0cec581ec0 Add and apply Capybara/RSpec/HaveSelector rule
This rule was added in rubocop-capybara 2.19.0. We were following it
about 85% of the time.

Now we won't have to check both have_css and have_selector when
searching the code.
2023-11-08 14:18:16 +01:00
Javi Martín
b59899e9f7 Add and apply RSpec/MetadataStyle rubocop rule
This rule was introduced in rubocop-rspec 2.24.0. We were applying it
most of the time.
2023-10-24 22:59:05 +02:00
Javi Martín
013f3282e4 Use the RSpec/FilePath rule replacements
RSpec/FilePath is deprecated since rubocop-rspec 2.24.0 and will be
removed in rubocop-rspec 3.0 in favor of RSpec/SpecFilePathFormat and
RSpec/SpecFilePathSuffix.
2023-10-24 22:59:05 +02:00
Javi Martín
584176adfa Add and apply Style/ArrayIntersect rubocop rule
The `intersect?` method has been added in Ruby 3.1, and it's more
readable than `(a & b).any?`.
2023-09-12 15:19:04 +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
fd9169e0d6 Update Style/HashSyntax Rubocop rule
Ruby 3.1 adds the option for hash shortcuts, so it's possible to write
`{ user: , poll: }` instead of `{ user: user, poll: poll }`.

By default, Rubocop expects the new syntax in Ruby 3.1. While right now
I absolutely hate this new syntax, we're allowing both the old and the
new styles because we might start adopting it once we get used to it.
2023-09-12 15:17:17 +02:00
Javi Martín
6e9df3be5a Upgrade to Rails 6.1
Note that `Capybara.app_host` now returns `nil` by default and that
breaks tests using `lvh.me` or our custom `app_host` method, so we're
setting `Capybara.app_host` to the value it had in earlier versions of
Rails. I also haven't found a way to remove the code to set the
integration session host in relationable tests which I mentioned in
commit ffc14e499.

Also note that we now filter more parameters, and that they match
regular expressions, so filtering `:passw` means we're filtering
`passwd`, `password`, ...
2023-09-11 23:40:37 +02:00
Javi Martín
614b4fbe4c Add and apply FactoryBot/AssociationStyle rule
This rule was added in rubocop-factory_bot 2.23.0. We were following it
sometimes, and sometimes we were not.
2023-09-08 13:52:54 +02:00
Javi Martín
b88a01f641 Add and apply FactoryBot/RedundantFactoryOption rule
This rule was added in rubocop-factory_bot 2.23.0.
2023-09-08 13:39:26 +02:00
Javi Martín
3b2484af77 Add FactoryBot/FactoryAssociationWithStrategy rule
This rule was added in rubocop-factory_bot 2.23.0. Even if we always
follow it, it's a mistake that we've accidentally made in the past
during development.
2023-09-08 13:37:38 +02:00
Javi Martín
1d5f03be8c Add and apply RSpec/ReceiveMessages rubocop rule
This rule was added in rubocop-rspec 2.23.0. I didn't know this method
existed, and it makes the code more readable in some cases.
2023-09-08 13:31:42 +02:00
Javi Martín
9bb2bfdd06 Add and apply RSpec/Rails/NegationBeValid rule
This rule was added in rubocop-rspec 2.23.0. We were already applying it
most of the time.
2023-09-08 13:24:46 +02:00
Javi Martín
fc0511f4d3 Add and appy RSpec/Rails/TravelAround rubocop rule
This rule was added in rubocop-rspec 2.19.0.

When freezing time in a test, `travel_back` is called automatically when
the test finishes, so we can do it in a `before` block instead of an
`around` block.

Note this rule didn't detect our usage of `freeze_time` because we were
using it on cops with a certain tag, but I expect the rule to be able to
detect this usage in the future.
2023-09-08 13:16:24 +02:00
dependabot[bot]
aef78ee6f6 Bump rubocop-rspec from 2.17.1 to 2.23.2
Note that in rubocop-rspec 2.18.0, most Capybara cops were extracted to
a different gem, and the sam happened in rubocop-rspec 2.22.0 with
FactoryBot cops.

So we're adding both rubocop-capybara and rubocop-factory_bot as
dependencies.

Bumps [rubocop-rspec](https://github.com/rubocop/rubocop-rspec) from 2.17.1 to 2.23.2.
- [Release notes](https://github.com/rubocop/rubocop-rspec/releases)
- [Changelog](https://github.com/rubocop/rubocop-rspec/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rubocop/rubocop-rspec/compare/v2.17.1...v2.23.2)

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

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-08 12:46:41 +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
11b962da8f Add and apply Style/MinMaxComparison rubocop rule
This rule was added in rubocop 1.42.0.
2023-09-07 13:09:11 +02:00
Javi Martín
daa35845da Add and apply Style/RedundantStringEscape rule
This rule was added in rubocop 1.37.0. It's particularly useful in the
background image spec, since now there's one less backslash to decipher
when reading the code :).
2023-09-07 13:09:11 +02:00
Javi Martín
a796e450b7 Add RSpec/Capybara/SpecificActions rubocop rule
This rule was added in rubocop-rspec 2.14.0. Even though we always
follow this rule, we haven't always done so in the past. Now we're
making sure we'll keep following this rule.
2023-09-06 19:00:56 +02:00
Javi Martín
05757c7f02 Add and apply RSpec/Rails/InferredSpecType rule
This rule was added in rubocop-rspec 2.14.0.
2023-09-06 19:00:56 +02:00
Javi Martín
8e276e2891 Add FactoryBot/ConsistentParenthesesStyle rule
This rule was added in rubocop-rspec 2.14.0. We were already applying it
most of the time.
2023-09-06 19:00:56 +02:00
Javi Martín
2d4ff86837 Add and apply Capybara/NegationMatcher rubocop rule
This rule was added in rubocop-rspec 2.14.0. We were already following
it most of the time.
2023-09-06 19:00:56 +02:00
Javi Martín
c4e32ea528 Add and apply HaveHttpStatus rubocop rule
This rule was added in rubocop-rspec 2.12.0. We were already applying it
most of the time.
2023-09-06 19:00:56 +02:00
Javi Martín
f79a21f071 Add and apply RSpec/BeEq rubocop rule
This rule was added in rubocop-rspec 2.9.0. Once again, we were applying
it about 50% of the time.
2023-09-06 19:00:56 +02:00
Javi Martín
6268ae9274 Add and apply RSpec/BeNil rubocop rule
This rule was added in rubocop-rspec 2.9.0.

We were using `be_nil` 50% of the time, and `be nil` the rest of the
time. No strong preference for either one, but IMHO we don't lose
anything be being consistent.
2023-09-06 19:00:56 +02:00
Javi Martín
4fc4afa3a7 Add RSpec/ExcessiveDocstringSpacing rubocop rule
This rule was added in rubocop-rspec 2.5.0.
2023-09-06 19:00:56 +02:00
Javi Martín
2951d0fdf8 Add and apply Rails/ResponseParsedBody rubocop rule
This rule was introduced in rubocop-rails 2.18.0.

Since using `response.parsed_body` is shorter than using
`JSON.parse(response.body)`, this also means we can group some lines in
one.
2023-09-06 19:00:39 +02:00