Make Banners translatable

This commit is contained in:
Marko Lovic
2018-07-27 14:57:49 +02:00
committed by María Checa
parent bfd9032e9d
commit 0fa4b60f39
7 changed files with 203 additions and 14 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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 %>
<div class="row">
<% date_started_at = @banner.post_started_at.present? ? I18n.localize(@banner.post_started_at) : "" %>
<div class="small-12 medium-3 column">
@@ -28,8 +34,18 @@
<div class="row">
<div class="small-12 medium-6 column">
<%= 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 %>
</div>
<div class="small-12 medium-6 column">
@@ -43,10 +59,17 @@
<div class="row">
<div class="small-12 column">
<%= 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 %>
</div>
</div>

View File

@@ -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

View File

@@ -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

View File

@@ -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"

View File

@@ -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