From a64a290392d826159e611d05831dbe85da503dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 17 Sep 2018 20:16:54 +0200 Subject: [PATCH 1/4] Extract `commentable_path` to an initializer By doing so and including it in ActionDispatch::Routing::UrlFor, we make it available in controllers, helpers and specs, and so we can remove the duplication we had there with methods dealing with the same problem. Even if monkey-patching is ugly, using a different module and executing ActionDispatch::Routing::UrlFor.send(:include, MyModule) wouldn't make the method available in the controller. --- app/controllers/notifications_controller.rb | 14 +---- app/helpers/comments_helper.rb | 17 +----- config/initializers/routes_hierarchy.rb | 58 ++++++++++++++++++++ spec/support/common_actions/notifications.rb | 15 +---- 4 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 config/initializers/routes_hierarchy.rb diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index ee6878502..2c4fd3e9f 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -36,19 +36,11 @@ class NotificationsController < ApplicationController end private - def linkable_resource_path(notification) - case notification.linkable_resource.class.name - when "Budget::Investment" - budget_investment_path @notification.linkable_resource.budget, @notification.linkable_resource - when "Topic" - community_topic_path @notification.linkable_resource.community, @notification.linkable_resource + if notification.linkable_resource.is_a?(AdminNotification) + notification.linkable_resource.link || notifications_path else - if @notification.linkable_resource.is_a?(AdminNotification) - @notification.linkable_resource.link || notifications_path - else - url_for @notification.linkable_resource - end + polymorphic_hierarchy_path(notification.linkable_resource) end end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index e5060dad2..ab81c76c6 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -41,22 +41,7 @@ module CommentsHelper end def commentable_path(comment) - commentable = comment.commentable - - case comment.commentable_type - when "Budget::Investment" - budget_investment_path(commentable.budget_id, commentable) - when "Legislation::Question" - legislation_process_question_path(commentable.process, commentable) - when "Legislation::Annotation" - legislation_process_draft_version_annotation_path(commentable.draft_version.process, commentable.draft_version, commentable) - when "Topic" - community_topic_path(commentable.community, commentable) - when "Legislation::Proposal" - legislation_process_proposal_path(commentable.legislation_process_id, commentable) - else - commentable - end + polymorphic_hierarchy_path(comment.commentable) end def user_level_class(comment) diff --git a/config/initializers/routes_hierarchy.rb b/config/initializers/routes_hierarchy.rb new file mode 100644 index 000000000..f440e7663 --- /dev/null +++ b/config/initializers/routes_hierarchy.rb @@ -0,0 +1,58 @@ +# This module is expanded in order to make it easier to use polymorphic +# routes with nested resources. +# HACK: is there a way to avoid monkey-patching here? Using helpers is +# a similar use of a global namespace too... +module ActionDispatch::Routing::UrlFor + def resource_hierarchy_for(resource) + case resource.class.name + when "Budget::Investment" + [resource.budget, resource] + when "Legislation::Annotation" + [resource.draft_version.process, resource.draft_version, resource] + when "Legislation::Proposal", "Legislation::Question" + [resource.process, resource] + when "Topic" + [resource.community, resource] + else + resource + end + end + + def polymorphic_hierarchy_path(resource) + # Unfortunately, we can't use polymorphic routes because there + # are cases where polymorphic_path doesn't get the named routes properly. + # Example: + # + # polymorphic_path([legislation_proposal.process, legislation_proposal]) + # + # That line tries to find legislation_process_legislation_proposal_path + # while the correct route would be legislation_process_proposal_path + # + # We probably need to define routes differently in order to be able to use + # polymorphic_path which might be possible with Rails 5.1 `direct` and + # `resolve` methods. + + resources = resource_hierarchy_for(resource) + + case resource.class.name + when "Budget::Investment" + # polymorphic_path would return budget_budget_investment_path + budget_investment_path(*resources) + when "Legislation::Annotation" + # polymorphic_path would return: + # "legislation_process_legislation_draft_version_legislation_annotation_path" + legislation_process_draft_version_annotation_path(*resources) + when "Legislation::Proposal" + # polymorphic_path would return legislation_process_legislation_proposal_path + legislation_process_proposal_path(*resources) + when "Legislation::Question" + # polymorphic_path would return legislation_process_legislation_question_path + legislation_process_question_path(*resources) + when "Poll::Question" + # polymorphic_path would return poll_question_path + question_path(*resources) + else + polymorphic_path(resources) + end + end +end diff --git a/spec/support/common_actions/notifications.rb b/spec/support/common_actions/notifications.rb index e387ce1fd..264d0ef83 100644 --- a/spec/support/common_actions/notifications.rb +++ b/spec/support/common_actions/notifications.rb @@ -32,20 +32,7 @@ module Notifications end def path_for(resource) - nested_path_for(resource) || url_for([resource, only_path: true]) - end - - def nested_path_for(resource) - case resource.class.name - when "Legislation::Question" - legislation_process_question_path(resource.process, resource) - when "Legislation::Proposal" - legislation_process_proposal_path(resource.process, resource) - when "Budget::Investment" - budget_investment_path(resource.budget, resource) - else - false - end + polymorphic_hierarchy_path(resource) end def error_message(resource_model = nil) From 726110c91e5f54ba7a019239767a3870ac2f0df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 17 Sep 2018 20:23:23 +0200 Subject: [PATCH 2/4] Share Globalize JavaScript interface specs --- config/initializers/routes_hierarchy.rb | 2 + spec/features/admin/banners_spec.rb | 50 ++----------- .../information_texts_spec.rb | 59 ++------------- spec/features/translations_spec.rb | 50 ++----------- spec/shared/features/translatable.rb | 73 +++++++++++++++++++ 5 files changed, 90 insertions(+), 144 deletions(-) create mode 100644 spec/shared/features/translatable.rb 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 From 612fdb09dddeb24c044e88fb9678c6833be1b393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 28 Aug 2018 17:44:46 +0200 Subject: [PATCH 3/4] Share translatable specs --- spec/features/admin/banners_spec.rb | 74 ----------- .../budget_investment_milestones_spec.rb | 5 + spec/features/translations_spec.rb | 113 ---------------- spec/shared/features/translatable.rb | 124 +++++++++++++++++- 4 files changed, 126 insertions(+), 190 deletions(-) delete mode 100644 spec/features/translations_spec.rb diff --git a/spec/features/admin/banners_spec.rb b/spec/features/admin/banners_spec.rb index 2d52bb682..6eaaea486 100644 --- a/spec/features/admin/banners_spec.rb +++ b/spec/features/admin/banners_spec.rb @@ -188,78 +188,4 @@ feature 'Admin banners magement' do visit admin_root_path expect(page).not_to have_content 'Ugly banner' end - - context "Translations" do - - let(:banner) { create(:banner, title_en: "Title in English", - title_es: "Título en Español", - target_url: 'http://url.com', - description_en: "Description in English", - description_es: "Descripción en Español") } - - before do - @edit_banner_url = edit_admin_banner_path(banner) - end - - scenario "Add a translation", :js do - visit @edit_banner_url - - select "Français", from: "translation_locale" - fill_in 'banner_title_fr', with: 'Titre en Français' - fill_in 'banner_description_fr', with: 'Description en Français' - - click_button 'Save changes' - - 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') - - click_link "Français" - expect(page).to have_field('banner_description_fr', with: 'Description en Français') - end - - scenario "Update a translation", :js do - banner.update_attributes(target_url: 'http://www.url.com', - post_started_at: (Time.current - 4.days), - post_ended_at: (Time.current + 10.days)) - - section = create(:web_section, name: 'debates') - create(:banner_section, web_section: section, banner_id: banner.id) - - visit @edit_banner_url - - click_link "Español" - fill_in 'banner_title_es', with: 'Título correcto en Español' - - click_button 'Save changes' - - visit debates_path - - within('.banner') do - expect(page).to have_content("Description in English") - end - - select('Español', from: 'locale-switcher') - - within('.banner') do - expect(page).to have_content('Título correcto en Español') - end - end - - scenario "Remove a translation", :js do - - visit @edit_banner_url - - click_link "Español" - click_link "Remove language" - - expect(page).not_to have_link "Español" - - click_button "Save changes" - visit @edit_banner_url - expect(page).not_to have_link "Español" - end - end end diff --git a/spec/features/admin/budget_investment_milestones_spec.rb b/spec/features/admin/budget_investment_milestones_spec.rb index a4272432d..3b2777792 100644 --- a/spec/features/admin/budget_investment_milestones_spec.rb +++ b/spec/features/admin/budget_investment_milestones_spec.rb @@ -9,6 +9,11 @@ feature 'Admin budget investment milestones' do @investment = create(:budget_investment) end + it_behaves_like "translatable", + "budget_investment_milestone", + "edit_admin_budget_budget_investment_budget_investment_milestone_path", + %w[description] + context "Index" do scenario 'Displaying milestones' do milestone = create(:budget_investment_milestone, investment: @investment) diff --git a/spec/features/translations_spec.rb b/spec/features/translations_spec.rb deleted file mode 100644 index 12bf585c5..000000000 --- a/spec/features/translations_spec.rb +++ /dev/null @@ -1,113 +0,0 @@ -require 'rails_helper' - -feature "Translations" do - - context "Milestones" do - - let(:investment) { create(:budget_investment) } - let(:milestone) { create(:budget_investment_milestone, - investment: investment, - 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) - end - - before do - @edit_milestone_url = edit_admin_budget_budget_investment_budget_investment_milestone_path(investment.budget, investment, milestone) - end - - scenario "Add a translation", :js do - visit @edit_milestone_url - - select "Français", from: "translation_locale" - fill_in 'budget_investment_milestone_description_fr', with: 'Description en Français' - - click_button 'Update milestone' - expect(page).to have_content "Milestone updated successfully" - - 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') - - click_link "Français" - expect(page).to have_field('budget_investment_milestone_description_fr', with: 'Description en Français') - end - - scenario "Update a translation", :js do - visit @edit_milestone_url - - click_link "Español" - fill_in 'budget_investment_milestone_description_es', with: 'Descripción correcta en Español' - - click_button 'Update milestone' - expect(page).to have_content "Milestone updated successfully" - - visit budget_investment_path(investment.budget, investment) - - click_link("Milestones (1)") - expect(page).to have_content("Description in English") - - select('Español', from: 'locale-switcher') - click_link("Seguimiento (1)") - - expect(page).to have_content("Descripción correcta en Español") - end - - scenario "Remove a translation", :js do - visit @edit_milestone_url - - click_link "Español" - click_link "Remove language" - - expect(page).not_to have_link "Español" - - click_button "Update milestone" - visit @edit_milestone_url - expect(page).not_to have_link "Español" - end - - scenario 'Change value of a translated field to blank' do - visit @edit_milestone_url - - fill_in 'budget_investment_milestone_description_en', with: '' - - click_button "Update milestone" - expect(page).to have_content "Milestone updated successfully" - - expect(page).to have_content "Milestone updated successfully" - expect(page).not_to have_content "Description in English" - end - - scenario "Add a translation for a locale with non-underscored name", :js do - visit @edit_milestone_url - - select "Português", from: "translation_locale" - fill_in 'budget_investment_milestone_description_pt_br', with: 'Description in pt-BR' - - click_button 'Update milestone' - expect(page).to have_content "Milestone updated successfully" - - visit budget_investment_path(investment.budget, investment) - - click_link("Milestones (1)") - expect(page).to have_content("Description in English") - - select('Português', from: 'locale-switcher') - click_link("Milestones (1)") - - expect(page).to have_content('Description in pt-BR') - end - - end - -end diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index 5ce91d415..d3fb86477 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -1,5 +1,13 @@ shared_examples "translatable" do |factory_name, path_name, fields| - let(:language_texts) { { es: "en español", en: "in English", fr: "en Français" } } + 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 @@ -12,6 +20,105 @@ shared_examples "translatable" do |factory_name, path_name, fields| 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 + + fill_in field_for(field, :es), with: "Corrección de #{text_for(field, :es)}" + + click_button update_button_text + + visit path + + expect(page).to have_content(text_for(field, :en)) + + select('Español', from: 'locale-switcher') + + expect(page).to have_content("Corrección de #{text_for(field, :es)}") + 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' 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_content text_for(field, :en) + + fill_in field_for(field, :en), with: '' + click_button update_button_text + + visit path + expect(page).not_to have_content text_for(field, :en) + end + + scenario "Add a translation for a locale with non-underscored name", :js do + visit path + field = fields.sample + + select "Português", 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', from: 'locale-switcher') + + expect(page).to have_content(text_for(field, :"pt-BR")) + end + end + context "Globalize javascript interface" do scenario "Highlight current locale", :js do visit path @@ -35,7 +142,7 @@ shared_examples "translatable" do |factory_name, path_name, fields| scenario "Show selected locale form", :js do visit path - field = fields.last # TODO: not sure about first, last, or sample + field = fields.sample expect(page).to have_field(field_for(field, :en), with: text_for(field, :en)) @@ -53,7 +160,7 @@ shared_examples "translatable" do |factory_name, path_name, fields| click_link "Français" - expect(page).to have_field(field_for(fields.last, :fr)) + expect(page).to have_field(field_for(fields.sample, :fr)) end end end @@ -71,3 +178,14 @@ def field_for(field, locale) "#{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" + else + "Save changes" + end +end From 0552616764037002b4f5ad9b52eec4d573be190f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 29 Aug 2018 14:50:19 +0200 Subject: [PATCH 4/4] Use `have_field` to detect text input contents Using `have_content` detected textareas, but not text input contents. All credit to Marko (mlovic) for finding the issue and suggesting the implemented solution. --- spec/shared/features/translatable.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index d3fb86477..215148fdc 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -55,18 +55,19 @@ shared_examples "translatable" do |factory_name, path_name, fields| click_link "Español" field = fields.sample + updated_text = "Corrección de #{text_for(field, :es)}" - fill_in field_for(field, :es), with: "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_content(text_for(field, :en)) + expect(page).to have_field(field_for(field, :en), with: text_for(field, :en)) select('Español', from: 'locale-switcher') - expect(page).to have_content("Corrección de #{text_for(field, :es)}") + expect(page).to have_field(field_for(field, :es), with: updated_text) end scenario "Remove a translation", :js do @@ -93,13 +94,13 @@ shared_examples "translatable" do |factory_name, path_name, fields| field = possible_blanks.sample visit path - expect(page).to have_content text_for(field, :en) + 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).not_to have_content text_for(field, :en) + expect(page).to have_field(field_for(field, :en), with: '') end scenario "Add a translation for a locale with non-underscored name", :js do @@ -115,7 +116,7 @@ shared_examples "translatable" do |factory_name, path_name, fields| select('Português', from: 'locale-switcher') - expect(page).to have_content(text_for(field, :"pt-BR")) + expect(page).to have_field(field_for(field, :pt_br), with: text_for(field, :"pt-BR")) end end