Commit Graph

26 Commits

Author SHA1 Message Date
Javi Martín
07202fea10 Add and apply Style/RedundantBegin rubocop rule
We're about to add code which might fall into the `RedundantBegin`
category, so we're adding the rule in order to prevent that.
2024-10-30 15:57:44 +01:00
Javi Martín
5033691666 Avoid duplicate records in poll answers
Until now, we've stored the text of the answer somebody replied to. The
idea was to handle the scenarios where the user voters for an option but
then that option is deleted and restored, or the texts of the options
are accidentally edited and so the option "Yes" is now "Now" and vice
versa.

However, since commit 3a6e99cb8, options can no longer be edited once
the poll starts, so there's no risk of the option changing once somebody
has voted.

This means we can now store the ID of the option that has been voted.
That'll also help us deal with a bug introduced int 673ec075e, since
answers in different locales are not counted as the same answer. Note we
aren't dealing with this bug right now.

We're still keeping (and storing) the answer as well. There are two
reasons for that.

First, we might add an "open answer" type of questions in the future and
use this column for it.

Second, we've still got logic depending on the answer, and we need to be
careful when changing it because there are existing installations where
the answer is present but the option_id is not.

Note that we're using `dependent: nullify`. The reasoning is that, since
we're storing both the option_id and the answer text, we can still use
the answer text when removing the option. In practice, this won't matter
much, though, since we've got a validation rule that makes it impossible
to destroy options once the poll has started.

Also note we're still allowing duplicate records when the option is nil.
We need to do that until we've removed every duplicate record in the
database.
2024-06-26 20:20:24 +02:00
Javi Martín
9a8bfac5bd Prevent creation of duplicate poll voters
Note that, when taking votes from an erased user, since poll answers
don't belong to poll voters, we were not migrating them in the
`take_votes_from` method (and we aren't migrating them now either).
2024-06-26 15:41:44 +02:00
Javi Martín
a54d424aed Add missing validation rule to poll answers
We were checking we didn't have more votes than allowed in the case of
questions with multiple answers, but we weren't checking it in the case
of questions with a single answer. This made it possible to create more
than one answer to the same question. This could happen because the
method `find_or_initialize_user_answer` might initialize two answers in
different threads, due to a race condition.
2024-06-26 15:41:44 +02:00
Javi Martín
0c650c423d Fix exception creating an answer without an author
We were getting `undefined method `lock!' for nil:NilClass` when the
question allowed multiple answers.
2024-06-26 15:41:44 +02:00
Javi Martín
38b38d1fcc Rename Poll::Question::Answer to Poll::Question::Option
Having a class named `Poll::Question::Answer` and another class named
`Poll::Answer` was so confusing that no developer working on the project
has ever been capable of remembering which is which for more than a few
seconds.

Furthermore, we're planning to add open answers to polls, and we might
add a reference from the `poll_answers` table to the
`poll_question_answers` table to property differentiate between open
answers and closed answers. Having yet another thing named answer would
be more than what our brains can handle (we know it because we did this
once in a prototype).

So we're renaming `Poll::Question::Answer` to `Poll::Question::Option`.
Hopefully that'll make it easier to remember. The name is also (more or
less) consistent with the `Legislation::QuestionOption` class, which is
similar.

We aren't changing the table or columns names for now in order to avoid
possible issues when upgrading (old code running with the new database
tables/columns after running the migrations but before deployment has
finished, for instance). We might do it in the future.

I've tried not to change the internationalization keys either so
existing translations would still be valid. However, since we have to
change the keys in `activerecord.yml` so methods like
`human_attribute_name` keep working, I'm also changing them in places
where similar keys were used (like `poll_question_answer` or
`poll/question/answer`).

Note that it isn't clear whether we should use `option` or
`question_option` in some cases. In order to keep things simple, we're
using `option` where we were using `answer` and `question_option` where
we were using `question_answer`.

Also note we're adding tests for the admin menu component, since at
first I forgot to change the `answers` reference there and all tests
passed.
2024-06-13 19:13:01 +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
Senén Rodero Rodríguez
3da4112d94 Remove Poll::Voter record when there is no more user answers
Now we can remove answers we should provide a way of removing voting.
2022-10-18 11:04:40 +02:00
decabeza
48d7ec75d0 Do not allow to create more answers than the maximum defined
In case we receive consecutive requests we are locking the poll author record
until the first request transaction ends, so the author answers count during
subsequent requests validations is up to date.
2022-10-18 10:39:00 +02:00
Senén Rodero Rodríguez
64676be246 Remove token column from poll_voters table
As it is no longer used as originally pretended [1][2].

[1] Check consul/consul pull request 1994
[2] Check consul/consul pull request 3539
2022-09-22 10:34:07 +02:00
Javi Martín
24ccf23ed8 Don't save the answer if the voter is not recorded
Up until now, we were assuming the voter was valid, but were not raising
an exception if it wasn't. And in the user interface everything seemed
to be working properly.

We were having this issue when skipping verification, when there could
be voters without a document number, which would be considered invalid.

Raising an exception when failing to save the voter and making sure the
answer and the voter are saved inside a transaction solves the problem.
2020-08-07 11:52:24 +02:00
Javi Martín
db97f9d08c Add and apply rubocop rules for empty lines
We were very inconsistent regarding these rules.

Personally I prefer no empty lines around blocks, clases, etc... as
recommended by the Ruby style guide [1], and they're the default values
in rubocop, so those are the settings I'm applying.

The exception is the `private` access modifier, since we were leaving
empty lines around it most of the time. That's the default rubocop rule
as well. Personally I don't have a strong preference about this one.


[1] https://rubystyle.guide/#empty-lines-around-bodies
2019-10-24 17:11:47 +02:00
Javi Martín
19a41eb651 Rename trait with_answers to yes_no
We barely use this trait. In the votation type spec we're probably using
it wrong, and in the answer spec we assume one of the answers is going
to be "Yes".

The name `yes_no` is more expressive, since it makes it clear what the
answers are.
2019-09-24 21:34:06 +02:00
Julian Herrero
31ac8b7f55 Change single quotes to double quotes 2019-02-15 11:40:39 +01:00
Bertocq
34bb9d65b1 Enable RSpec/NotToNot cop and fix all issues
Read about cop at http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NotToNot
2018-01-07 17:39:48 +01:00
Bertocq
ed16a78f42 Enables RSpec/ExampleWording and fixes all issues
Both avoiding 'should' and repiting 'it' on the tests description
improves reading them and also makes all descriptions consistent.

Read about cop at http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleWording
2018-01-07 01:03:45 +01:00
Bertocq
36610f0c1a Use poll question trait with_answers to generate yes/no answers 2017-10-18 02:06:34 +02:00
Bertocq
39f23d3b7b Switch from valid_answers to question_answers on specs 2017-10-18 01:28:38 +02:00
Bertocq
9a396fe634 Fix record_voter_participation and usage on specs 2017-10-07 10:48:24 +02:00
rgarcia
007bcb7ec7 fixes specs 2017-10-04 17:46:25 +02:00
Bertocq
85087b0bea Check officer value for Poll Voter created from officing panel 2017-10-04 01:09:51 +02:00
rgarcia
60fb142fff adds comprehensive validation specs 2017-10-02 13:39:55 +02:00
rgarcia
b623422805 allows users to change their vote without refreshing the page 2017-10-02 13:14:58 +02:00
Juanjo Bazán
51be80eedc removes answer <-> voter association 2017-01-27 11:59:37 +01:00
Juanjo Bazán
b13a76963a records participation of user via web
answering a poll question creates a voter with the user data to record
participation in question’s poll
2017-01-25 14:21:03 +01:00
Juanjo Bazán
8d8d822323 adds answer spec 2017-01-25 13:04:26 +01:00