Fix Translatable when field values are changed to blank

If we ignore all params that are blank, there is no way to
"remove" an attribute (i.e. change its value to blank)

On the other hand, we don't want to create new translations
where all fields are empty, so the new code keeps only the
blank fields which belong to existing translations.
This commit is contained in:
Marko Lovic
2018-08-01 13:49:30 +02:00
committed by Javi Martín
parent 4f2ed27f8c
commit 3aa53449c8
2 changed files with 22 additions and 3 deletions

View File

@@ -8,12 +8,15 @@ module Translatable
private
def translation_params(params)
resource_model.globalize_attribute_names.select { |k, v| params.include?(k.to_sym) && params[k].present? }
resource_model
.globalize_attribute_names
.select { |k| params[k].present? ||
resource_model.translated_locales.include?(get_locale_from_attribute(k)) }
end
def delete_translations
locales = resource_model.globalize_locales.
select { |k, v| params[:delete_translations].include?(k.to_sym) && params[:delete_translations][k] == "1" }
locales = resource_model.translated_locales
.select { |l| params.dig(:delete_translations, l) == "1" }
locales.each do |l|
Globalize.with_locale(l) do
resource.translation.destroy
@@ -21,4 +24,8 @@ module Translatable
end
end
def get_locale_from_attribute(attribute_name)
locales = resource_model.globalize_locales
attribute_name.to_s.match(/(#{locales.join('|')})\Z/)&.captures&.first
end
end

View File

@@ -71,6 +71,18 @@ feature "Translations" do
expect(page).not_to have_link "Español"
end
scenario 'Change value of a translated field to blank' do
milestone.update_attributes!(status: create(:budget_investment_status))
visit @edit_milestone_url
fill_in 'budget_investment_milestone_description_en', with: ''
click_button "Update milestone"
expect(page).to have_content "Milestone updated successfully"
expect(milestone.reload.description).to be_blank
end
context "Globalize javascript interface" do
scenario "Highlight current locale", :js do