Commit Graph

5 Commits

Author SHA1 Message Date
Javi Martín
5c2ca27df1 Update Globalize monkey-patch to forward kw args
This method changed in Globalize version 6.0.1 [1], which we're using since
commit 6072372c9d.

We were getting a deprecation warning in Ruby 2.7:

```
config/initializers/globalize.rb:8: warning: Using the last argument as
keyword parameters is deprecated; maybe ** should be added to the call
```

So we're using the `(...)` arguments syntax to make it compatible with
Ruby 3.0, just like Globalize does when using Ruby 2.7.

[1] See pull request 778 in the globalize/globalize GitHub repo.
2023-01-26 17:19:15 +01:00
Javi Martín
5b844bf231 Use index_with to simplify hash generation
This method was introduced in Rails 6.0. It can be used to take an array
and create a hash where the elements of the array are the indexes of the
hash.
2022-08-24 18:19:02 +02:00
Javi Martín
b5a4609b56 Make it easier to customize validations
There are CONSUL installations where the validations CONSUL offers by
default don't make sense because they're using a different business
logic. Removing these validations in a custom model was hard, and that's
why in many cases modifying the original CONSUL models was an easier
solution.

Since modifying the original CONSUL models makes the code harder to
maintain, we're now providing a way to easily skip validations in a
custom model. For example, in order to skip the price presence
validation in the Budget::Heading model, we could write a model in
`app/models/custom/budget/heading.rb`:

```
require_dependency Rails.root.join("app", "models", "budget", "heading").to_s

class Budget::Heading
  skip_validation :price, :presence
end
```

In order to skip validation on translatable attributes (defined with
`validates_translation`), we have to use the
`skip_translation_validation` method; for example, to skip the proposal
title presence validation:

```
require_dependency Rails.root.join("app", "models", "proposal").to_s

class Proposal
  skip_translation_validation :title, :presence
end

```

Co-Authored-By: taitus <sebastia.roig@gmail.com>
2022-03-24 17:05:35 +01:00
Javi Martín
2e6644d513 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-10-22 16:36:18 +02:00
Javi Martín
3b5a12b0ab 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-10-22 16:28:53 +02:00