Commit Graph

15065 Commits

Author SHA1 Message Date
Javi Martín
ddb37f89ae Apply Style/Proc rubocop rule
While I tend to use `Proc.new`, using `proc` is shorter and more
consistent since we also use `lambda`.
2019-10-26 13:22:49 +02:00
Javi Martín
69c01df63e Apply Style/OrAssignment rubocop rule 2019-10-26 13:22:44 +02:00
Javi Martín
8b5cca746c Apply rubocop rules to freeze constants
Added by popular demand among our team members.
2019-10-26 13:21:36 +02:00
Javi Martín
1ba929b4dd Apply Style/MethodDefParentheses rubocop rule
This is an obvious one, but there was one place where we weren't
following it.
2019-10-26 13:03:49 +02:00
Javi Martín
01fe31c5e3 Apply Style/IdenticalConditionalBranches rule 2019-10-26 13:03:49 +02:00
Javi Martín
b5eeb3f32f Apply Style/HashSyntax rubocop rule
We were already using it almost everywhere.
2019-10-26 13:03:49 +02:00
Javi Martín
70da5a3801 Apply Style/ColonMethodCall rubocop rule
This rule seems to be an obvious one, but we weren't following it in one
place.
2019-10-26 13:03:49 +02:00
Javi Martín
eafb4018bf Apply Style/CollectionMethods rubocop rule
We were already using `map` and `reduce` almost everywhere.
2019-10-26 13:03:49 +02:00
Javi Martín
e3bfcbcd25 Apply Style/ClassVars rubocop rule
Class variables in Ruby are not the same as instance variables of a
class. They're particularly tricky when it comes to inheritance and
modules.

In the case of the Measurable module, for example, using a class
variable will make all classes including the Measurable module share
the same value. However, that's not what we want; we want the variable
to be different for every class. And that's accomplished using a class
instance variable.

Although in this case it would probably be better to remove the caching
variable. I don't think these methods are called more than once during a
request, and even if they did it's highly unlikely the would become a
bottleneck.
2019-10-26 13:03:49 +02:00
Javi Martín
dfe9ab2c69 Apply Style/ClassCheck rubocop rule
We were already using `is_a?` almost everywhere.
2019-10-26 13:03:49 +02:00
Javi Martín
a5def0cdb5 Apply Style/AndOr and Style/Not rubocop rules
The `and` and `or` keywords are not equivalent to `&&` and `||` and its
use is counterintuitive. Here's an example

```
good = true && false # good if false
bad = true and false # bad is true
```

The reason is `and` and `or` are control flow operators. So the code:

```
bad = true and false
```

Is equivalent to:


```
if bad = true
  false
end
```
2019-10-26 13:03:49 +02:00
Javi Martín
f07c422f21 Apply Layout/SpaceInLambdaLiteral rubocop rule
I had mixed feelings about this rule, since I like spaces where
possible.

However, I changed my mind when I realized writing `->(thing) { }` was
similar to defining a method, and we don't have a space before the
parenthesis when defining a method.
2019-10-26 13:03:49 +02:00
Javi Martín
a0554f5bba Apply Layout/LeadingBlankLines rubocop rule
This rule is pretty obvious. However, we weren't following it in a
couple of places.
2019-10-26 13:03:48 +02:00
Javi Martín
d218e09654 Use squiggly heredocs
This is a feature which was introduced in Ruby 2.3, and makes it much
easier to write readable heredocs.
2019-10-26 13:03:48 +02:00
Javi Martín
e21565628f Apply Layout/IndentAssignment rubocop rule
There was one place where we weren't applying it.
2019-10-26 13:03:48 +02:00
Javier Martín
e5e414f806 Merge pull request #3799 from consul/rubocop_all
Merge basic and standard rubocop files in one file
2019-10-25 23:50:15 +02:00
Javi Martín
cf361defc7 Update rubocop version for hound
Hound now supports rubocop 0.75.
2019-10-25 23:23:28 +02:00
Javi Martín
621523cfd9 Merge basic and standard rubocop files in one file
A bit of history in order to understand this change.

A year ago we introduced Hound so it would review our pull requests and
warn contributors when they weren't following our coding style.

However, back then we had many rules we hadn't reviewed yet, and we
weren't sure we wanted to ask contributors to follow them.

So we decided to split these files: .rubocop_basic.yml would contain
rules we had already agreed on and wanted contributors to respect, and
.rubocop.yml would contain rules we still had to review.

Now we've finally gone through all these rules. We've removed some of
them, kept some of them, added new ones, and applied them.

Now all rules with a severity level of "convention" or higher return no
offenses. The rules with "severity: refactor" return some offenses,
though:

* Metrics/LineLenght can only be properly accomplished when we define
better multi-line indentation standards, while in some cases long lines
indicate we need to refactor the code
* Rails/DynamicFindBy returns a few false positives
* Rails/HasManyOrHasOneDependent should by all means be implemented,
although it will not be a trivial task
* Rails/SaveBang returns a few false positives and there are a couple of
places where we skip it on purpose

There are also rules excluding some files:

* Rails/InverseOf returns a false positive
* Rails/OutputSafety is ignored on purpose when we add auto-links to a
text we trust
* RSpec/InstanceVariable returns false positives in two files

Other than that, everything works as expected.
2019-10-25 23:23:27 +02:00
Javi Martín
dd0dbee73b Reduce severity of HasManyOrHasOneDependent rule
This is actually a hack. We want Hound to warn us about this rule;
however, we don't want to be notified about our many existing offenses.

Ideally we would add the `dependent` option to all existing models.
However, this is extra tricky because adding this option would change
existing behavior.
2019-10-25 23:17:51 +02:00
Javi Martín
c06186a111 Move performance and security rules to basic cops
As mentioned in commit 9d566a22, I've kept these performance cops but
not for performance reasons but because they make the code easier to
read.

As for the security cops, we've never had problems with any of them, but
since we recently added an `eval` call by accident, there's a chance we
could do the same with JSONLoad and YAMLLoad.
2019-10-25 23:17:51 +02:00
Javi Martín
f3c31f0ea3 Remove MarshalLoad rubocop rule
We don't use Marshal objects in our application.
2019-10-25 23:17:51 +02:00
Javi Martín
97e826f2a4 Don't use update_attribute
This method is ambiguous. Sometimes we use it to set invalid data in
tests (which can usually be done with `update_column`), and other times
we use it instead of `update!`.

I'm removing it because, even if sometimes it could make sense to use
it, it's too similar to `update_attributes` (which is an alias for
`update` and runs validations), making it confusing.

However, there's one case where we're still using it: in the
ActsAsParanoidAliases module, we need to invoke the callbacks, which
`update_column` skips, but tests related to translations fail if we use
`update!`. The reason for this is the tests check what happens if we
restore a record without restoring its translations. But that will make
the record invalid, since there's a validation rule checking it has at
least one translation.

I'm not blacklisting any other method which skips validations because we
know they skip validations and use them anyway (hopefully with care).
2019-10-25 23:17:50 +02:00
Javi Martín
bbce3479cf Simplify touching a budget when a phase changes
The `belongs_to` method already has that option, so there's no need to
do it manually in an `after_save` callback.
2019-10-25 23:17:49 +02:00
Javi Martín
35e8e9da31 Fix validations being accidentally skipped in spec
We were trying to test a before_validation call, but the `touch` method
skips validations.
2019-10-25 20:14:40 +02:00
Javier Martín
1e6aacea73 Merge pull request #3798 from consul/inverse_of
Apply Rails/InverseOf rubocop rule
2019-10-25 20:09:35 +02:00
Javi Martín
1da0fe1ee2 Apply Rails/HasAndBelongsToMany rubocop rule
We were using it everywhere except in one place.
2019-10-25 19:29:12 +02:00
Javi Martín
42d2e5b3ad Apply Rails/InverseOf rubocop rule
Not doing so has a few gotchas when working with relations, particularly
with records which are not stored in the database.

I'm excluding the related content file because it's got a very peculiar
relationship with itself: the `has_one :opposite_related_content` has no
inverse; the relation itself is its inverse. It's a false positive since
the inverse condition is true:

```
content.opposite_related_content.opposite_related_content.object_id ==
  content.object_id
```
2019-10-25 19:29:12 +02:00
Javi Martín
94d2496f8f Add missing has_many relations for users
Usually when we specify a `belongs_to` relations, we also specify its
equivalent `has_many`. That allows us to write, for example:
`topic.user.topics`.
2019-10-25 19:27:30 +02:00
Javi Martín
915e2792fc Use self.name for same-table relations
Personally reading the class name made me take some time to realize it
was a same-table relation.
2019-10-25 19:03:14 +02:00
Javi Martín
27ed26d6f2 Remove unnecessary class names in relations
Just like we do in the Budget module, and in some places in the Poll and
Legislation modules, we don't need to specify the class name when the
name of the relation matches the name of a class in the same module.
2019-10-25 19:03:14 +02:00
Javi Martín
fda53a0a2a Remove unnecessary foreign_key options
When we specify `belongs_to :author`, ActiveRecord automatically uses
`author_id` as the foreign key.
2019-10-25 19:03:10 +02:00
Javi Martín
904917e568 Fix typo in pair answers column name 2019-10-25 18:05:59 +02:00
Javier Martín
86e159662f Merge pull request #3802 from consul/html_area
Make HTML areas independent of CKEditor
2019-10-25 17:31:12 +02:00
Javi Martín
e844b0b2db Remove CKEditor divs
This way the HTML does not depend on CKEditor, and changing the editor
we use in textareas will require very few changes.
2019-10-25 17:00:18 +02:00
Javi Martín
6ef07f8a54 Use text_area instead of cktext_area
We're going to change CKEditor to an inline editor, and the "ckeditor"
gem doesn't provide an option to do so.

Since using `cktext_area` would automatically generate a "classic"
iframe CKEditor, we need to use `text_area` and load the editor using
JavaScript. Personally I prefer this option anyway.

Note in the jQuery selector we need to use `textarea.html-area`; using
just `.html-area` would fail if there's an error message associated to
the textarea, since Rails will add the `.html-area` class to the error
message.
2019-10-25 16:34:25 +02:00
Javier Martín
86c00245ec Merge pull request #3801 from consul/refactor_shared_partial
Use the shared partial to render errors
2019-10-25 15:46:41 +02:00
Javi Martín
025923ac4e Set current locale as HTML language in all layouts
The same way it's done in the application layout. Not doing so causes
accessibility issues affecting screen reader users.
2019-10-25 15:34:40 +02:00
Javi Martín
3012df29e7 Add matcher for CKEditor fields 2019-10-25 15:34:40 +02:00
Javi Martín
8ff728ee83 Use the shared partial to render errors
We were using it most of the time, but in some places we still had
duplicated code.
2019-10-25 15:15:47 +02:00
Javier Martín
ed15b3730c Merge pull request #3796 from consul/rubocop_rails
Apply rubocop Rails rules
2019-10-24 21:46:19 +02:00
Javi Martín
2fd79de068 Remove unneeded Rails/Exit rubocop rule
I don't think we need it because I've never seen it in our Rails
application.
2019-10-24 21:20:18 +02:00
Javi Martín
6214bda941 Move already in use Rails rules to basic cops
We were already applying the FindEach rule since commit cae210c1, while
the EnumUniqueness rule is essential when we use `enum`, and the
EnvironmentComparison rule is very useful since it forces us to use a
format the UnknownEnv rule will recognize.
2019-10-24 21:20:18 +02:00
Javi Martín
df959b74f6 Make migrations reversible
While I don't use this feature, there are developers who do. It's useful
when running migrations and changing branches.

I'm raising an `ActiveRecord::IrreversibleMigration` exception in every
`drop_table` migration because these migrations were all done before
version 1.0.0, and so making all of them reversible would be too much
work for little benefit.
2019-10-24 21:20:17 +02:00
Javi Martín
a9359fd570 Add rules related to migrations
These are rules we can't apply to existing migrations, so I'm excluding
migrations in the affected years from this check. However, we should
probably add timestamps columns and default values in non-null columns
to at least some of the tables which don't have them.
2019-10-24 21:20:17 +02:00
Javi Martín
91a3184281 Simplify creating timestamps in migrations
We were using the `timestamps` method most of the time, but sometimes we
were creating the columns manually.

Note editing past migrations if fine as long as the SQL they generate
remains identical, which is the case here.
2019-10-24 21:20:16 +02:00
Javi Martín
55addaa58a Apply rubocop rules to migration files
There are some rules which only affect migration files, and we cannot
enable them if we're excluding those files from being inspected.

We're also changing migrations related to the Rails/TimeZone rule
slightly because these fields were already changed afterwards, so we
aren't changing the schema.
2019-10-24 20:35:13 +02:00
Javier Martín
b8abf52836 Merge pull request #3797 from consul/use_block_in_travel
Use a block to travel in time in specs
2019-10-24 20:10:32 +02:00
Javi Martín
aac23ea596 Use a block to travel in time in specs
If we don't use a block, if the spec fails the `travel_back` method
isn't executed.
2019-10-24 19:24:22 +02:00
Javier Martín
00145d54ca Merge pull request #3795 from consul/rubocop_empty_lines
Add and apply rubocop rules for empty lines
2019-10-24 18:41:51 +02:00
Javi Martín
57ef380379 Add and apply Layout/ExtraSpacing rubocop rule 2019-10-24 18:11:58 +02:00