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:
@@ -8,12 +8,15 @@ module Translatable
|
|||||||
private
|
private
|
||||||
|
|
||||||
def translation_params(params)
|
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
|
end
|
||||||
|
|
||||||
def delete_translations
|
def delete_translations
|
||||||
locales = resource_model.globalize_locales.
|
locales = resource_model.translated_locales
|
||||||
select { |k, v| params[:delete_translations].include?(k.to_sym) && params[:delete_translations][k] == "1" }
|
.select { |l| params.dig(:delete_translations, l) == "1" }
|
||||||
locales.each do |l|
|
locales.each do |l|
|
||||||
Globalize.with_locale(l) do
|
Globalize.with_locale(l) do
|
||||||
resource.translation.destroy
|
resource.translation.destroy
|
||||||
@@ -21,4 +24,8 @@ module Translatable
|
|||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@@ -71,6 +71,18 @@ feature "Translations" do
|
|||||||
expect(page).not_to have_link "Español"
|
expect(page).not_to have_link "Español"
|
||||||
end
|
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
|
context "Globalize javascript interface" do
|
||||||
|
|
||||||
scenario "Highlight current locale", :js do
|
scenario "Highlight current locale", :js do
|
||||||
|
|||||||
Reference in New Issue
Block a user