Skip to content
This repository was archived by the owner on Nov 30, 2018. It is now read-only.

Commit ee70898

Browse files
committed
Added empty spans for blank elements
1 parent 082e914 commit ee70898

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

lib/active_admin/globalize/attributes_table_extension.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,19 @@ def field_translations(field, tag, initial_locale)
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
91-
# build span with translation
92-
content_tag(tag, class: css_classes) do
93-
# If block given call it with translation model, else return raw value of field
94-
block_given? ? yield(translation) : translation.send(field)
91+
# Build content for cell or div
92+
content = block_given? ? yield(translation) : translation.send(field)
93+
# return element
94+
if tag == :span # inline element
95+
# attach class to span if inline
96+
css_classes.push(:empty) if content.blank?
97+
content_tag(tag, content.presence || 'Empty', class: css_classes)
98+
else
99+
# block content
100+
content_tag(tag, class: css_classes) do
101+
# Return content or empty span
102+
content.presence || content_tag(:span, 'Empty', class: 'empty')
103+
end
95104
end
96105
end.join(' ').html_safe
97106
end

spec/dummy/app/models/article.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ class Article < ActiveRecord::Base
22
attr_accessible :title, :body, :translations_attributes
33

44
# Translated fields with globalize and for active admin
5-
active_admin_translates :title, :body do
6-
# Here I'm inside the Person::Translation which is an AR class
7-
validates_presence_of :title, :body
8-
end
5+
active_admin_translates :title, :body
96

107
end

spec/features/article_composing_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@
7777

7878
end
7979

80+
scenario 'Viewing empty translations' do
81+
# create empty translations for it
82+
I18n.with_locale(:de) { article.update_attributes! title: '', body: '' }
83+
# Reload article page
84+
visit admin_article_path(article)
85+
86+
# First row shows default locale with label title
87+
within first_table_row do
88+
flag_link(:de).click # change shown translation
89+
page.should have_css 'span.field-translation.empty', text: 'EMPTY'
90+
end
91+
92+
# Third table has a block translation element
93+
within third_table_row do
94+
tab_link(:de).click # change shown translation
95+
page.should have_css 'div.field-translation span.empty', text: 'EMPTY'
96+
end
97+
98+
end
99+
80100
end
81101

82102
end

0 commit comments

Comments
 (0)