0

Not sure if this is a bug, a feature, a missed doc or a bad config, however when I set all of the translation attributes to a null-sy value, then the translation record self-destructs.

I have the table-backend Mobility setup as follows:

schema.rb

create table "base_model", force: :cascade do |t|
  t.boolean "is_animal"
  t.boolean "is_vehicle"

  ...
end

create table "base_model_translation", force: :cascade do |t|
  t.bigint "base_model_id", null: false
  t.string "locale", null: false
  t.boolean "is_animal_translation"
  t.boolean "is_vehicle_translation"

  ...
end

base_model.rb

class BaseModel < ApplicationRecord
  extend Mobility

  has_many  :translations,
            class_name: "BaseModelTranslations",
            autosave: true,
            dependent: :destroy

  translates :is_animal_translation
  translates :is_vehicle_translation
end

base_model_translation.rb

class BaseModelTranslation < ApplicationRecord
  belongs_to  :base_model,
              touch: true,
              inverse_of: :translations
end

On the Rails console:

base_model = BaseModel.last
base_model.reload.translations # => []

# SET TO NULLSY
base_model.is_animal_translation = true
base_model.save
base_model.reload.translations
# => [
# <#BaseModel::Translation
# id: 1,
# is_animal_translation: true,
# is_vehicle_translation: nil,
# ...>]

base_model.is_animal_translation = false
base_model.save
base_model.reload.translations # => []

# SET TO PARTIAL NULLSY THEN FULL NULLSY
base_model.is_animal_translation = true
base_model.is_vehicle_translation = true
base_model.save
base_model.reload.translations
# => [
# <#BaseModel::Translation
# id: 1,
# is_animal_translation: true,
# is_vehicle_translation: true,
# ...>]

base_model.is_animal_translation = false
base_model.save
base_model.reload.translations
# => [
# <#BaseModel::Translation
# id: 1,
# is_animal_translation: false,
# is_vehicle_translation: true,
# ...>]

base_model.is_animal_translation = false
base_model.save
base_model.reload.translations # => []

Is this an intended behaviour, if so is there a bypass for this?

EDIT: Looks like this was an an intended behaviour (https://github.com/shioyama/mobility/blob/3cbeaeec8ef0a6d22ee05229141cd2b2bb33b17f/lib/mobility/backends/active_record/table.rb#L306), is there a bypass for this?

dropbeardan
  • 1,080
  • 10
  • 11
  • Not sure if relevant, I did notice that the KV backend uses `.blank?` to do this check: https://github.com/shioyama/mobility/blob/e803847f967117a34ef8f31491af555836d97c6d/lib/mobility/backends/active_record/key_value.rb#L66 – dropbeardan Mar 24 '21 at 02:35
  • Oh, it is an intended feature: https://github.com/shioyama/mobility/blob/3cbeaeec8ef0a6d22ee05229141cd2b2bb33b17f/lib/mobility/backends/active_record/table.rb#L306 – dropbeardan Mar 24 '21 at 02:37
  • 1
    Yes this is intended behaviour, maybe could be re-thought. Globalize had issues with empty translations and it seemed safer just to destroy them, but I can imagine there would be situations where you might not want to do that. Happy to consider an option to disable this behaviour if someone wants ot make a PR for it. – Chris Salzberg Mar 24 '21 at 14:09
  • By empty translations I assume you mean that all fields have `nil` for a value but in this case if the translation has all fields with a value of false it will also be destroyed `[nil].none? => true` `[nil, false].none? => true` `[false].none? => true` `["foo", false].none? => false` would you prefer to consider false a valid value and not destroy the record or add an extra flag to disable the behaviour completely? – Rafael Simões Mar 26 '21 at 00:23

0 Answers0