Commit Graph

1350 Commits

Author SHA1 Message Date
Javi Martín
7212657c02 Remove Paperclip and use just Active Storage 2022-02-23 18:43:48 +01:00
Javi Martín
e0e35298d5 Use Active Storage to handle cached attachments
This fixes a few issues we've had for years.

First, when attaching an image and then sending a form with validation
errors, the image preview would not be rendered when the form was
displayed once again. Now it's rendered as expected.

Second, when attaching an image, removing it, and attaching a new
one, browsers were displaying the image preview of the first one. That's
because Paperclip generated the same URL from both files (as they both
had the same hash data and prefix). Browsers usually cache images and
render the cached image when getting the same URL.

Since now we're storing each image in a different Blob, the images have
different URLs and so the preview of the second one is correctly
displayed.

Finally, when users downloaded a document, they were getting files with
a very long hexadecimal hash as filename. Now they get the original
filename.
2022-02-23 18:21:38 +01:00
Javi Martín
4b42a68b6a Use the vote action to vote featured proposals
The action and the views were almost identical, with the supports
progress and the HTML classes of the success message element being the
only exceptions; we can use CSS for the styles instead.
2022-02-21 18:47:13 +01:00
Javi Martín
b98244afd9 Remove votes query optimizations
Just like we did in commit 0214184b2d for investments, we're removing
some possible optimizations (we don't have any benchmarks proving they
affect performance at all) in order to simplify the code.

The investement votes component `delegate` code was accidentally left
but isn't used since commit 0214184b2, so we're removing it now that
we're removing the `voted_for?` helper method.
2022-02-21 18:47:13 +01:00
taitus
2bef215fc6 Add method to generate subscriptions_token
Note that we only update a user with a new token if the user has not
yet been assigned one.
2022-01-21 18:58:38 +01:00
Javi Martín
c34fc7f0b8 Remove unneeded lines restricting permissions
In the past, users had permission to edit their own legislation
proposals. However, that changed in commit ebfa3fb01, where we replaced
the `can` method with `cannot`.

An easier way to remove this permission is to simply remove the whole
statement, since by default users don't have permissions to do anything.

We're also adding a test checking users can't edit their own legislation
proposals, since it was missing.
2021-12-30 18:25:45 +01:00
Javi Martín
76555495f6 Hide legislation proposals when blocking a user
We're also updating the notice messages to specify all contents have
been hidden (not just debates).
2021-12-30 15:50:03 +01:00
Javi Martín
021fef07b6 Make action names to block and hide more clear
The `hide` action was calling the `block` method while the `soft_block`
action was calling the `hide` method.

Combined with the fact that we also have a `block` permission which is
used in `ModerateActions` the logic was hard to follow.
2021-12-30 15:50:03 +01:00
Carlos Iniesta
49edd6a9b1 Add soft block button in moderation user view 2021-12-30 15:50:03 +01:00
Javi Martín
4c8dfb6695 Use just one action to hide users
Other than removing a redundant action, we're fixing two bugs when
blocking an author using the links in the public views:

* We were always redirecting to the debates index, even if we blocked
  the author of a proposal or an investment
* We weren't showing any kind of success message
2021-12-30 15:50:02 +01:00
Javi Martín
d6b85a038c Allow regular expressions in postal codes
Programmers can take advantage of this feature when defining custom
default settings. And, since many CONSUL installations had custom
changes in the `custom/verification/residence.rb` model and those
changes might use regular expressions, we're making it easier to migrate
that code to the new system to define valid postal codes.

We aren't documenting this feature in the description in the admin
section because most administrators don't know what regular expressions
are.

Note that, in order to simplify the setting, we already define the `/\A`
and `\Z/` characters. So, if the custom code had something like
`postal_code =~ /^280/`, the setting would have to be "280*" (without
the quotes) or, in order to comply with a length validation,
"280[0-9]{2}" (without the quotes).
2021-12-16 23:58:40 +01:00
Javi Martín
a79bbac894 Fix invalid postal code message
We were using the word "registered" in English as an equivalent of the
Spanish word "empadronado". However, the term "registered" is very
confusing because it might be understood as being registered in the
CONSUL website.

In the message, we're saying "cannot participate" in order to make the
message consistent with the message regarding the required age.
2021-12-16 23:58:36 +01:00
Javi Martín
c2e95f6b86 Allow any postal code by default
Due to the way Madrid handled postal code validations (see issue 533),
by default we were requiring everyone to validate against the local
census *and* to specify valid postal codes.

This could be useful in some cases, but in other cases, the census
validation will be enough and there'll be no need to manually define the
valid postal codes. Besides, some CONSUL installations are used in
organizations or political parties where the postal code validation
doesn't make sense.
2021-12-16 13:57:00 +01:00
Javi Martín
35e0477e03 Ignore trailing spaces in postal codes verification
This way both administrators and regular citizens have a certain margin
of error when entering the postal codes.
2021-12-16 13:57:00 +01:00
Javi Martín
5cc10cbadf Allow dashes in postal codes
In some countries, postal codes are defined with a dash in the middle,
so we're using a colon to define ranges instead. We could also use two
dots, like in Ruby ranges, but IMHO this would cause typos since people
would enter codes separated with three dots or just one dot.
2021-12-16 13:57:00 +01:00
Javi Martín
b8b4336266 Compare postal codes with strings and not integers
We can now get rid of the rule forcing a length of 5 digits, which
doesn't apply to all countries.
2021-12-16 13:57:00 +01:00
Javi Martín
006269a94b Simplify tests validating postal codes
We weren't using the `be_valid` matcher because we had errors in the
census data.

Removing the `before_validation` callback and using a method to get the
census data instead allows us to stub the `census_data` method in the
tests, and so we can use the `be_valid` matcher instead of calling
`valid?` manually and then checking for errors.
2021-12-16 13:57:00 +01:00
Laura Concepción Rodríguez
f4512b2117 Redefine postal code verification methods to use setting config parameter 2021-12-16 13:57:00 +01:00
Javi Martín
e612705463 Make investment filters easier to understand
So now:

* In the first few phases, no filters are shown (just like before)
* During the valuation phase, we show "Active" and "Unfeasible"
* During the final voting, we show "Active" (which now refers to the
  selected investments), "Not selected for the final voting" and
  "Unfeasible"
* When the budget is finished, we show "Winners", "Not selected for the
  final voting" and "Unfeasible"

Now each investment is shown in one (and only one) of the filters
(except when the budget is finished; in this case we don't show selected
investments which didn't win), and we remove the confusing "Not
unfeasible" filter by only showing it during the valuation phase (before
filters are selected) and renaming it to "Active". We also rearrange the
filters so the default one for each phase is shown first.

The idea of using the "Active" text for investments which can be
selected during the selection phase and voted during the final voting is
experimental. Right now, for simplicity, since we assume filters will
always use the same text, we're removing the "Active" filter when the
budget is finished, since having both "Winners" and "Active" filters
would be confusing.
2021-11-16 19:18:25 +01:00
Senén Rodero Rodríguez
30afb64bac Do not consider attributes using the :unless option as required
Remove some of the factories introduced in commit 66334b5 as now we do
not need them anymore.

Co-Authored-By: Javi Martín <35156+javierm@users.noreply.github.com>
2021-11-05 16:40:36 +01:00
Javi Martín
28a7aea1c0 Don't show investment filters before valuation
Before the "valuating" phase, all investments have undecided feasibility
and none have been selected, so the filters would return no results
(except the "not_unfeasible" one, which would return everything).
2021-10-29 15:01:37 +02:00
Javi Martín
56ac154d1f Add feasible investments filter again
We removed it in commit c322b2c4a because it was hard to know the
difference between "Feasible" and "Not unfeasible". We're renaming the
"Not unfeasible" filter instead.

We're also moving the "selected" filter so it appears before the
"unselected" filter, just like the "feasible" filter appears before the
"unfeasible" filter.
2021-10-29 14:53:33 +02:00
Javi Martín
1bfcfca2e2 Move method to get available filters to the model
We're naming the method `investments_filters`, with the word
"investments" in plural, to be consistent with the method
`investments_orders`.
2021-10-29 14:53:33 +02:00
Javi Martín
7d818e24ca Fix condition to show the "see results" link
This condition was obsolete since we introduced the `results_enabled`
field in commit 4f4dc2c2a.
2021-10-25 18:01:47 +02:00
Javi Martín
0a14337580 Disable calculating winners during balloting
Calculating winners before the balloting is over is useless (results
aren't published at that point) and can lead to the wrong results since
users are still voting and results might change.

And we were showing the button to calculate winners even when a budget
had finished. However, in this case the action to calculate winners did
nothing, which resulted in administrators seeing nothing happened after
pressing the button.
2021-10-25 18:01:47 +02:00
decabeza
25aa77c4c3 Show polls with the user's geozone first 2021-10-18 13:07:16 +02:00
Javi Martín
6d26ce57cb Show answers with attachments in additional info
We weren't showing the details of answers without a description, even if
they had images, videos or documents. Some users found that behavior
unexpected since the description isn't a mandatory field and so they
left it blank, but they added images to that answer and they didn't
appear on the poll page.

Note we had a condition not to show the title of an answer when it had
no description. I think that condition was redundant because answers
without a description weren't loaded in the first place. Anyway, that
condition doesn't make sense anymore because we're displaying answers
with images but no description.
2021-10-06 16:52:23 +02:00
Javi Martín
480bb8cd55 Remove link column in dashboard actions
This column wasn't used in any released Consul version since it was only
used during development. For the same reason, the task to migrate the
information in the `link` column to the `links` table isn't needed
either.
2021-10-06 14:13:44 +02:00
Javi Martín
3cd4f3827e Hide what users are following unless they allow it
It could be argued that seeing which proposals a user follows is a good
indicator of which proposals a user has supported, since we're
automatically creating follows for supported proposals since commit
74fbde09f. So now, we're extending the `public_interests` funcionality,
so it only shows elements users are following if they've enabled it.

This is an improvement over using the `public_activity` attribute in two
ways:

* The `public_interests` attribute is disabled by default, so by default
  other users won't be able to see what a user is following
* Who has created proposals/debates/investments/comments is public
  information, while who is following which elements is not; so enabling
  `public_activity` shouldn't imply potentially private information should
  be displayed as well

We've considered removing the `public_interests` attribute completely
and just hiding the "following" page for everyone except its owner, but
keeping it provides more compatibility with existing installations.
2021-10-05 14:43:09 +02:00
Javi Martín
c025fef50b Add short titles to SDG targets
Since targets didn't have a title but only a long description, every
form allowing to select targets was pretty much unusable: we either
displayed just the code or the whole description.

Now, with a concise title, it's easier to find and select the desired
target.

The titles have been copied from The Global Goals page [1].

Note we're using the `short_title` I18n key for the `title` method and
the `long_title` I18n key for the `long_title` method. We can't use
`title` as I18n key instead of `short_title` because it would affect
existing translations.

[1] https://www.globalgoals.org/
2021-10-01 16:19:10 +02:00
Javi Martín
7f9a05d052 Extract method to get SDG code with title 2021-09-30 16:05:57 +02:00
Javi Martín
1290e2ecd3 Store files with both Paperclip and ActiveStorage
In order to migrate existing files from Paperclip to ActiveStorage, we
need Paperclip to find out the files associated to existing database
records. So we can't simply replace Paperclip with ActiveStorage.

That's why it's usually recommended [1] to first run the migration and
then replace Paperclip with ActiveStorage using two consecutive
deployments.

However, in our case we can't rely on two consecutive deployments
because we have to make an easy process so existing CONSUL installations
don't run into any issues. We can't just release version 1.4.0 and 1.5.0
and day and ask everyone to upgrade twice on the same day.

Instead, we're following a different plan:

* We're going to provide a Rake task (which will require Paperclip) to
  migrate existing files
* We still use Paperclip to generate link and image tags
* New files are handled using both Paperclip and ActiveStorage; that
  way, when we make the switch, we won't have to migrate them, and in
  the meantime they'll be accessible thanks to Paperclip
* After we make the switch, we'll update the `name` column in the active
  storage attachments tables in order to remove the `storage_` prefix

Regarding our handling of new files, the exception are cached
attachments. Since those attachments are temporary files used while
submitting a form and we have to delete them afterwards, we're only
handling them with Paperclip. We'll handle these ones in version 1.5.0.

Note the task creating the dev seeds was failing after these changes
with an `ActiveStorage::IntegrityError` exception because we were
opening some files without closing them. If the same file was attached
twice, it failed the second time.

We're solving it by closing the files with `File.open` and a block. Even
though we didn't get any errors, we're doing the same thing in the
`Attachable` concern because it's a good practice to close files after
we're done with them.

Also note we have to change the CKEditor Active Storage code so it's
compatible with Paperclip. In this case, I haven't been able to write a
test to confirm the attachment exists; I was getting the same
`ActiveStorage::IntegrityError` mentioned above.

Finally, we're updating the site customization image controller to use
`update` so the image and the attachment are updated within the same
transaction. This is also what we do in most controllers.

[1] https://www.youtube.com/watch?v=tZ_WNUytO9o
2021-09-24 13:39:15 +02:00
Javi Martín
2b4b2f3442 Use aria-label in admin table actions
This way screen reader users will know which record they're going to
access when focusing on a link to a certain action. Otherwise they'd
hear something like "Edit, link", and they wouldn't know which record
they'll end up editing if they follow the link.
2021-09-20 20:27:37 +02:00
Javi Martín
f7b83319c6 Expire cache when adding documents and images
Proposals and budget investments were not correctly updated when adding,
removing or modifying their documents and images.
2021-09-14 18:34:31 +02:00
Javi Martín
53f4770544 Merge pull request #4667 from consul/published_proposals_feed
Do not show unpublished proposals on the homepage
2021-09-08 17:00:01 +02:00
taitus
7e826a9cb4 Do not show unpublished proposals on the homepage
The chances of an unpublished proposal appearing on the homepage was
very low because only the proposals with the most votes appear there and
unpublished proposals don't have any votes. However, it was technically
possible on new sites where only a few proposals had been created.
2021-09-08 12:38:54 +02:00
Javi Martín
f0d0f1f623 Fix updating a translation to its original value
Users were unable to reset a translation to its original value after
updating it because we weren't storing anything in the database in that
case.

I've considered deleting the existing translation when this happens. I'm
not sure about which approach is the better one, so I'm using the less
destructive one.
2021-09-08 12:38:27 +02:00
Javi Martín
bc47d84a1e Extract method do update I18n contents
This way we can test it properly, which will be helpful when fixing
bugs.
2021-09-08 12:38:27 +02:00
Javi Martín
65c9786db7 Apply Layout/RedundantLineBreak rule to short lines
We're not adding the rule because it would apply the current line length
rule of 110 characters per line. We still haven't decided whether we'll
keep that rule or make lines shorter so they're easier to read,
particularly when vertically splitting the editor window.

So, for now, I'm applying the rule to lines which are about 90
characters long.
2021-09-03 11:49:53 +02:00
Javi Martín
9b61945ee4 Add and apply Lint/EmptyBlock rubocop rule
It was introduced in Rubocop 1.1.0.
2021-09-03 11:49:53 +02:00
Javi Martín
071bcb7023 Add and apply Rails/I18nLocaleAssignment rule
This rule was added in rubocop-rails 2.11.0.

Although we prevent I18n locale leaking between tests by setting it
before each test, the `with_locale` method makes the scope of the locale
change more obvious.
2021-09-03 11:49:52 +02:00
efgalvao
713ae540b0 Add length validation for debate description
Fixes issue #4013.
2021-08-29 01:13:48 +02:00
Machine Learning
4d27bbebad Add experimental machine learning 2021-08-16 16:31:04 +02:00
Javi Martín
e01a94d7bd Use mem_cache_store instead of dalli_store
`dalli_store` is deprecated since dalli 2.7.11.

We can now enable cache_versioning. We didn't enable it when upgrading
to Rails 5.2 because of possible incompatibility with `dalli_store` [1],
even though apparently some the issues were fixed in dalli 2.7.9 and
dalli 2.7.10 [2].

Since using cache versioning makes cache expiration more efficient, and
I'm not sure whether the options we were passing to the dalli store are
valid with memcache store (documentation here is a bit lacking), I'm
just removing the option we used to double the default cache size on
production.

[1] https://www.schneems.com/2018/10/17/cache-invalidation-complexity-rails-52-and-dalli-cache-store
[2] https://github.com/petergoldstein/dalli/blob/master/History.md
2021-08-15 19:42:22 +02:00
Javi Martín
102cf74b3d Bump faker from 1.8.7 to 2.0
Since version 2.0 introduced many breaking changes, we're upgrading to
it first.

The changes have been done by installing the rubocop-faker gem and
running:

```
rubocop \
  --require rubocop-faker \
  --only Faker/DeprecatedArguments \
  --auto-correct
```
2021-08-13 04:39:44 +02:00
Javi Martín
930bb753c5 Use a rake task to delete cached attachments
Our previous system to delete cached attachments didn't work for
documents because the `custom_hash_data` is different for files created
from a file and files created from cached attachments.

When creating a document attachment, the name of the file is taken into
account to calculate the hash. Let's say the original file name is
"logo.pdf", and the generated hash is "123456". The cached attachment
will be "123456.pdf", so the generated hash using the cached attachment
will be something different, like "28af3". So the file that will be
removed will be "28af3.pdf", and not "123456.pdf", which will still be
present.

Furthermore, there are times where users choose a file and then they
close the browser or go to a different page. In those cases, we weren't
deleting the cached attachments either.

So we're adding a rake task to delete these files once a day. This way
we can simplify the logic we were using to destroy cached attachments.

Note there's related a bug in documents: when editing a record (for
example, a proposal), if the title of the document changes, its hash
changes, and so it will be impossible to generate a link to that
document. Changing the way this hash is generated is not an option
because it would break links to existing files. We'll try to fix it when
moving to Active Storage.
2021-07-30 17:30:11 +02:00
Javi Martín
701378d02c Add padding to the whole header
Instead of adding the padding to each individual element inside the
container, why not adding padding to the container itself? The answer is
"because we want the background of the children elements to take the
width of the whole screen". But this generates either HTML cluttered
with elements to add padding or repetitive padding definitions in the
CSS.

So now we only define the padding once, and when an element requires a
full width background or border, we use the `full-width-background`
mixin.

In this case the code is a bit more complex because the header is also
used in the dashboard and admin layouts:

* In the public layout, the body has a margin, so we include the mixin
  to take margin into account
* In the dashboard layout, the header itself has a margin, so we include
  the same mixin
* In the admin layout, the headet doesn't have a margin but gets the
  whole width, so in this case we include the mixin which dosen't take
  the margin into account

In the future, the idea is to apply this principle to the <body>
element and remove the `@include grid-column-gutter` in the CSS as well
as the `small-12 column` classes in the HTML.

Note we use the `calc()` function inside the mixin instead of using it
in the `$full-width-margin` variable. That way we avoid nested `calc()`
operations, which don't work in Internet Explorer.

Also note we're using `flex-grow: 1` to make one element appear on the
left of the screen and the other one on the right. It would be easier to
use `justify-content: space-between` (which is actually the default for
the top-bar element). However, there's a bug in Internet Explorer and
old versions of Firefox; they include the absolutely-positioned
`::before` element we use to set the full width background when
calculating where to position the elements. The bug was fixed in Firefox
52 (released in 2017).

Finally, we're removing the padding from our logo. In order to allow
logos like the new one and at the same time provide backwards
compatibility to logos in existing CONSUL installations, we're relaxing
the validation rule for the logo width.
2021-07-09 03:45:55 +02:00
Javi Martín
bf5afd8e7c Add missing expectation to related content test
It looks like we accidentally missed it while rebasing commit
48dc72cea9.
2021-06-30 16:13:37 +02:00
taitus
108de86da5 Add link_url validation for cards when are header cards
Add link_url presence validation only when link_text is provided only for header cards.

In this case it makes sense to allow creating a "header card" without link_url, since
we can show the header without link text and without link url and it still does its
function.
2021-06-28 15:59:04 +02:00
taitus
1d47a5c079 Add link_url validation for cards when are not header cards
Currently it is not necessary to include the link_url field.

When we display these cards without link_url, they create an empty link that
redirects to the same page. I understand that this is not a desired behavior, so I
think it is better to add a validation in this case and force administrators to add a
link_url when creating a card.
2021-06-28 15:57:39 +02:00