Commit Graph

11202 Commits

Author SHA1 Message Date
Javi Martín
a7a9355a58 Enable SCSS rules in Hound 2018-11-05 18:20:50 +01:00
Javi Martín
6a42f6fee2 Add basic rubocop configuraton for Hound
This way we can ask contributors to follow some basic guidelines like
removing trailing whitespaces while not overwhelming them with all our
rules.
2018-11-05 18:20:50 +01:00
Javi Martín
bf79ddb858 Remove rubocop_todo file
No developers are maintaining it anymore.
2018-11-05 18:20:50 +01:00
Javi Martín
70195a1c91 Fix bug with non-underscored locales
Ruby can't have hyphens in method names, so sending something like
`record.title_pt-BR` would raise an exception.

Using globalize's `localized_attr_name_for` method fixes the bug.

Thanks Marko for the tip.
2018-11-05 18:20:50 +01:00
Javi Martín
adf03b9e1b Don't abort the migration if the simulation fails
We think aborting the migration will generate more headaches to system
administrators, who will have to manually check and fix every invalid
record before anything can be migrated.
2018-11-05 18:20:50 +01:00
Javi Martín
10b0d8232d Add task to simulate data migration
This way we can check everything is OK before actually migrating the
data to the translations tables.
2018-11-05 18:20:50 +01:00
Javi Martín
9b671a8132 Log failed data migrations
In theory, it should never happen, but that's why exceptions exist.
2018-11-05 18:20:50 +01:00
Javi Martín
b9906209ea Use migrate_data option for globalize
This way the task to migrate the data doesn't have to be run manually if
these migrations weren't already executed.
2018-11-05 18:20:50 +01:00
Javi Martín
232d5b0edd Migrate custom pages data to their locale 2018-11-05 18:20:50 +01:00
Javi Martín
c774ae67a0 Add task to migrate data to translation tables
We forgot to do it when we created the translation tables, and so now we
need to make sure we don't overwrite existing translations.
2018-11-05 18:20:50 +01:00
Javi Martín
c9fadc7c3d Fix poll question with non-underscored locales
Ruby can't have hyphens in method names, so sending something like
`title_pt-BR=` would raise an exception.
2018-11-05 18:20:50 +01:00
Javi Martín
8f4d600b47 Fix spec assuming German isn't available
Since we've recently added German to the available languages, and we
might support every language in the future, we're using the fictional
world language to check a locale which isn't available.

Another option could be to set the available locales in the test
environment (or the rspec helper), but then we'd have to make sure it's
executed before the call to `globalize_accessors` in the model, and it
might be confusing for developers.
2018-11-05 18:20:50 +01:00
Javi Martín
0f759f1e16 Extract method to render form fields for a locale 2018-11-05 18:20:50 +01:00
Javi Martín
c8d3c8fc1b Use detect instead of select.first 2018-11-05 18:20:50 +01:00
Javi Martín
b745ddc9dd Simplify code checking whether to enable a locale 2018-11-05 18:20:50 +01:00
Javi Martín
7f5f144837 Don't run specs if there are custom fallbacks
This spec depends on French falling back to Spanish and was failing on
forks using a different fallback.
2018-11-05 18:20:50 +01:00
Javi Martín
0c1228a7ce Fix crash with no translation for default locale
When we were visiting a page showing the content of a record which uses
globalize and our locale was the default one and there was no
translation for the default locale, the application was crashing in some
places because there are no fallbacks for the default locale.

For example, when visiting a legislation process, the line with
`CGI.escape(title)` was crashing because `title` was `nil` for the
default locale.

We've decided to solve this issue by using any available translations as
globalize fallbacks instead of showing a 404 error or a translation
missing error because these solutions would (we thinkg) either require
modifying many places in the application or making the translatable
logic even more complex.

Initially we tried to add this solution to an initializer, but it must
be called after initializing the application so I18n.fallbacks[locale]
gets the value defined in config.i18n.fallbacks.

Also note the line:

fallbacks[locale] = I18n.fallbacks[locale] + I18n.available_locales

Doesn't mention `I18n.default_locale` because the method
`I18n.fallbacks[locale]` automatically adds the default locale.
2018-11-05 18:20:50 +01:00
Javi Martín
98cfe8c503 Remove question option uniqueness validation
Having translations makes this validation too complex and not worth the
effort, since now we can't just scope in a single column but we need to
scope in the translations locale and the question ID.
2018-11-05 18:20:50 +01:00
Javi Martín
cf4c30b143 Prefix classes used in JavaScript with "js-"
The same way it's done in the rest of the application.
2018-11-05 18:20:50 +01:00
Javi Martín
d10f07b0b4 Make it easier to know destroy_field is an input
By using the input and finding it by its name, it's easier to see the
difference between this input and the delete-language link.
2018-11-05 18:20:50 +01:00
Javi Martín
9aa07e3b74 Follow naming conventions for HTML classes and IDs
We use underscores for IDs and hyphens for classes.
2018-11-05 18:20:50 +01:00
Javi Martín
f475f4ff3f Remove reference to site customization page locale
We don't use that attribute since we added translations for this model.
2018-11-05 18:20:50 +01:00
Javi Martín
ed990c24d0 Fix "Add option" link position
The new options were being added inside the `.column` div, when they
needed to be added before it.
2018-11-05 18:20:50 +01:00
Javi Martín
44f71b24db Fix rubocop line too long warning 2018-11-05 18:20:50 +01:00
Javi Martín
9fc235727e Extract method in translatable builder
This way we fix the rubocop warning for line too long and make the code
a bit easier to read.
2018-11-05 18:20:50 +01:00
Javi Martín
b024363fae Fix removing an option for legislation questions
We were allowing the `_destroy` field for translations, but not for the
options themselves.
2018-11-05 18:20:50 +01:00
Javi Martín
3165b9b9f4 Fix legislation options not being updated
We broke this behaviour by introducing translations and not allowing the
`id` parameter anymore.
2018-11-05 18:20:50 +01:00
Javi Martín
015cdcc557 Simplify creating a process in questions specs 2018-11-05 18:20:50 +01:00
Javi Martín
1c75df92d7 Fix updating translatables without current locale
The current locale wasn't being marked for destruction and so saving
the record tried to create a new translation for the current locale.
2018-11-05 18:20:50 +01:00
Javi Martín
01ed66e9bb Fix alignment in last translatable fields
When we grouped the fields together, the last one turned into a
`last-child`, which foundation automatically aligns to the right.

The markdown editor also needed to be tweaked a little bit.
2018-11-05 18:20:50 +01:00
Javi Martín
7b9b65043f Fix ambiguous field in test 2018-11-05 18:20:50 +01:00
Javi Martín
28ed6aa136 Make private methods private 2018-11-05 18:20:50 +01:00
Javi Martín
2658bb55f6 Wrap translation fields in a div
This way we can show/hide that div when displaying translations, and we
can remove the duplication applying the same logic to the label, the
input, the error and the CKEditor.

This way we also solve the problem of the textarea of the CKEditor
taking space when we switch locales, as well as CKEditor itself taking
space even when not displayed.
2018-11-05 18:20:50 +01:00
Javi Martín
a48db19c05 Show error message for just the displayed locale
Unfortunately the builder didn't offer any options for the error message
and we just had to overwrite the `error_for` methods.
2018-11-05 18:20:50 +01:00
Javi Martín
87484015da Update information texts translatable fields
This part used the code we deleted in order to make it easier to
refactor the rest of the translatable models. Now we add the code back.
2018-11-05 18:20:50 +01:00
Javi Martín
1f033383e5 Refactor globalize_locales partials to increase DRYness 2018-11-05 18:20:50 +01:00
Angel Perez
4af5e1d46d Improve I18nContent#flat_hash readability using concise variable names 2018-11-05 18:20:50 +01:00
Angel Perez
ae9cad3c5b Avoid ternary operator usage when appending/creating I18n keys
When using the OR operator, if the left side of the expression evaluates
to false, its right side is taken into consideration. Since in Ruby nil
is false, we can avoid using conditionals for this particular scenario
2018-11-05 18:20:50 +01:00
Angel Perez
3c033d0ac3 Improve readability for I18nContent#begins_with_key spec
In order to be consistent with similar specs, the I18nContent#begins_with_key
spec was improved by explicitly specifying an I18n key for one (1) factory
that relied on its default value
2018-11-05 18:20:50 +01:00
Angel Perez
64dbf55d55 Fix Rubocop warnings [ci skip] 2018-11-05 18:20:50 +01:00
Raúl Fuentes
ff6e8c9bcb Move flat_hash to I18nContent model
also add unit tests for this function and a description
into the model of the behaviour of the function
2018-11-05 18:20:50 +01:00
Angel Perez
9b19a4e956 Extract translation logic to helper method 2018-11-05 18:20:50 +01:00
Angel Perez
362157d56e Add I18nContent model specs 2018-11-05 18:20:50 +01:00
Javi Martín
937cf0bdd3 Validate both the model and its translations
This way we guarantee there will be at least one translation for a model
and we keep compatibility with the rest of the application, which
ideally isn't aware of globalize.
2018-11-05 18:20:50 +01:00
Javi Martín
a38eac02df Refactor globalize models code using a concern
I've chosen the name "Globalizable" because "Translatable" already
existed.
2018-11-05 18:20:50 +01:00
Javi Martín
1ab3b7f42d Don't force translations for the current locale
Globalize creates a translation for the current locale, and the only way
I've found to change this behaviour is to monkey-patch it.

The original code uses `translation.locale` instead of
`Globalize.locale`. Since `translation.locale` loads the translation
with empty attributes. It both makes the record invalid if there are
validations and it makes it almost impossible to create a record with
translations which don't include the current locale.

See also the following convertations:

https://github.com/globalize/globalize/pull/328
https://github.com/globalize/globalize/issues/468
https://github.com/globalize/globalize/pull/578
https://github.com/shioyama/mobility/wiki/Migrating-from-Globalize#blank-translations
2018-11-05 18:20:50 +01:00
Javi Martín
f2d64833f0 Simplify methods defining translation styles
This refactor is going to be useful when we change these rules within
the next few commits.
2018-11-05 18:20:50 +01:00
Javi Martín
981b13ac9b Update widget cards translatable fields 2018-11-05 18:20:50 +01:00
Javi Martín
e400cb8eae Update site customization pages translatable fields 2018-11-05 18:20:50 +01:00
Javi Martín
e9a5f03089 Update poll question answers translatable fields
We needed to bring back support for CKEditor in our translatable form,
which we had temporarily remove.

And now we support CKEditor in our translatable specs, and so we can
remove the duplicated specs for poll question answers.
2018-11-05 18:20:50 +01:00