Skip to content

Avoid saving new translations that are empty, remove existing empty translations. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/active_admin/globalize3/engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_admin/globalize3/filter_empty_translations'

module ActiveAdmin
module Globalize3
class Engine < ::Rails::Engine
Expand All @@ -13,6 +15,14 @@ class Engine < ::Rails::Engine
ActiveAdmin.application.register_javascript "active_admin/active_admin_globalize3.js"
end

# Register a before_filter in ActionController that will filter out
# empty translations submitted by activeadmin-globalize3.
# See ActiveAdmin::Globalize3::FilterEmptyTranslations#filter_empty_translations
initializer "activeadmin-globalize3.load_helpers" do |app|
ActionController::Base.send :include, ActiveAdmin::Globalize3::FilterEmptyTranslations
ActionController::Base.send :before_filter, :filter_empty_translations, only: [:create, :update]
end

end
end
end
53 changes: 53 additions & 0 deletions lib/active_admin/globalize3/filter_empty_translations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
module ActiveAdmin
module Globalize3
module FilterEmptyTranslations
private
# Activeadmin-globalize3 renders inputs for all translations,
# resulting in many empty translations being created by globalize.
#
# For instance, given the available locales L1, L2 and L2, the
# following params would be submitted on 'create':
#
# {
# :
# MODEL => {
# "translations_attributes" => {
# "0" => {
# "locale"=>"L1", "id" => "", ATTR1 => "", ATTR2 => "", ...
# }
# "1" => {
# "locale"=>"L2", "id" => "", ATTR1 => "", ATTR2 => "", ...
# }
# "2" => {
# "locale"=>"L3", "id" => "", ATTR1 => "", ATTR2 => "", ...
# }
# }
# }
# :
# }
#
# Given these parameters, globalize3 would create a record for every
# possible translation - even empty ones.
#
# This filter removes all empty and unsaved translations from params
# and marks empty and saved translation for deletion.
def filter_empty_translations
model_class = controller_name.classify.safe_constantize
if model_class.nil? or not model_class.translates?
return
end
model = controller_name.singularize.to_sym
params[model][:translations_attributes].each do |k,v|
if v.values[2..-1].all?(&:blank?)
if v[:id].empty?
params[model][:translations_attributes].delete(k)
else
params[model][:translations_attributes][k]['_destroy'] = '1'
end
end
end
end
end
end
end