When using polymorphic relations, e.g. when specifying a Rating model which either references an Article or a Comment, via a belongs_to :rateable, polymorphic: true, we would have a rateable_id and rateably_type column.
I like to use enumerize to validate my rateably_type column, via enumerize :rateably_type, in: %w[Article Comment], which works as intended.
Now, if I were to display a list of all ratings, I would do something like this:
<% ratings.each do |rating| %>
= rating.rateable_type.text
<% end %>
Where rateably_type.text (or rateable_type_text) would give me the translated enumerize value. This is usually taken from en.enumerize.rating.rateably_type.<value>, where value would be Article or Comment.
Problem
The translations I specify for enumerize are the same translations I already stored under activerecord.models.article. I don't like repetition, so I'm looking for a way to avoid this 🙂
There is the i18n_scope-Option which can be used in the Model, where I could do enumerize :rateably_type in: %w[Article Comment], i18n_scope: "activerecord.models".
However, this does not work, as the model translations use one and other. So when using rating.rateable_type.text, it gives me { one: "Article", other: "Articles" } instead of just "Article".
Proposed solutions
Allow passing a proc to i18n_scope, which gets evaluated: i18n_scope: ->(value) { I18n.t(value, scope: "activerecord.models", count: 1 }
Allow passing arguments to the .text or *_text-methods: rating.rateable_type.text(count: 1)
Number 1 would be my favourite, as it gives the most flexibility 🙂
Let me know how you think about that issue - I can probably make a PR if that contribution would be welcome, but wanted to check first!
Background
In a Rails app, we typicall have model translations like this:
When using polymorphic relations, e.g. when specifying a
Rating
model which either references anArticle
or aComment
, via abelongs_to :rateable, polymorphic: true
, we would have arateable_id
andrateably_type
column.I like to use enumerize to validate my
rateably_type
column, viaenumerize :rateably_type, in: %w[Article Comment]
, which works as intended.Now, if I were to display a list of all ratings, I would do something like this:
Where
rateably_type.text
(orrateable_type_text
) would give me the translated enumerize value. This is usually taken fromen.enumerize.rating.rateably_type.<value>
, wherevalue
would beArticle
orComment
.Problem
The translations I specify for enumerize are the same translations I already stored under
activerecord.models.article
. I don't like repetition, so I'm looking for a way to avoid this 🙂There is the
i18n_scope
-Option which can be used in the Model, where I could doenumerize :rateably_type in: %w[Article Comment], i18n_scope: "activerecord.models"
.However, this does not work, as the model translations use
one
andother
. So when usingrating.rateable_type.text
, it gives me{ one: "Article", other: "Articles" }
instead of just"Article"
.Proposed solutions
i18n_scope
, which gets evaluated:i18n_scope: ->(value) { I18n.t(value, scope: "activerecord.models", count: 1 }
.text
or*_text
-methods:rating.rateable_type.text(count: 1)
Number 1 would be my favourite, as it gives the most flexibility 🙂
Let me know how you think about that issue - I can probably make a PR if that contribution would be welcome, but wanted to check first!