-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
translatable.rb
200 lines (143 loc) · 5.21 KB
/
translatable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
shared_examples "translatable" do |factory_name, path_name, fields|
let(:language_texts) do
{
es: "en español",
en: "in English",
fr: "en Français",
"pt-BR": "Português"
}
end
let(:translatable_class) { build(factory_name).class }
let(:attributes) do
fields.product(%i[en es]).map do |field, locale|
[:"#{field}_#{locale}", text_for(field, locale)]
end.to_h
end
let(:translatable) { create(factory_name, attributes) }
let(:path) { send(path_name, *resource_hierarchy_for(translatable)) }
before { login_as(create(:administrator).user) }
context "Manage translations" do
before do
if translatable_class.name == "I18nContent"
skip "Translation handling is different for site customizations"
end
end
scenario "Add a translation", :js do
visit path
select "Français", from: "translation_locale"
fields.each do |field|
fill_in field_for(field, :fr), with: text_for(field, :fr)
end
click_button update_button_text
visit path
field = fields.sample
expect(page).to have_field(field_for(field, :en), with: text_for(field, :en))
click_link "Español"
expect(page).to have_field(field_for(field, :es), with: text_for(field, :es))
click_link "Français"
expect(page).to have_field(field_for(field, :fr), with: text_for(field, :fr))
end
scenario "Update a translation", :js do
visit path
click_link "Español"
field = fields.sample
updated_text = "Corrección de #{text_for(field, :es)}"
fill_in field_for(field, :es), with: updated_text
click_button update_button_text
visit path
expect(page).to have_field(field_for(field, :en), with: text_for(field, :en))
select('Español', from: 'locale-switcher')
expect(page).to have_field(field_for(field, :es), with: updated_text)
end
scenario "Remove a translation", :js do
visit path
click_link "Español"
click_link "Remove language"
expect(page).not_to have_link "Español"
click_button update_button_text
visit path
expect(page).not_to have_link "Español"
end
scenario 'Change value of a translated field to blank', :js do
possible_blanks = fields.select do |field|
translatable.dup.tap { |duplicate| duplicate.send(:"#{field}=", '') }.valid?
end
skip("can't have translatable blank fields") if possible_blanks.empty?
field = possible_blanks.sample
visit path
expect(page).to have_field(field_for(field, :en), with: text_for(field, :en))
fill_in field_for(field, :en), with: ''
click_button update_button_text
visit path
expect(page).to have_field(field_for(field, :en), with: '')
end
scenario "Add a translation for a locale with non-underscored name", :js do
visit path
field = fields.sample
select "Português brasileiro", from: "translation_locale"
fill_in field_for(field, :pt_br), with: text_for(field, :"pt-BR")
click_button update_button_text
visit path
select('Português brasileiro', from: 'locale-switcher')
expect(page).to have_field(field_for(field, :pt_br), with: text_for(field, :"pt-BR"))
end
end
context "Globalize javascript interface" do
scenario "Highlight current locale", :js do
visit path
expect(find("a.js-globalize-locale-link.is-active")).to have_content "English"
select('Español', from: 'locale-switcher')
expect(find("a.js-globalize-locale-link.is-active")).to have_content "Español"
end
scenario "Highlight selected locale", :js do
visit path
expect(find("a.js-globalize-locale-link.is-active")).to have_content "English"
click_link "Español"
expect(find("a.js-globalize-locale-link.is-active")).to have_content "Español"
end
scenario "Show selected locale form", :js do
visit path
field = fields.sample
expect(page).to have_field(field_for(field, :en), with: text_for(field, :en))
click_link "Español"
expect(page).to have_field(field_for(field, :es), with: text_for(field, :es))
end
scenario "Select a locale and add it to the form", :js do
visit path
select "Français", from: "translation_locale"
expect(page).to have_link "Français"
click_link "Français"
expect(page).to have_field(field_for(fields.sample, :fr))
end
end
end
def text_for(field, locale)
I18n.with_locale(locale) do
"#{translatable_class.human_attribute_name(field)} #{language_texts[locale]}"
end
end
def field_for(field, locale)
if translatable_class.name == "I18nContent"
"contents_content_#{translatable.key}values_#{field}_#{locale}"
else
"#{translatable_class.model_name.singular}_#{field}_#{locale}"
end
end
# FIXME: button texts should be consistent. Right now, buttons don't
# even share the same colour.
def update_button_text
case translatable_class.name
when "Budget::Investment::Milestone"
"Update milestone"
when "AdminNotification"
"Update notification"
when "Poll"
"Update poll"
when "Poll::Question"
"Save"
when "Widget::Card"
"Save card"
else
"Save changes"
end
end