Skip to content
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

Support multiple translated_inputs #31

Open
wants to merge 1 commit into
base: develop
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ $ ->

# Hides or shows the + button and the remove button.
updateLocaleButtonsStatus = ($dom) ->
$localeList = $dom.find('.add-locale ul li:not(.hidden)')
if $localeList.length == 0
$dom.find('.add-locale').hide()
else
$dom.find('.add-locale').show()
$dom.find('.add-locale').each ->
$localeList = $(this).find('ul li:not(.hidden)')
if $localeList.length == 0
$(this).hide()
else
$(this).show()


# Hides or shows the locale tab and its corresponding element in the add menu.
Expand Down Expand Up @@ -112,24 +113,25 @@ $ ->
$('.activeadmin-translations > ul').each ->
# Get the corresponding fieldsets.
$fieldsets = $(this).siblings('fieldset')
$("li:not(.add-locale) > a", this).each ->
# Remove them if the locale is hidden.
if $(this).hasClass('hidden')
# check if it's an existing translation otherwise remove it
$currentFieldset = $("fieldset#{$(this).attr('href')}")
$translationId = $('input[id$=_id]', $currentFieldset)
if $translationId.val()
# mark it for database removal appending a _destroy element
$destroy = $('<input/>').attr(
type: 'hidden',
name: $translationId.attr('name').replace('[id]', '[_destroy]'),
id: $translationId.attr('id').replace('_id', '_destroy'),
value: '1'
)
$destroy.appendTo($currentFieldset)
else
# remove the fieldset from dom so it won't be submitted
$fieldsets.filter($(this).attr('href')).remove()
$("li:not(.add-locale) > a.hidden", this).each ->
# check if it's an existing translation otherwise remove it
$currentFieldset = $fieldsets.filter($(this).attr('href'))
$translationId = $('input[id$=_id]', $currentFieldset)
if !$translationId.val()
# remove the fieldset from dom so it won't be submitted
$fieldsets.filter($(this).attr('href')).remove()
else if !$(".activeadmin-translations > ul > li:not(.add-locale) > a[href='#{$(this).attr('href')}']:not(.hidden)").length
# mark it for database removal appending a _destroy element
$destroy = $('<input/>').attr(
type: 'hidden',
name: $translationId.attr('name').replace('[id]', '[_destroy]'),
id: $translationId.attr('id').replace('_id', '_destroy'),
value: '1'
)
$destroy.appendTo($currentFieldset)
else
# clear the particular fields for the existing translation
$currentFieldset.find('input[type=text],textarea').val("")

#Initially update the buttons' status
updateLocaleButtonsStatus($dom)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_admin/globalize/form_builder_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def translated_inputs(name = "Translations", options = {}, &block)
inputs_for_nested_attributes(
for: [:translations, translation ],
class: "inputs locale locale-#{translation.locale}",
# permit_params :translation_attributes => [:name, :description] does
# not permit "translation_attributes" => {"en" => {:name =>, "", :description => ""}, "de" => ...}
# While the keys can be arbitrary, they must be integers
for_options: {child_index: translation.locale.to_s.bytes.select{|b| b != 45}.map{|b| b - 27}.join()},
&fields
)
end.join.html_safe
Expand Down