Commit Graph

1233 Commits

Author SHA1 Message Date
Javi Martín
c0edd1b227 Allow SDGs to get all their related contents
Note we cannot directly get all related contents through SQL because
related contents are spread through different tables.
2020-12-04 18:27:49 +01:00
Javi Martín
42699275a1 Add relations between relatable models and SDGs
Note we cannot directly get all related SDGs through SQL because they're
spread through different tables.
2020-12-04 18:27:49 +01:00
Javi Martín
1740e0ba66 Add SDG::Relation model 2020-12-04 18:27:49 +01:00
Senén Rodero Rodríguez
c39c7213c7 Add SDG target model
and its relation with the SDG goal model.

Add comparable module be able to sort collections of targets
by code attribute.

Co-Authored-By: Javi Martín <35156+javierm@users.noreply.github.com>
2020-12-04 15:15:32 +01:00
taitus
609e51bbf3 Include new type 'sdg' on settings 2020-12-03 18:00:10 +01:00
Javi Martín
d5ee1ff89c Add method to easily access a goal by code
Similar to what we do with settings, only for settings we return the
value of the setting (which is what we're going to need most of the
time), and here we return the object.
2020-12-02 12:59:17 +01:00
Senén Rodero Rodríguez
cbe84450ac Add Goals seeds and translations
Extracted from the official United Nations Sustainable Development
Goals website [1].

[1] https://www.un.org/sustainabledevelopment/sustainable-development-goals/
2020-12-02 12:38:03 +01:00
Javi Martín
428644cd3e Add SDG goal model
Since data for this model (title and description) is not generated in
CONSUL but by the United Nations, we aren't storing it in the database
but in our YAML translation files.

The reasoning is as follows. Suppose that, a few months after CONSUL
gets SDG support, a new language is added to CONSUL.

With YAML files, getting the texts in the new language would mean
updating CONSUL to include the new language.

But if we store these texts in the database, it means we have to update
the databases of all existing CONSUL installations, either each
installation by themselves (duplicating efforts) or running a rake task
(which we would have to write each time).

So we believe using translations works better in this case.

We're still storing records in the database with the code, so they can
be easily referenced via `has_many` or `has_many :through` associations.
2020-12-02 12:13:02 +01:00
Senén Rodero Rodríguez
ac6260a2ef Mock remote census responses in tests using XML
By using real XML responses developers will be able to understand better
how the integration works (the data flow), and the correspondency between
`remote_census` settings and their place at a real XML response.

As `stubbed_responses` methods were removed from the model layer now the
stubbing part should be managed from the test environment code so also
added a new helper module `RemoteCensusSetup` that can be used anywhere
where we need to call the web service.

Co-Authored-By: Javi Martín <javim@elretirao.net>
2020-11-02 11:42:39 +01:00
Javi Martín
8257a82f2f Use date objects when building office residences
Since the interface to select this date uses the classic multi-field
interface for day, month and year, we're transforming it into a date in
the Office::Residence initializer.

However, the factory to build an office residence does not assign the
paremeters in the initializer but using the `date_of_birth=` method, so
when doing so we need to use a date instead of a string.
2020-11-02 11:42:39 +01:00
Senén Rodero Rodríguez
06dcbd699c Extract block to configure remote census in tests
Co-Authored-By: Javi Martín <javim@elretirao.net>
2020-11-02 11:42:39 +01:00
Javi Martín
cef54b4e59 Add and apply RepeatedExampleGroupDescription rule
This rule was added in rubocop-rspec 1.38.0. Thanks to it we've detected
we had one method tested in two different places.
2020-10-25 14:23:53 +01:00
Javi Martín
f4a6830a1b Add and appy RSpec/RepeatedExampleGroupBody rule
This rule is available since rubocop-rspec 1.38.0. Thanks to it, we've
found out we were testing topics behave like notifiable twice.
2020-10-25 14:23:53 +01:00
Javi Martín
b6a095ced7 Add and apply RSpec/EmptyLineAfterExample rule
It was introduced in rubocop-rspec 1.36.0. We were already applying it
almost everywhere.
2020-10-25 14:23:53 +01:00
Javi Martín
4865cbeb59 Add and apply RSpec/ContextMethod rubocop rule
This rule is available since rubocop-rspec 1.36.0. We were already
applying it in most cases.
2020-10-25 14:23:53 +01:00
anks
9bd012f83c Hide retired proposals from related content proposals (Merge pull request #4196)
Co-authored-by: Anna Anks Nowak <matisnape@users.noreply.github.com>
2020-10-25 14:22:41 +01:00
Javi Martín
b27d0a8e92 Add and apply ConstantDefinitionInBlock rule
This rule was added in Rubocop 0.91.0. A similar rule named
LeakyConstantDeclaration was added in rubocop-rspec 1.34.0.

Note using the FILENAMES constant did not result in an offense using the
ConstantDefinitionInBlock rule but did result in an offense using the
LeakyConstantDeclaration rule. I've simplified the code to get rid of
the constant; not sure why we were adding a constant with `||=` in the
middle of a spec.
2020-10-23 12:04:22 +02:00
Javi Martín
14df74fed7 Add collaborative legislation summary again
It was removed in commit 128a8164 because we hadn't reviewed it nor
tested it properly. We're now adding it again, fixing the issues we've
found while reviewing.
2020-09-08 13:32:08 +02:00
taitus
85ffc70f5f Apply Legislation Process default colors to dev seeds
Legislation Processes created through the admin form were getting the default color.
However, Legislation processes created by other means (like the `db:dev_seed` rake task) were not getting these default values.

This feature was originally implemented when we were using Rails 4.
With Rails 5, we can provide default values to all new Legislation processes
and simplify the code at the same time thanks to its `attribute` method.

Related commit:
https://github.com/consul/consul/pull/4080/commits/0b83be6
2020-09-08 10:07:43 +02:00
Javi Martín
0b83be6837 Apply banner default colors to dev seeds
Banners created through the admin form were getting the default color.
However, banners created by other means (like the `db:dev_seed` rake
task) were not getting these default values.

This feature was originally implemented when we were using Rails 4.
With Rails 5, we can provide default values to all new banners and
simplify the code at the same time thanks to its `attribute` method.

Now, when creating a new banner, instead of getting a blank space, we
get an empty line with the banner's default background color, which most
users won't know what it's about until they fill in the banner's title.
So we're not displaying the content of the banner when it's empty,
thanks to the `:empty` CSS pseudoclass.
2020-08-12 17:16:11 +02:00
Javi Martín
096f546c24 Make sure users only vote once in the same poll
When skipping verification, we cannot apply the validation rule saying
the document number and document type must be unique, because they'll be
`nil` in many cases. So we were skipping the rule, but that makes it
possible for the same user to vote several times (for instance, once in
a booth and once via web).

So we're changing the scope of the uniqueness rule: instead of being
unique per document number, voters are unique per user. The reason we
made them unique per document number was that back in commit 900563e3
(when we added the rule), we hadn't added the relation between users and
poll voters yet.
2020-08-07 13:40:25 +02:00
decabeza
4367b2054a Allow voting when skip verification is enabled 2020-08-07 11:52:24 +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
Ziyan Junaideen
1e3e8c1304 Add approval voting to budgets
Co-Authored-By: Javi Martín <javim@elretirao.net>
2020-08-06 12:38:18 +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
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
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
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
a20622e2fa Allow remote translation tests to be run offline
The method `available_locales` in
`RemoteTranslations::Microsoft::AvailableLocales` needs to execute a
request to an external server in order to work, meaning it will fail if
the machine its executed on doesn't have an internet connection.

So we're stubbing the method in the tests using it.
2020-04-28 15:45:50 +02:00
Javi Martín
e7fcca9b47 Check remote translations locales at runtime
We were evaluating its value when the server starts. On production
enviroments, that could mean weeks or months before the available
locales are checked again, and so it would be possible to use a list
which is no longer in sync with the list provided by microsoft.
2020-04-28 15:45:32 +02:00
Javi Martín
12774c7484 Replace attribute_changed? in after callbacks
This method is deprecated in Rails 5.1 because its behavior will be
different in `before` and `after` callbacks.

We're replacing the deprecated `attribute_changed?` and `attribute_was`
methods with `saved_change_to_attribute?` and
`attribute_before_last_save` during `after_save` callbacks.

https://github.com/rails/rails/pull/32835/
2020-04-24 15:43:54 +02:00
Javi Martín
1118c732f1 Bump acts-as-taggable-on to 6.0.0
Rails 5.1 introduced certain changes in the way a record is touched when
the counter cache option is enabled in a belongs to association.

We need to upgrade acts-as-taggable-on so it keeps changing the
`updated_at` attribute when a new tag is added to a record.

Note we now need to reload the records in some cases to get the
`context_tag_list` method to return what we expect. Methods like
`context_tags` however work properly with no need to reload the record.
2020-04-23 18:49:43 +02:00
Paweł Świątkowski
d99875cde2 Get search dictionary based on I18n.default_locale (merge pull request #3856)
Implementation tries to be open for further extensions, such as deciding on
search dictionary based on configuration option or by locale set for
given user.
2020-04-12 14:22:36 +02:00
Javi Martín
958d373247 Fix duplicate records in investments by tag
When an investment had been assigned a user tag and a valuation tag with
the same name, it appeared twice when filtering by tag.

This is because by design, in order to provide compatibility with scopes
using "select" or "distinct", the method `tagged_with` doesn't select
unique records.

Forcing the query to return unique records solves the issue.
2020-04-09 21:09:28 +02:00
Andrew Sims
95c82d8777 Changes following PR review
* Internationalisation for admin fields
* Correct typos
* Additional tests
* Replace ternary with if-then statement
2020-04-09 07:11:53 +10:00
Cassiano Sampaio
1e4f539104 Add title to differentiate signature sheets 2020-04-09 07:11:52 +10:00
Andy Sims
74fbde09f1 Support creates follow (Merge pull request #3895)
* Supporting a proposal will create a follow relationship
* Only followers receive notifications
2020-04-06 15:26:47 +02:00
Srinandan Pai
14106ce800 Issue-1794: Allow to hide proposals created by admin (#3884) 2020-03-26 15:07:55 +01:00
Javier Martín
9d750dd399 Merge pull request #3917 from rockandror/improve-remote-translations
Improve remote translations
2020-02-26 17:29:14 +01:00
taitus
d853366d38 Add RemoteTranslation validations
- Validate that locale is a valid locale for RemoteTranslation Client.
- RemoteTranslation can only be created for resources that do not have the requested
language translated
2020-02-26 16:47:13 +01:00
Javier Martín
14b1f1b4d3 Merge pull request #3911 from rockandror/dashboard-email
Improve Dashboard emails
2020-02-19 19:06:53 +01:00
taitus
5608424510 Rename all ocurrences from this Setting to new key 2020-02-17 16:30:38 +01:00
Javi Martín
f223b3945f Fix filters for investments without admin/valuator
These filters were only returning investments with valuation open, but
we don't want to do that since the time we changed the interface in
order to allow users to apply several filters at the same time.
2020-02-06 14:33:36 +01:00
Javi Martín
12eee3f5d1 Avoid invalid random titles in dashboard specs
Generating a random title with `Faker::Lorem.sentence` sometimes caused
validation errors in tests because the generated sentence was more than
80 characters long.
2019-11-19 23:32:12 +01:00
Javi Martín
ed6a4a1b95 Don't generate stats for budget polls
The link to show stats for these polls is nowhere to be seen in the
application, and these stats are included in the budget stats, so it
makes sense to restrict access to them.
2019-11-09 19:34:24 +01:00
Javi Martín
f8e6e98d3a Define stats and result permissions with scopes
When defining abilities, scopes cover more cases because they can be
used to check permissions for a record and to filter a collection. Ruby
blocks can only be used to check permissions for a record.

Note the `Budget::Phase.kind_or_later` name sounds funny, probably
because we use the word "phase" for both an an attribute in the budgets
table and an object associated with the budget, and so naming methods
for a budget phase is a bit tricky.
2019-11-09 19:34:21 +01:00
Javi Martín
2029d7baa5 Limit admin access to poll stats and results
There's no reason to allow administrators to check stats and results for
a poll when it isn't finished or when results and stats are not enabled.

Now admins have the same permissions as everyone else.
2019-11-09 19:32:54 +01:00
Javi Martín
8dbad5c3d2 Simplify tests for stats and results permissions
Now these tests look like the other ability tests.
2019-11-09 15:18:58 +01:00
Javi Martín
ea2aeab383 Apply rubocop Rails/FindBy rule everywhere
We didn't detect these cases because by default the rule only searches
for offenses in `app/models/`.
2019-11-08 19:15:04 +01:00