<%= f.label :title, t("admin.banners.banner.title") %>
- <%= f.text_field :title, placeholder: t("admin.banners.banner.title"), label: false,
- data: {js_banner_title: "js_banner_title"} %>
+
+ <% @banner.globalize_locales.each do |locale| %>
+ <% globalize(locale) do %>
+ <%= f.text_field "title_#{locale}",
+ placeholder: t("admin.banners.banner.title"),
+ class: "js-globalize-attribute",
+ data: {js_banner_title: "js_banner_title",
+ locale: locale},
+ style: display_translation?(locale),
+ label: false %>
+ <% end %>
+ <% end %>
@@ -43,10 +59,17 @@
<%= f.label :description, t("admin.banners.banner.description") %>
- <%= f.text_field :description,
- label: false,
- data: {js_banner_description: "js_banner_description"},
- placeholder: t("admin.banners.banner.description") %>
+ <% @banner.globalize_locales.each do |locale| %>
+ <% globalize(locale) do %>
+ <%= f.text_field "description_#{locale}",
+ placeholder: t("admin.banners.banner.description"),
+ class: "js-globalize-attribute",
+ data: {js_banner_description: "js_banner_description",
+ locale: locale},
+ style: display_translation?(locale),
+ label: false %>
+ <% end %>
+ <% end %>
diff --git a/app/views/admin/budget_investment_milestones/_form.html.erb b/app/views/admin/budget_investment_milestones/_form.html.erb
index 4e7017d75..d39c736dd 100644
--- a/app/views/admin/budget_investment_milestones/_form.html.erb
+++ b/app/views/admin/budget_investment_milestones/_form.html.erb
@@ -1,4 +1,4 @@
-<%= render "globalize_locales" %>
+<%= render "admin/shared/globalize_locales", resource: @milestone %>
<%= form_for [:admin, @investment.budget, @investment, @milestone] do |f| %>
diff --git a/app/views/admin/budget_investment_milestones/_globalize_locales.html.erb b/app/views/admin/shared/_globalize_locales.html.erb
similarity index 80%
rename from app/views/admin/budget_investment_milestones/_globalize_locales.html.erb
rename to app/views/admin/shared/_globalize_locales.html.erb
index b4b789ad2..977fa6d9d 100644
--- a/app/views/admin/budget_investment_milestones/_globalize_locales.html.erb
+++ b/app/views/admin/shared/_globalize_locales.html.erb
@@ -1,5 +1,5 @@
<% I18n.available_locales.each do |locale| %>
- <%= link_to t("admin.milestones.form.remove_language"), "#",
+ <%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{neutral_locale(locale)}",
style: show_delete?(locale),
class: 'float-right delete js-delete-language',
@@ -11,7 +11,7 @@
<% I18n.available_locales.each do |locale| %>
<%= link_to name_for_locale(locale), "#",
- style: css_to_display_translation?(@milestone, locale),
+ style: css_to_display_translation?(resource, locale),
class: "js-globalize-locale-link #{highlight_current?(locale)}",
data: { locale: neutral_locale(locale) },
remote: true %>
@@ -22,6 +22,6 @@
<%= select_tag :translation_locale,
options_for_locale_select,
- prompt: t("admin.milestones.form.add_language"),
+ prompt: t("admin.translations.add_language"),
class: "js-globalize-locale" %>
diff --git a/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb b/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb
index 9e90283cd..f69e48897 100644
--- a/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb
+++ b/app/views/admin/site_customization/information_texts/_globalize_locales.html.erb
@@ -1,5 +1,5 @@
<% I18n.available_locales.each do |locale| %>
- <%= link_to t("admin.milestones.form.remove_language"), "#",
+ <%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{neutral_locale(locale)}",
style: show_delete?(locale),
class: 'float-right delete js-delete-language',
@@ -22,6 +22,6 @@
<%= select_tag :translation_locale,
options_for_locale_select,
- prompt: t("admin.milestones.form.add_language"),
+ prompt: t("admin.translations.add_language"),
class: "js-globalize-locale" %>
diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml
index 6fe45a37f..0806b68b9 100644
--- a/config/locales/en/admin.yml
+++ b/config/locales/en/admin.yml
@@ -269,8 +269,6 @@ en:
show_image: "Show image"
documents: "Documents"
form:
- add_language: Add language
- remove_language: Remove language
admin_statuses: Admin investment statuses
no_statuses_defined: There are no defined investment statuses yet
new:
@@ -1382,3 +1380,6 @@ en:
submit_header: Save header
card_title: Edit card
submit_card: Save card
+ translations:
+ remove_language: Remove language
+ add_language: Add language
diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml
index fabad8a8d..3ef8039ae 100644
--- a/config/locales/es/admin.yml
+++ b/config/locales/es/admin.yml
@@ -270,8 +270,6 @@ es:
show_image: "Mostrar imagen"
documents: "Documentos"
form:
- add_language: Añadir idioma
- remove_language: Eliminar idioma
admin_statuses: Gestionar estados de proyectos
no_statuses_defined: No hay estados definidos
new:
@@ -1383,3 +1381,6 @@ es:
submit_header: Guardar encabezado
card_title: Editar tarjeta
submit_card: Guardar tarjeta
+ translations:
+ add_language: Añadir idioma
+ remove_language: Eliminar idioma
diff --git a/db/dev_seeds/banners.rb b/db/dev_seeds/banners.rb
index d0e24e16b..fa2710a06 100644
--- a/db/dev_seeds/banners.rb
+++ b/db/dev_seeds/banners.rb
@@ -3,12 +3,20 @@ section "Creating banners" do
title = Faker::Lorem.sentence(word_count = 3)
description = Faker::Lorem.sentence(word_count = 12)
target_url = Rails.application.routes.url_helpers.proposal_path(proposal)
- banner = Banner.create!(title: title,
+ banner = Banner.new(title: title,
description: description,
target_url: target_url,
post_started_at: rand((Time.current - 1.week)..(Time.current - 1.day)),
post_ended_at: rand((Time.current - 1.day)..(Time.current + 1.week)),
created_at: rand((Time.current - 1.week)..Time.current))
+ I18n.available_locales.map do |locale|
+ neutral_locale = locale.to_s.downcase.underscore.to_sym
+ Globalize.with_locale(neutral_locale) do
+ banner.description = "Description for locale #{locale}"
+ banner.title = "Title for locale #{locale}"
+ banner.save!
+ end
+ end
end
end
diff --git a/db/migrate/20180727140800_add_banner_translations.rb b/db/migrate/20180727140800_add_banner_translations.rb
new file mode 100644
index 000000000..7678a34a1
--- /dev/null
+++ b/db/migrate/20180727140800_add_banner_translations.rb
@@ -0,0 +1,14 @@
+class AddBannerTranslations < ActiveRecord::Migration
+
+ def self.up
+ Banner.create_translation_table!(
+ title: :string,
+ description: :text
+ )
+ end
+
+ def self.down
+ Banner.drop_translation_table!
+ end
+end
+
diff --git a/db/schema.rb b/db/schema.rb
index 9b5c93c8e..9c568058e 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20180718115545) do
+ActiveRecord::Schema.define(version: 20180727140800) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -81,6 +81,18 @@ ActiveRecord::Schema.define(version: 20180718115545) do
t.datetime "updated_at", null: false
end
+ create_table "banner_translations", force: :cascade do |t|
+ t.integer "banner_id", null: false
+ t.string "locale", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.string "title"
+ t.text "description"
+ end
+
+ add_index "banner_translations", ["banner_id"], name: "index_banner_translations_on_banner_id", using: :btree
+ add_index "banner_translations", ["locale"], name: "index_banner_translations_on_locale", using: :btree
+
create_table "banners", force: :cascade do |t|
t.string "title", limit: 80
t.string "description"
diff --git a/spec/features/admin/banners_spec.rb b/spec/features/admin/banners_spec.rb
index 59db31b27..cf3dfb1d6 100644
--- a/spec/features/admin/banners_spec.rb
+++ b/spec/features/admin/banners_spec.rb
@@ -84,8 +84,8 @@ feature 'Admin banners magement' do
click_link "Create banner"
- fill_in 'banner_title', with: 'Such banner'
- fill_in 'banner_description', with: 'many text wow link'
+ fill_in 'banner_title_en', with: 'Such banner'
+ fill_in 'banner_description_en', with: 'many text wow link'
fill_in 'banner_target_url', with: 'https://www.url.com'
last_week = Time.current - 7.days
next_week = Time.current + 7.days
@@ -110,7 +110,7 @@ feature 'Admin banners magement' do
fill_in 'banner_background_color', with: '#850000'
fill_in 'banner_font_color', with: '#ffb2b2'
- fill_in 'banner_title', with: 'Fun with flags'
+ fill_in 'banner_title_en', with: 'Fun with flags'
# This last step simulates the blur event on the page. The color pickers and the text_fields
# has onChange events that update each one when the other changes, but this is only fired when
@@ -141,8 +141,8 @@ feature 'Admin banners magement' do
click_link "Edit banner"
- fill_in 'banner_title', with: 'Modified title'
- fill_in 'banner_description', with: 'Edited text'
+ fill_in 'banner_title_en', with: 'Modified title'
+ fill_in 'banner_description_en', with: 'Edited text'
page.find("body").click
@@ -184,4 +184,122 @@ feature 'Admin banners magement' do
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
+
+ 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