I'm having some issues with configuring the mobility gem.
If a translation isn't present, I would like to get the default translation (in this case :de should fallback to :en). I built a test which should explain what I'm struggling with. I guess it's a misconfiguration, hopefully someone can point out what I'm missing here.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
gem "rails", "~> 5.2.0"
gem "pg", "~> 0.12"
gem "mobility", "~> 1.2.9"
gem "i18n", "< 1.9.0"
end
require "active_record"
require "minitest/autorun"
require "mobility"
Mobility.configure do
plugins do
backend :key_value
active_record
reader
writer
column_fallback true
fallbacks({ :en => :de }) # this is the line that's giving me headaches
locale_accessors [:en, :de]
end
end
I18n.available_locales = [:en, :de]
I18n.default_locale = :en
ActiveRecord::Base.establish_connection(adapter: "postgresql", host: "localhost", database: "testing", user: "postgres", password: "password")
ActiveRecord::Schema.define do
#enable_extension "plpgsql"
create_table "mobility_string_translations", force: :cascade do |t|
t.string "locale", null: false
t.string "key", null: false
t.string "value"
t.string "translatable_type"
t.bigint "translatable_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["translatable_id", "translatable_type", "key"], name: "index_mobility_string_translations_on_translatable_attribute"
t.index ["translatable_id", "translatable_type", "locale", "key"], name: "index_mobility_string_translations_on_keys", unique: true
t.index ["translatable_type", "key", "value", "locale"], name: "index_mobility_string_translations_on_query_keys"
end
create_table "mobility_text_translations", force: :cascade do |t|
t.string "locale", null: false
t.string "key", null: false
t.text "value"
t.string "translatable_type"
t.bigint "translatable_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["translatable_id", "translatable_type", "key"], name: "index_mobility_text_translations_on_translatable_attribute"
t.index ["translatable_id", "translatable_type", "locale", "key"], name: "index_mobility_text_translations_on_keys", unique: true
end
create_table "contents", id: :serial, force: :cascade do |t|
t.string "key"
t.text "body"
end
end
class Content < ActiveRecord::Base
self.table_name = "contents"
extend Mobility
translates :key, type: :string
translates :body, type: :text
end
class BugTest < ActiveSupport::TestCase
test "should have fallbacks enabled" do
model = Content.create!
model.key = "foo"
model.body = "bar"
model.save
model.reload
assert model.key # works as expected
assert model.body # works as expected
assert model.key(locale: :de) # fails. expecting "foo"
assert model.body(locale: :de) # fails. expecting "bar"
assert model.key_de # fails
assert model.body_de # fails
end
end