diff --git a/lib/active_admin/globalize3/engine.rb b/lib/active_admin/globalize3/engine.rb index 267b8bd0..04ee875a 100644 --- a/lib/active_admin/globalize3/engine.rb +++ b/lib/active_admin/globalize3/engine.rb @@ -1,3 +1,5 @@ +require 'active_admin/globalize3/filter_empty_translations' + module ActiveAdmin module Globalize3 class Engine < ::Rails::Engine @@ -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 diff --git a/lib/active_admin/globalize3/filter_empty_translations.rb b/lib/active_admin/globalize3/filter_empty_translations.rb new file mode 100644 index 00000000..4c873fe7 --- /dev/null +++ b/lib/active_admin/globalize3/filter_empty_translations.rb @@ -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