Commit Graph

25 Commits

Author SHA1 Message Date
taitus
ee66c4182a Return error when the related content already exists
It looks like this bug was introduced in commit 7ca55c4. Before that
commit, a message saying "You added a new related content" was shown,
but (as expected) the related content wasn't added twice.

We now check this case and add a slightly clearer message.
2021-06-25 17:15:34 +02:00
Javi Martín
89a06ea6ef Fix related content with custom URLs
Some CONSUL installations might want to customize their URLs. For
instance, Spanish institutions might want to use "/propuestas" instead
of "/proposals". In that case, it would be impossible to add proposals
as related content because the related contents controller assumed the
name of the model was part of the URL.

Using `recognize_path` instead of manually analyzing the URL solves the
issue.

Now that we don't call the `constantize` method on an empty string as we
previously did, we can be more specific in the `rescue` block and point
out that the only exception we expect is the one where users enter a
route which isn't recognized.
2021-06-23 23:28:35 +02:00
Javi Martín
48dc72cea9 Avoid a brakeman warning in related contents
Although it wasn't a real security concern because we were only calling
a `find_by` method based on the user input, it's a good practice to
avoid using constants based on user parameters.

Since we don't use the `find_by` method anymore but we still need to
check the associated record exists, we're changing the validations in
the `RelatedContent` model to do exactly that.
2021-06-23 23:13:58 +02:00
Javi Martín
8f20ee1a33 Move related content validation to the model
We were manually checking validation rules (like both relationable
objects are present, or they're both the same object) in the controller
and then using the `save!` method.

However, we usually use the `save` method (which checks all validations)
in a condition, and proceed depending on the result.

Now we're taking the same approach here. This means introducing a new
validation rule in the model to check whether both relationable objects
are the same, which is more robust than checking a condition in the
controller.
2021-06-23 23:13:58 +02:00
Javi Martín
b318a8b8d2 Use polymorphic_path instead of Relationable#url
The #url method in the models provides a feature that can be done using
native Rails features (polymorphic paths), is inconsistent (some models
have that method and some haven't), and only works for URLs in the
public area (but not in sections like administration or management).
Besides, models are already fat enough without introducing methods
related to controllers or routes.
2021-06-23 23:13:58 +02:00
Javi Martín
82c8a5896a Simplify relationable comparison
Two different records will have two different URLs, so we can compare
the records themselves.
2021-06-23 23:13:58 +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
7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00
Javi Martín
47b2c42a1d Apply IndentationConsistency rubocop rule 2019-09-10 20:02:15 +02:00
Javi Martín
e252d82cdb Apply IndentationWidth rubocop rule 2019-09-10 20:02:15 +02:00
Julian Herrero
d24376f6ad Use double quotes in controllers/ 2019-03-13 22:19:49 +01:00
Julian Herrero
9a23393535 Use find instead of find_by_id
This method will raise an exception if resource is not found when
trying to call score_action on nil.
Prefer to raise a 404 HTML NotFound error instead.
2019-01-25 09:03:56 +01:00
Vicente Mendoza
74124fb784 Control when the investment is trying to relate to itself. 2018-02-02 10:52:28 +01:00
María Checa
a1f840e8fd Added improvements to related contents 2018-01-12 18:35:15 +01:00
Bertocq
01d5656884 Fix valid related content url helper method 2017-12-20 14:51:39 +01:00
Bertocq
cf06d5b047 Refactor related content score methods to make them easier to follow 2017-12-20 10:24:00 +01:00
Bertocq
c67851df53 Fix relatedcontent creation and scoring on related content controller 2017-12-20 03:06:06 +01:00
Bertocq
d544d56722 Add correct user to RelatedContentScore on creation 2017-12-20 02:36:13 +01:00
Bertocq
2e0dd9caea Relationable find_related_content should not be used to find, refactored to have a single purpose 2017-12-20 02:36:13 +01:00
Bertocq
f21f5ec05e Refactor Related Content Controller score methods 2017-12-20 02:36:13 +01:00
María Checa
7d8b5838b9 Added new related contents score method 2017-12-19 13:14:12 +01:00
María Checa
d944a88c63 Added flag/unflag related content routes 2017-12-18 18:21:51 +01:00
Bertocq
b02e2729e7 Rubocop style autocorrections 2017-12-16 17:59:16 +01:00
María Checa
88fc740863 Moved RELATIONABLE_MODELS from controller to model 2017-12-14 20:48:32 +01:00
María Checa
5c7b5eed53 Added RelatedContentsController 2017-12-14 14:50:47 +01:00