Got a question on how to setup fixtures for Mobility. Would be very grateful for any tips on how to get this going and would be a valuable lesson for me as well on how to tackle setting up fixtures in general.
Not using any gems to setup fixtures, just the default Rails approach for this case. I have a Song model which has multiple translatable attributes, title
uses Mobility, description
and content
use Mobility Action Text.
It works really well but when setting up fixtures I'm finding it difficult to relate the records. There's three tables at play here songs
where the only field used is status. mobility_string_translations
stores translations for title and action_text_rich_texts
stores translated description
s and content
.
This is how my translation setup looks like in Song
:
class Song < ApplicationRecord
extend Mobility
validates :title_pt, presence: true
validates :status, inclusion: { in: %w(draft published private) },
presence: true
translates :title, type: :string, locale_accessors: I18n.available_locales
translates :description, backend: :action_text, locale_accessors: I18n.available_locales
translates :content, backend: :action_text, locale_accessors: I18n.available_locales
# file continuation...
As for fixtures songs.yml
looks like this:
one:
status: "published"
Then based on what I've found online I've created mobility/string_translations.yml
with the following content:
one:
translatable_id: one (Song)
translatable_type: "Song"
key: "title"
value: "Title in English"
locale: "en"
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
two:
translatable_id: one (Song)
translatable_type: "Song"
key: "title"
value: "Titulo em Português"
locale: "pt"
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
This seems to work but I know it isn't because when I inspect @song = songs(:one)
looking for translated values (@song.title_pt
and @song.title_en
) they're both nil.
Any idea on what to do here?
The issue in my case is that
translatable_type
wasSong
instead of"Song"
and it couldn't map the records inmobility_string_translations
to the correctSong
record. Here's a bit more detail on the setup that I have that does work to write tests:Let's consider the following
Song
model, it has atitle
that can be translated and astatus
which is only used to affect the visibility of the song in the front end. Fixtures for a couple ofSongs
would look like this:The
id
is usually not specified in fixtures but here it becomes necessary so that we're sure which identifier to use when pointing translated records.The Mobility implementation will store any translated
title
s atmobility_string_translations
the following can be added totest/fixtures/mobility/string_translations.yml
:Each song includes a title for English and Portuguese in this case but any locales the record is going to make use of or need to be tested can be included here, in an individual record.
Setting up fixtures with this method associates translated properties to a record and enables them to be accessed in a test.
For example, to change the title of a song, the record can be brought into the test in a
setup
block and thetitle
translations will be available and can be modified:To make sure that the fixture has setup the association bettween the model and the translated records, the
debugger
method can be used. Start by adding it as a breakpoint to your test logic, in this case I'm going to use the example above:Then the test can be run,
bin/rails test
would work but in this example the command to run just the tests for this file would be:The output in the terminal will look similar to this, the program will be paused at this point and it is interactive:
Any variables defined before
debugger
can be accessed, this can be used to inspect if@song
was changed:The title was updated using the
patch
request defined in the test case. Typingcontinue
will move on from the breakpoint and continue running the code in the test file.That should be it!