<%= 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/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