Skip to content

Commit 3477d6a

Browse files
committed
In translated_row, use gray flags for empty values and transparent flags for inactive value
1 parent afc011c commit 3477d6a

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

app/assets/javascripts/active_admin/active_admin_globalize.js.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ $ ->
148148
$td = $(this).closest('td')
149149
$('.field-translation', $td).hide()
150150
$(".locale-#{$locale}", $td).show()
151+
$(this).parent().children('a.ui-translation-trigger').removeClass('active')
152+
$(this).addClass('active')
151153
e.preventDefault()
152154

153155
translations()
154-

app/assets/stylesheets/active_admin/active_admin_globalize_flags.sass

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
// Used to distantiate inline locale selector
4141
span.inline-locale-selector
4242
margin-right: 10px
43+
> .ui-translation-trigger
44+
opacity: .4
45+
&.empty
46+
filter: grayscale(100%)
47+
&.active
48+
opacity: 1
4349

4450
.field-translation.hidden
4551
display: none

lib/active_admin/globalize/attributes_table_extension.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ def translated_row(*args, &block)
5353
if options[:inline]
5454
''.html_safe.tap do |value|
5555
# Add selectors for inline locale
56-
value << inline_locale_selectors
56+
value << inline_locale_selectors(field, options[:locale], &block)
5757
# Build translations spans
5858
value << field_translations(field, :span, options[:locale], &block)
5959
end
6060
else
6161
content_tag(:div, class: 'activeadmin-translations') do
6262
''.html_safe.tap do |value|
6363
# Render selectors as in translation ui
64-
value << block_locale_selectors
64+
value << block_locale_selectors(field, options[:locale], &block)
6565
# Build translations divs for actual translations
6666
value << field_translations(field, :div, options[:locale], &block)
6767
end
@@ -82,20 +82,18 @@ def translatable?(field)
8282
# @param [String] field field name to render
8383
# @param [Symbol] tag tag to enclose field translation
8484
# @param [Symbol] initial_locale locale to set as not hidden
85-
def field_translations(field, tag, initial_locale)
85+
def field_translations(field, tag, initial_locale, &block)
8686
available_translations.map do |translation|
8787
# Classes for translation span only first element is visible
8888
css_classes = ['field-translation', "locale-#{translation.locale}"]
8989
# Initially only element for selected locale is visible
9090
css_classes.push 'hidden' unless translation.locale == initial_locale.to_sym
9191
# Build content for cell or div using translation locale and given block
92-
content = I18n.with_locale(translation.locale) do
93-
block_given? ? yield(translation) : translation.send(field)
94-
end
92+
content = field_translation_value(translation, field, &block)
9593
# return element
9694
if tag == :span # inline element
9795
# attach class to span if inline
98-
css_classes.push(:empty) if content.blank?
96+
css_classes.push('empty') if content.blank?
9997
content_tag(tag, content.presence || 'Empty', class: css_classes)
10098
else
10199
# block content
@@ -107,25 +105,32 @@ def field_translations(field, tag, initial_locale)
107105
end.join(' ').html_safe
108106
end
109107

110-
def block_locale_selectors
108+
def block_locale_selectors(field, initial_locale, &block)
111109
content_tag(:ul, class: 'available-locales locale-selector') do
112-
available_translations.map(&:locale).map do |locale|
113-
default = 'default' if locale == I18n.default_locale
114-
content_tag(:li, class: 'translation-tab') do
115-
I18n.with_locale(locale) do
116-
content_tag(:a, I18n.t(:"active_admin.globalize.language.#{locale}"), href: ".locale-#{locale}", class: default)
110+
available_translations.map do |translation|
111+
css_classes = ['translation-tab']
112+
css_classes << 'active' if translation.locale == initial_locale.to_sym
113+
css_classes << 'empty' unless content.presence
114+
content_tag(:li, class: css_classes) do
115+
I18n.with_locale(translation.locale) do
116+
default = 'default' if translation.locale == initial_locale.to_sym
117+
content_tag(:a, I18n.t(:"active_admin.globalize.language.#{translation.locale}"), href: ".locale-#{translation.locale}", class: default)
117118
end
118119
end
119120
end.join.html_safe
120121
end
121122
end
122123

123124
# Return flag elements to show the given locale using javascript
124-
def inline_locale_selectors
125+
def inline_locale_selectors(field, initial_locale, &block)
125126
content_tag(:span, class: 'inline-locale-selector') do
126127
available_translations.map do |translation|
128+
content = field_translation_value(translation, field, &block)
129+
css_classes = ['ui-translation-trigger']
130+
css_classes << 'active' if translation.locale == initial_locale.to_sym
131+
css_classes << 'empty' unless content.presence
127132
# Build a link to show the given translation
128-
link_to(flag_icon(translation.locale), '#', class: 'ui-translation-trigger', data: {locale: translation.locale})
133+
link_to(flag_icon(translation.locale), '#', class: css_classes, data: {locale: translation.locale})
129134
end.join(' ').html_safe
130135
end
131136
end
@@ -134,6 +139,11 @@ def available_translations
134139
@record_translations ||= @collection.first.translations.order(:locale)
135140
end
136141

142+
def field_translation_value(translation, field)
143+
I18n.with_locale(translation.locale) do
144+
block_given? ? yield(translation) : translation.send(field)
145+
end
146+
end
137147
end
138148
end
139149
end

spec/features/article_composing_spec.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@
6161
within first_table_row do
6262
page.should have_css 'th', text: 'TITLE'
6363
page.should have_css 'span.field-translation', text: article.title
64+
65+
flag_link(:hu).find(:xpath, '..').should have_css '.empty:not(.active)'
66+
flag_link(:it).find(:xpath, '..').should have_css ':not(.empty):not(.active)'
67+
68+
flag_link(:hu).click # change shown translation
69+
flag_link(:hu).find(:xpath, '..').should have_css '.empty.active'
70+
6471
flag_link(:it).click # change shown translation
72+
flag_link(:it).find(:xpath, '..').should have_css ':not(.empty).active'
73+
6574
page.should have_css 'span.field-translation', text: article.translation_for(:it).title
6675
end
6776

@@ -101,4 +110,4 @@
101110

102111
end
103112

104-
end
113+
end

spec/models/article_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@
1212
let(:article) { create(:localized_article) }
1313
subject { article }
1414

15-
it { should have(2).translations }
15+
it { should have(3).translations }
1616

1717
it 'should have italian translation' do
1818
I18n.with_locale :it do
1919
article.title.should == 'Italian title'
20+
article.body.should == 'Italian Body'
21+
end
22+
end
23+
24+
it 'should have hungarian translation' do
25+
I18n.with_locale :hu do
26+
article.title.should == 'Article title'
27+
article.body.should == 'Hungarian Body'
2028
end
2129
end
2230

2331
end
2432

25-
end
33+
end

spec/support/factories/articles.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

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

1415
end

0 commit comments

Comments
 (0)