Commit Graph

15065 Commits

Author SHA1 Message Date
Javi Martín
a38cdb4df3 Remove unnecessary safe navigation operator
The heading is used with `find_by_slug_or_id`, which raises an exception
if it isn't found, so executing `@heading.group` after it does not need
the safe navigation operator.
2020-07-28 13:06:31 +02:00
Javi Martín
a9900e3f27 Remove duplication calculating insufficient funds
We were using the same logic twice.

I've moved the logic to the Ballot model, which to me is a more natural
place to calculate whether there's enough money left than the Investment
model. After all, the remaining money is in the ballot, and not in the
investment.
2020-07-28 13:06:31 +02:00
Javi Martín
a32c0f8154 Remove unused parameter
The `refresh_ballots` partial ignores the `investment` parameter
completely; instead, it iterates over the investments in the
`@investments` instance variable.
2020-07-28 13:06:31 +02:00
Javi Martín
aff213b0ef Remove redundant calls to load resources
We already load the budget and the ballot in `before_action` calls, so
we don't have to load them again.
2020-07-28 13:06:30 +02:00
Javi Martín
7ce2d8b7eb Remove unused methods
One method was calling `reason_for_not_being_ballotable_by` passing just
one parameter instead of two.

The other method was calling the method `amount_spent`, which does not
exist in the Budget class.

So both methods would make the application crash if they were called.
Luckily, they aren't, so the application doesn't crash.
2020-07-27 16:49:58 +02:00
Javi Martín
7c425c00aa Remove unnecessary condition
The ballot is used in the previous lines, so there's no point checking
whether it's present.
2020-07-27 16:49:58 +02:00
Javier Martín
792ebdb175 Merge pull request #4068 from consul/bug_delete_users
Do not delete users when deleting legislation answers
2020-07-27 14:55:37 +02:00
Julian Herrero
46c78fc3ef Do not delete users when deleting legislation answers
When a legislation process is deleted, everything related will be
deleted, including the answers. This `dependent: :destroy` was causing
that users accounts were being accidentally deleted.
2020-07-27 14:32:22 +02:00
Javier Martín
c03ada579d Merge pull request #4061 from consul/ballot_race_condition
Fix race condition with ballot lines
2020-07-27 13:01:37 +02:00
Javier Martín
54095d7928 Merge pull request #4064 from consul/remove_google_plus
Remove Google plus share button
2020-07-21 13:03:59 +02:00
Javier Martín
7ee3eecae3 Merge pull request #3980 from consul/erased-users
Filter erased users and show erase reason in admin
2020-07-17 23:00:12 +02:00
decabeza
f72f255d15 Filter erased users and show erase reason in admin 2020-07-17 22:21:52 +02:00
decabeza
e3539544a8 Remove google plus share button 2020-07-17 16:24:20 +02:00
Javier Martín
74627641b3 Merge pull request #4034 from consul/help_page_processes
Add processes feature info section in the help page
2020-07-16 23:32:15 +02:00
Javier Martín
53f054ff69 Merge pull request #4050 from consul/stemming_spec
Improve test checking order by relevance
2020-07-14 18:12:21 +02:00
Javi Martín
d9eeb1ad15 Improve test checking order by relevance
The test wasn't working when postgres used the English dictionary
because in English the word "what" was ignored (or, at least, not given
enough relevance) while searching. When we wrote the test, it passed
because back then we always used the Spanish dictionary. However, when
we switched to a dictionary based on the default locale (in commit
d99875cd), we had to force this test to keep using the Spanish
dictionary.

Using the Spanish dictionary in a test where all texts are in English is
strange to say the least ;). So here we're making the test a bit easier
to understand.

Since now we're only using the `:spanish_search` tag in one test, I've
decided to remove it and simply add it to that test's setup.
2020-07-14 14:49:39 +02:00
Javier Martín
ae0fb131d4 Merge pull request #4028 from consul/rails5.2_compatibility
Add Rails 5.2 compatibility
2020-07-14 13:40:30 +02:00
Javi Martín
9318c4f1e9 Bump pg_search from 2.0.1 to 2.3.0
Using pg_search 2.0.1 with Rails 5.2 results in deprecation warnings:

DEPRECATION WARNING: Dangerous query method (method whose arguments used
as raw SQL) called with non-attribute argument(s):
"pg_search_978c2f8941354cf552831b.rank DESC, \"tags\".\"id\" ASC".
Non-attribute arguments will be disallowed in Rails 6.0. This method
should not be called with user-provided values, such as request
parameters or model attributes. Known-safe values can be passed by
wrapping them in Arel.sql().

We're not upgrading to the latest pg_search because it only supports
ActiveRecord >= 5.2.
2020-07-14 13:16:08 +02:00
Javi Martín
17f442c723 Extract method to get a few random records
In Ruby 5.2, we get a warning when using the "RANDOM()" function:

DEPRECATION WARNING: Dangerous query method (method whose arguments are
used as raw SQL) called with non-attribute argument(s): "RANDOM()".
Non-attribute arguments will be disallowed in Rails 6.0. This method
should not be called with user-provided values, such as request
parameters or model attributes. Known-safe values can be passed by
wrapping them in Arel.sql().

This warning doesn't make much sense, though, since RANDOM() is a common
function which is not dangerous at all. However, since the warning is
annoying, we'll probably have to find a way to deal with it.

So I'm extracting all our RANDOM() usages into a method. This way we'll
only have to change one method to avoid this warning.

I've chosen `sample` because it's similar to Ruby's Array#sample, and
because `order_by_random` would be confusing if we consider we already
have a method called `sort_by_random`.
2020-07-14 12:32:14 +02:00
Javi Martín
057679248f Use be_not_found instead of be_missing
We were getting a deprecation message in Rails 5.2:

The missing? predicate is deprecated and will be removed in Rails 6.0.
Please use not_found? as provided by Rack::Response::Helpers
2020-07-14 12:32:14 +02:00
Javi Martín
1b34c061bb Use where.not instead of where(NOT IN)
This way we simplify the code a bit and reduce our usage of raw SQL.
2020-07-14 12:32:12 +02:00
Javi Martín
d2d517059d Fix race condition with ballot lines
With two concurrent requests, it's possible to create two ballot lines
when only one of them should be created.

The reason is the code validating the line is not thread safe:

```
if ballot.amount_available(investment.heading) < investment.price.to_i
  errors.add(:money, "insufficient funds")
end
```

If the second request executes this code after the first request has
executed it but before the first request has saved the record to the
database, both records will pass this validation and both will be saved
to the database.

So we need to introduce a lock. Now when the second request tries to
lock the ballot, it finds it's already locked by the first request, and
will wait for the transaction of the first request to finish before
checking whether there are sufficient funds.

Note we need to disable transactions during the test; otherwise the
second thread will wait for the first one to finish.

Also note that we need to update a couple of tests because records are
reloaded when they're locked.

In one case, reloading the ballot causes `ballot.user` to be `nil`,
since the user is hidden. So we hide the user after creating all its
associated records (which is the scenario that would take place in real
life).

In the other case, reloading the ballot causes `ballot.user` to reload
as well. So we need to reload the user object used in the test too so it
gets the updates done on `ballot.user`.

I haven't been able to reproduce this behavior in a system test. The
following test works with Rails 5.0, but it stopped working when we
moved to system tests in commit 9427f014. After that commit, for reasons
I haven't been able to debug (reintroducing truncation with
DatabaseClaner didn't seem to affect this test, and neither did
increasing the number of threads in Puma), the two AJAX requests
executed here are no longer simultaneous; the second request waits for
the first one to finish.

scenario "Race conditions with simultaneous requests", :js do
  allow_any_instance_of(Budget::Ballot::Line).to receive(:check_sufficient_funds) do |object|
    allow(object).to receive(:check_sufficient_funds).and_call_original
    object.check_sufficient_funds
    sleep 0.3
  end

  ["First", "Second"].each do |title|
    create(:budget_investment, :selected,
      heading: california,
      price:   california.price,
      title:   title
    )
  end

  login_as(user)
  visit budget_investments_path(budget, heading_id: california.id)

  within(".budget-investment", text: "First") { click_link "Vote" }
  within(".budget-investment", text: "Second") { click_link "Vote" }

  expect(page).to have_link "Remove vote"
  expect(Budget::Ballot::Line.count).to eq 1
end
2020-07-12 22:11:40 +02:00
Javier Martín
226000214d Merge pull request #4054 from consul/allow_delete_polls_with_videos
Allow deleting polls with answers including videos
2020-07-09 13:50:35 +02:00
Julian Herrero
89962ba61a Allow deleting polls with answers including videos
If a poll has a question with an answer containing a related video,
an error was raised because the poll ID was referenced in another
table.
2020-07-09 13:39:15 +02:00
Javi Martín
f427c757ba Use hash conditions instead of SQL's IN
This is what we're doing in most places.
2020-07-08 18:34:58 +02:00
Javi Martín
f6351819fa Simplify SQL using DISTINCT
Using `pluck("DISTINCT")` was raising a warning in Rails 5.2:

DEPRECATION WARNING: Dangerous query method (method whose arguments are
used as raw SQL) called with non-attribute argument(s): "DISTINCT
taggings.tag_id". Non-attribute arguments will be disallowed in Rails
6.0. This method should not be called with user-provided values, such as
request parameters or model attributes. Known-safe values can be passed
by wrapping them in Arel.sql().

Since there was only one other use of distinct, I've decided to change
both of them in the same commit, even if the second one wasn't raising a
warning.
2020-07-08 18:34:58 +02:00
Javi Martín
d7d421b88f Rename columns with a slash in their names
These columns were causing Rails 5.2 to throw a warning when ordering by
them, as if they weren't valid column names:

DEPRECATION WARNING: Dangerous query method (method whose arguments are
used as raw SQL) called with non-attribute argument(s):
:"budget/investments_count". Non-attribute arguments will be disallowed
in Rails 6.0. This method should not be called with user-provided
values, such as request parameters or model attributes. Known-safe
values can be passed by wrapping them in Arel.sql().

This change also makes their names consistent with the rest of our
tables and columns.
2020-07-08 18:34:58 +02:00
Javi Martín
6fd9a286d7 Don't access the database in after_initialize
Rails 5.2 crashes in the `db:create` task because it tries to run the
`after_initialize` block before the database is created.

The easiest way to solve it is to move the code out of the initializer
and calculate the API type definitions on demand. Note results are still
cached using a class instance variable (not to be confused with a class
variable), and so once definitions are obtained, they will remain
constant until the application is restarted, even in the development
environment.
2020-07-08 18:34:58 +02:00
Javi Martín
9837b1ab74 Remove tasks to upgrade to version 1.1
These tasks are not needed for new installations, and in existing
installations they've already been executed when upgrading to version
1.1.

One of them also raises a warning in Rails 5.2:

DEPRECATION WARNING: Dangerous query method (method whose arguments are
used as raw SQL) called with non-attribute argument(s): "MIN(id) as id".
Non-attribute arguments will be disallowed in Rails 6.0. This method
should not be called with user-provided values, such as request
parameters or model attributes. Known-safe values can be passed by
wrapping them in Arel.sql()
2020-07-08 18:34:58 +02:00
Javier Martín
8059c55fef Merge pull request #3978 from consul/ruby2.5
Upgrade Ruby to 2.5.8
2020-07-08 18:28:34 +02:00
Javi Martín
727e743887 Remove autoload trigger
I'm not sure why the code didn't work without this line, but it doesn't
seem to be necessary anymore (maybe after upgrading Ruby or Rails?).

I'm removing it now because... why not now? :) The Ruby interpreter is
raising a warning due to this line, and in Ruby 2.5 constant lookup has
changed slightly (although I don't think this line is affected by that
change).

Note about the change in the Setting model: Ruby actually ignores return
values in setter methods, so the line isn't necessary.
2020-07-08 12:39:25 +02:00
Javi Martín
4ba4289006 Upgrade Ruby to 2.5.8 2020-07-08 12:39:25 +02:00
Javier Martín
889064e23b Merge pull request #3948 from consul/fix-bug-flagging-legislation-proposals
Fix bug flagging legislation proposals
2020-07-08 12:34:51 +02:00
Javi Martín
a5f1245b7e Extract partial to refresh flag actions
Now that we're rendering `shared/flag_actions` everywhere, we can use
the same code in all cases.
2020-07-08 11:58:03 +02:00
Javi Martín
4f30720593 Fix flagging/unflagging in the admin section
We weren't adding the HTML id our JavaScript expects, and so the page
didn't update the flag element.
2020-07-07 23:39:21 +02:00
Javi Martín
31b65679c3 Extract partial to render flag actions
The main obstacle to extract this partial was probably the paths for the
flag and unflag actions.

Now that we use Rails 5.1 `resolve` method to handle nested resources,
we can use `polymorphic_path`.

Also note the code is a bit ugly because comments render a divider. We
should probably use a CSS border instead.

Co-Authored-By: taitus <sebastia.roig@gmail.com>
2020-07-07 23:39:21 +02:00
Javi Martín
bd7beed8a1 Remove no longer necessary flag/unflag HTML IDs
They were added in commit 015fe704 because we used them in the specs,
but we don't use them anymore and they make the code hard to read.
2020-07-07 23:39:21 +02:00
Javi Martín
014ccd8374 Use shared specs to flag comments 2020-07-07 23:39:21 +02:00
Javi Martín
3c27df592e Remove test for flagging poll comments
This feature hasn't been implemented and there are no plans to implement
it in the near future.
2020-07-07 23:39:21 +02:00
volcov
09fd3ab44a Fix legislation proposals flag actions
We were treating legislation proposals as if they were proposals,
omitting the "legislation" namespace, and so we were flagging/unflagging
proposals when we wanted to flag/unflag a legislation proposal.
2020-07-07 23:39:21 +02:00
Javi Martín
0d7c2c7a7c Simplify rendering flag actions
The `respond_with` method is no longer part of Rails (it's now part of
the responders gem) and we barely use it. Using a template forced us to
use different criteria for different controllers.

This change will also make it easier to fix the flag/unflag actions for
legislation proposals. With the old code, we would have to add another
condition for the legislation/proposals controller.
2020-07-07 23:39:19 +02:00
Javi Martín
91da038b27 Extract shared tests to flag/unflag a record 2020-07-07 22:56:17 +02:00
Javi Martín
9937e94fcd Fix flagging debates and comments with AJAX
We weren't using `foundation()` in these cases, so after flagging a
debate or a comment, we had to reload the page before we could unflag
it.

We're also adding a test for the fix in commit ea85059d. This test shows
it's necessary to filter the elements with JavaSctipt using `first()` if
we want the same code to work with comments.

Co-Authored-By: taitus <sebastia.roig@gmail.com>
2020-07-07 22:56:17 +02:00
Javier Martín
328ec5e25f Merge pull request #4001 from rockandror/check-session-locale
Discard session[:locale] when is not valid
2020-06-25 22:00:37 +02:00
taitus
3b5a96bdfd Refactor set_locale
Add new current_locale method to simplify logic
2020-06-25 19:53:48 +02:00
taitus
ee5ac25cb1 Improve set_locale
We discard session[:locale] as valid locale when it is no longer include in
the :available_locales
2020-06-25 19:45:56 +02:00
Javi Martín
002e9239d0 Simplify code involving Globalize.locale
We don't need to set this value. In commit f2ef27d3 I made a mistake
thinking `Globalize.locale` and `I18n.locale` should always be in sync,
but they're actually automatically in sync when `Globalize.locale` is
`nil`.

So the best way to avoid any issues is not to assign `Globalize.locale`,
and use `Globalize.with_locale` where necessary instead.
2020-06-25 19:37:57 +02:00
Javier Martín
11a1fa1b1d Merge pull request #4042 from consul/dependabot/bundler/rack-2.2.3
[Security] Bump rack from 2.2.2 to 2.2.3
2020-06-23 19:06:33 +02:00
Javier Martín
e7d557a95c Merge pull request #4004 from consul/shared-banner
Move conditional into shared banner partial
2020-06-18 23:39:32 +02:00
Javier Martín
3261a0c02f Merge pull request #4046 from consul/poll-documents
Remove unused document section on polls
2020-06-18 12:33:12 +02:00