Skip to content

Style the flags select for empty/inactive values in translated_row #18

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

Merged
merged 1 commit into from
Nov 30, 2016
Merged
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 @@ -148,7 +148,8 @@ $ ->
$td = $(this).closest('td')
$('.field-translation', $td).hide()
$(".locale-#{$locale}", $td).show()
$(this).parent().children('a.ui-translation-trigger').removeClass('active')
$(this).addClass('active')
e.preventDefault()

translations()

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
// Used to distantiate inline locale selector
span.inline-locale-selector
margin-right: 10px
> .ui-translation-trigger
opacity: .4
&.empty
filter: grayscale(100%)
&.active
opacity: 1

.field-translation.hidden
display: none
Expand Down
40 changes: 25 additions & 15 deletions lib/active_admin/globalize/attributes_table_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ def translated_row(*args, &block)
if options[:inline]
''.html_safe.tap do |value|
# Add selectors for inline locale
value << inline_locale_selectors
value << inline_locale_selectors(field, options[:locale], &block)
# Build translations spans
value << field_translations(field, :span, options[:locale], &block)
end
else
content_tag(:div, class: 'activeadmin-translations') do
''.html_safe.tap do |value|
# Render selectors as in translation ui
value << block_locale_selectors
value << block_locale_selectors(field, options[:locale], &block)
# Build translations divs for actual translations
value << field_translations(field, :div, options[:locale], &block)
end
Expand All @@ -82,20 +82,18 @@ def translatable?(field)
# @param [String] field field name to render
# @param [Symbol] tag tag to enclose field translation
# @param [Symbol] initial_locale locale to set as not hidden
def field_translations(field, tag, initial_locale)
def field_translations(field, tag, initial_locale, &block)
available_translations.map do |translation|
# Classes for translation span only first element is visible
css_classes = ['field-translation', "locale-#{translation.locale}"]
# Initially only element for selected locale is visible
css_classes.push 'hidden' unless translation.locale == initial_locale.to_sym
# Build content for cell or div using translation locale and given block
content = I18n.with_locale(translation.locale) do
block_given? ? yield(translation) : translation.send(field)
end
content = field_translation_value(translation, field, &block)
# return element
if tag == :span # inline element
# attach class to span if inline
css_classes.push(:empty) if content.blank?
css_classes.push('empty') if content.blank?
content_tag(tag, content.presence || 'Empty', class: css_classes)
else
# block content
Expand All @@ -107,25 +105,32 @@ def field_translations(field, tag, initial_locale)
end.join(' ').html_safe
end

def block_locale_selectors
def block_locale_selectors(field, initial_locale, &block)
content_tag(:ul, class: 'available-locales locale-selector') do
available_translations.map(&:locale).map do |locale|
default = 'default' if locale == I18n.default_locale
content_tag(:li, class: 'translation-tab') do
I18n.with_locale(locale) do
content_tag(:a, I18n.t(:"active_admin.globalize.language.#{locale}"), href: ".locale-#{locale}", class: default)
available_translations.map do |translation|
css_classes = ['translation-tab']
css_classes << 'active' if translation.locale == initial_locale.to_sym
css_classes << 'empty' unless content.presence
content_tag(:li, class: css_classes) do
I18n.with_locale(translation.locale) do
default = 'default' if translation.locale == initial_locale.to_sym
content_tag(:a, I18n.t(:"active_admin.globalize.language.#{translation.locale}"), href: ".locale-#{translation.locale}", class: default)
end
end
end.join.html_safe
end
end

# Return flag elements to show the given locale using javascript
def inline_locale_selectors
def inline_locale_selectors(field, initial_locale, &block)
content_tag(:span, class: 'inline-locale-selector') do
available_translations.map do |translation|
content = field_translation_value(translation, field, &block)
css_classes = ['ui-translation-trigger']
css_classes << 'active' if translation.locale == initial_locale.to_sym
css_classes << 'empty' unless content.presence
# Build a link to show the given translation
link_to(flag_icon(translation.locale), '#', class: 'ui-translation-trigger', data: {locale: translation.locale})
link_to(flag_icon(translation.locale), '#', class: css_classes, data: {locale: translation.locale})
end.join(' ').html_safe
end
end
Expand All @@ -134,6 +139,11 @@ def available_translations
@record_translations ||= @collection.first.translations.order(:locale)
end

def field_translation_value(translation, field)
I18n.with_locale(translation.locale) do
block_given? ? yield(translation) : translation.send(field)
end
end
end
end
end
11 changes: 10 additions & 1 deletion spec/features/article_composing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@
within first_table_row do
page.should have_css 'th', text: 'TITLE'
page.should have_css 'span.field-translation', text: article.title

flag_link(:hu).find(:xpath, '..').should have_css '.empty:not(.active)'
flag_link(:it).find(:xpath, '..').should have_css ':not(.empty):not(.active)'

flag_link(:hu).click # change shown translation
flag_link(:hu).find(:xpath, '..').should have_css '.empty.active'

flag_link(:it).click # change shown translation
flag_link(:it).find(:xpath, '..').should have_css ':not(.empty).active'

page.should have_css 'span.field-translation', text: article.translation_for(:it).title
end

Expand Down Expand Up @@ -101,4 +110,4 @@

end

end
end
12 changes: 10 additions & 2 deletions spec/models/article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
let(:article) { create(:localized_article) }
subject { article }

it { should have(2).translations }
it { should have(3).translations }

it 'should have italian translation' do
I18n.with_locale :it do
article.title.should == 'Italian title'
article.body.should == 'Italian Body'
end
end

it 'should have hungarian translation' do
I18n.with_locale :hu do
article.title.should == 'Article title'
article.body.should == 'Hungarian Body'
end
end

end

end
end
1 change: 1 addition & 0 deletions spec/support/factories/articles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

after :create do |a|
I18n.with_locale(:it) { a.update_attributes! title: 'Italian title', body: 'Italian Body' }
I18n.with_locale(:hu) { a.update_attributes! body: 'Hungarian Body' }
end

end
Expand Down