diff --git a/config/initializers/routes_hierarchy.rb b/config/initializers/routes_hierarchy.rb index f440e7663..6ed938732 100644 --- a/config/initializers/routes_hierarchy.rb +++ b/config/initializers/routes_hierarchy.rb @@ -7,6 +7,8 @@ module ActionDispatch::Routing::UrlFor case resource.class.name when "Budget::Investment" [resource.budget, resource] + when "Budget::Investment::Milestone" + [resource.investment.budget, resource.investment, resource] when "Legislation::Annotation" [resource.draft_version.process, resource.draft_version, resource] when "Legislation::Proposal", "Legislation::Question" diff --git a/spec/features/admin/banners_spec.rb b/spec/features/admin/banners_spec.rb index cf3dfb1d6..2d52bb682 100644 --- a/spec/features/admin/banners_spec.rb +++ b/spec/features/admin/banners_spec.rb @@ -6,6 +6,11 @@ feature 'Admin banners magement' do login_as(create(:administrator).user) end + it_behaves_like "translatable", + "banner", + "edit_admin_banner_path", + %w[title description] + context "Index" do background do @banner1 = create(:banner, title: "Banner number one", @@ -256,50 +261,5 @@ feature 'Admin banners magement' do visit @edit_banner_url expect(page).not_to have_link "Español" end - - context "Globalize javascript interface" do - - scenario "Highlight current locale", :js do - visit @edit_banner_url - - 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 @edit_banner_url - - 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 @edit_banner_url - - expect(page).to have_field('banner_description_en', with: 'Description in English') - - click_link "Español" - - expect(page).to have_field('banner_description_es', with: 'Descripción en Español') - end - - scenario "Select a locale and add it to the banner form", :js do - visit @edit_banner_url - - select "Français", from: "translation_locale" - - expect(page).to have_link "Français" - - click_link "Français" - - expect(page).to have_field('banner_description_fr') - end - end end end diff --git a/spec/features/admin/site_customization/information_texts_spec.rb b/spec/features/admin/site_customization/information_texts_spec.rb index b5ed5496b..ee00d7d9e 100644 --- a/spec/features/admin/site_customization/information_texts_spec.rb +++ b/spec/features/admin/site_customization/information_texts_spec.rb @@ -7,6 +7,11 @@ feature "Admin custom information texts" do login_as(admin.user) end + it_behaves_like "translatable", + "i18n_content", + "admin_site_customization_information_texts_path", + %w[value] + scenario 'page is correctly loaded' do visit admin_site_customization_information_texts_path @@ -113,60 +118,6 @@ feature "Admin custom information texts" do expect(debate_text.value_en).to eq('Custom debate text') expect(debate_title.value_en).to eq('Custom debate title') end - - context "Javascript interface" do - - scenario "Highlight current locale", :js do - visit admin_site_customization_information_texts_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 - key = "debates.form.debate_title" - content = create(:i18n_content, key: key, value_es: 'Título') - - visit admin_site_customization_information_texts_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 - key = "debates.form.debate_title" - content = create(:i18n_content, key: key, - value_en: 'Title', - value_es: 'Título') - - visit admin_site_customization_information_texts_path - - expect(page).to have_field("contents_content_#{key}values_value_en", with: 'Title') - - click_link "Español" - - expect(page).to have_field("contents_content_#{key}values_value_es", with: 'Título') - end - - scenario "Select a locale and add it to the form", :js do - key = "debates.form.debate_title" - - visit admin_site_customization_information_texts_path - select "Français", from: "translation_locale" - - expect(page).to have_link "Français" - - click_link "Français" - expect(page).to have_field("contents_content_#{key}values_value_fr") - end - end - end end diff --git a/spec/features/translations_spec.rb b/spec/features/translations_spec.rb index 61c674e7b..12bf585c5 100644 --- a/spec/features/translations_spec.rb +++ b/spec/features/translations_spec.rb @@ -10,6 +10,11 @@ feature "Translations" do description_en: "Description in English", description_es: "Descripción en Español") } + it_behaves_like "translatable", + "budget_investment_milestone", + "edit_admin_budget_budget_investment_budget_investment_milestone_path", + %w[description] + background do admin = create(:administrator) login_as(admin.user) @@ -103,51 +108,6 @@ feature "Translations" do expect(page).to have_content('Description in pt-BR') end - context "Globalize javascript interface" do - - scenario "Highlight current locale", :js do - visit @edit_milestone_url - - 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 @edit_milestone_url - - 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 @edit_milestone_url - - expect(page).to have_field('budget_investment_milestone_description_en', with: 'Description in English') - - click_link "Español" - - expect(page).to have_field('budget_investment_milestone_description_es', with: 'Descripción en Español') - end - - scenario "Select a locale and add it to the milestone form", :js do - visit @edit_milestone_url - - select "Français", from: "translation_locale" - - expect(page).to have_link "Français" - - click_link "Français" - - expect(page).to have_field('budget_investment_milestone_description_fr') - end - end - end end diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb new file mode 100644 index 000000000..5ce91d415 --- /dev/null +++ b/spec/shared/features/translatable.rb @@ -0,0 +1,73 @@ +shared_examples "translatable" do |factory_name, path_name, fields| + let(:language_texts) { { es: "en español", en: "in English", fr: "en Français" } } + 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 "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.last # TODO: not sure about first, last, or 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.last, :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