From bfd9032e9d9ec9863923ef1cb0dbb09822fbc217 Mon Sep 17 00:00:00 2001 From: Marko Lovic Date: Wed, 11 Jul 2018 18:32:58 +0200 Subject: [PATCH 1/2] Extract translation partial to be shared within admin Refactor: behaviour should be 100% the same --- app/views/admin/budget_investment_milestones/_form.html.erb | 2 +- .../_globalize_locales.html.erb | 6 +++--- .../information_texts/_globalize_locales.html.erb | 4 ++-- config/locales/en/admin.yml | 5 +++-- config/locales/es/admin.yml | 5 +++-- 5 files changed, 12 insertions(+), 10 deletions(-) rename app/views/admin/{budget_investment_milestones => shared}/_globalize_locales.html.erb (80%) 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 065d506d2..bb7e45351 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: @@ -1377,3 +1375,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 cc1561ffa..3fb310ae0 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: @@ -1378,3 +1376,6 @@ es: submit_header: Guardar encabezado card_title: Editar tarjeta submit_card: Guardar tarjeta + translations: + add_language: Añadir idioma + remove_language: Eliminar idioma From 0fa4b60f39e4bb39903af0afd5b99c1d84f04a00 Mon Sep 17 00:00:00 2001 From: Marko Lovic Date: Fri, 27 Jul 2018 14:57:49 +0200 Subject: [PATCH 2/2] Make Banners translatable --- app/controllers/admin/banners_controller.rb | 12 +- app/models/banner.rb | 4 + app/views/admin/banners/_form.html.erb | 35 ++++- db/dev_seeds/banners.rb | 10 +- .../20180727140800_add_banner_translations.rb | 14 ++ db/schema.rb | 14 +- spec/features/admin/banners_spec.rb | 128 +++++++++++++++++- 7 files changed, 203 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20180727140800_add_banner_translations.rb diff --git a/app/controllers/admin/banners_controller.rb b/app/controllers/admin/banners_controller.rb index 25038b80f..7d3773e24 100644 --- a/app/controllers/admin/banners_controller.rb +++ b/app/controllers/admin/banners_controller.rb @@ -1,4 +1,5 @@ class Admin::BannersController < Admin::BaseController + include Translatable has_filters %w{all with_active with_inactive}, only: :index @@ -41,7 +42,7 @@ class Admin::BannersController < Admin::BaseController :post_started_at, :post_ended_at, :background_color, :font_color, web_section_ids: []] - params.require(:banner).permit(*attributes) + params.require(:banner).permit(*attributes, *translation_params(params[:banner])) end def banner_styles @@ -59,4 +60,13 @@ class Admin::BannersController < Admin::BaseController def banner_sections @banner_sections = WebSection.all end + + def resource + @banner = Banner.find(params[:id]) unless @banner + @banner + end + + def resource_model + Banner + end end diff --git a/app/models/banner.rb b/app/models/banner.rb index 26d683e9c..8ad7b4409 100644 --- a/app/models/banner.rb +++ b/app/models/banner.rb @@ -3,6 +3,10 @@ class Banner < ActiveRecord::Base acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases + translates :title, touch: true + translates :description, touch: true + globalize_accessors locales: [:en, :es, :fr, :nl, :val, :pt_br] + validates :title, presence: true, length: { minimum: 2 } validates :description, presence: true diff --git a/app/views/admin/banners/_form.html.erb b/app/views/admin/banners/_form.html.erb index d9ab0e609..495cc5e8a 100644 --- a/app/views/admin/banners/_form.html.erb +++ b/app/views/admin/banners/_form.html.erb @@ -1,7 +1,13 @@ +<%= render "admin/shared/globalize_locales", resource: @banner %> + <%= form_for [:admin, @banner] do |f| %> <%= render 'errors' %> + <% @banner.globalize_locales.each do |locale| %> + <%= hidden_field_tag "delete_translations[#{locale}]", 0 %> + <% end %> +
    <% date_started_at = @banner.post_started_at.present? ? I18n.localize(@banner.post_started_at) : "" %>
    @@ -28,8 +34,18 @@
    <%= 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