From 5bb5cfa7fb8d3a27da42deb711dc6040b7ea81ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 26 Sep 2018 15:55:36 +0200 Subject: [PATCH 1/4] Make Widget::Card translatable --- .../admin/widget/cards_controller.rb | 5 +++ app/models/widget/card.rb | 9 +++++- app/views/admin/widget/cards/_form.html.erb | 14 ++++++--- ...20800_add_homepage_content_translations.rb | 16 ++++++++++ db/schema.rb | 14 +++++++++ spec/features/admin/widgets/cards_spec.rb | 31 +++++++++++-------- spec/shared/features/translatable.rb | 2 ++ 7 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 db/migrate/20180730120800_add_homepage_content_translations.rb diff --git a/app/controllers/admin/widget/cards_controller.rb b/app/controllers/admin/widget/cards_controller.rb index f51b269c5..f5dce76a1 100644 --- a/app/controllers/admin/widget/cards_controller.rb +++ b/app/controllers/admin/widget/cards_controller.rb @@ -1,4 +1,5 @@ class Admin::Widget::CardsController < Admin::BaseController + include Translatable def new @card = ::Widget::Card.new(header: header_card?) @@ -44,6 +45,7 @@ class Admin::Widget::CardsController < Admin::BaseController params.require(:widget_card).permit( :label, :title, :description, :link_text, :link_url, :button_text, :button_url, :alignment, :header, + *translation_params(Widget::Card), image_attributes: image_attributes ) end @@ -52,4 +54,7 @@ class Admin::Widget::CardsController < Admin::BaseController params[:header_card].present? end + def resource + Widget::Card.find(params[:id]) + end end diff --git a/app/models/widget/card.rb b/app/models/widget/card.rb index b408cc0a9..73bc3eb00 100644 --- a/app/models/widget/card.rb +++ b/app/models/widget/card.rb @@ -1,8 +1,15 @@ class Widget::Card < ActiveRecord::Base include Imageable + # table_name must be set before calls to 'translates' self.table_name = "widget_cards" + translates :label, touch: true + translates :title, touch: true + translates :description, touch: true + translates :link_text, touch: true + globalize_accessors + def self.header where(header: true) end @@ -10,4 +17,4 @@ class Widget::Card < ActiveRecord::Base def self.body where(header: false).order(:created_at) end -end \ No newline at end of file +end diff --git a/app/views/admin/widget/cards/_form.html.erb b/app/views/admin/widget/cards/_form.html.erb index 271f7d206..a823274de 100644 --- a/app/views/admin/widget/cards/_form.html.erb +++ b/app/views/admin/widget/cards/_form.html.erb @@ -1,14 +1,18 @@ -<%= form_for [:admin, @card] do |f| %> +<%= render "admin/shared/globalize_locales", resource: @card %> + +<%= translatable_form_for [:admin, @card] do |f| %> +
- <%= f.text_field :label %> + <%= f.translatable_text_field :label %>
- <%= f.text_field :title %> + <%= f.translatable_text_field :title %> - <%= f.text_area :description, rows: 5 %> + <%= f.translatable_text_area :description, rows: 5 %>
- <%= f.text_field :link_text %> + <%= f.translatable_text_field :link_text %> + <%= f.label :link_text %>
diff --git a/db/migrate/20180730120800_add_homepage_content_translations.rb b/db/migrate/20180730120800_add_homepage_content_translations.rb new file mode 100644 index 000000000..415964536 --- /dev/null +++ b/db/migrate/20180730120800_add_homepage_content_translations.rb @@ -0,0 +1,16 @@ +class AddHomepageContentTranslations < ActiveRecord::Migration + + def self.up + Widget::Card.create_translation_table!( + label: :string, + title: :string, + description: :text, + link_text: :string + ) + end + + def self.down + Widget::Card.drop_translation_table! + end +end + diff --git a/db/schema.rb b/db/schema.rb index 37bfe9912..2d59c76ae 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1404,6 +1404,20 @@ ActiveRecord::Schema.define(version: 20180813141443) do add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope", using: :btree add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope", using: :btree + create_table "widget_card_translations", force: :cascade do |t| + t.integer "widget_card_id", null: false + t.string "locale", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "label" + t.string "title" + t.text "description" + t.string "link_text" + end + + add_index "widget_card_translations", ["locale"], name: "index_widget_card_translations_on_locale", using: :btree + add_index "widget_card_translations", ["widget_card_id"], name: "index_widget_card_translations_on_widget_card_id", using: :btree + create_table "widget_cards", force: :cascade do |t| t.string "title" t.text "description" diff --git a/spec/features/admin/widgets/cards_spec.rb b/spec/features/admin/widgets/cards_spec.rb index 58899f0a2..22b1e6bff 100644 --- a/spec/features/admin/widgets/cards_spec.rb +++ b/spec/features/admin/widgets/cards_spec.rb @@ -7,14 +7,19 @@ feature 'Cards' do login_as(admin) end + it_behaves_like "translatable", + "widget_card", + "edit_admin_widget_card_path", + %w[title description link_text label] + scenario "Create", :js do visit admin_homepage_path click_link "Create card" - fill_in "widget_card_label", with: "Card label" - fill_in "widget_card_title", with: "Card text" - fill_in "widget_card_description", with: "Card description" - fill_in "widget_card_link_text", with: "Link text" + fill_in "widget_card_label_en", with: "Card label" + fill_in "widget_card_title_en", with: "Card text" + fill_in "widget_card_description_en", with: "Card description" + fill_in "widget_card_link_text_en", with: "Link text" fill_in "widget_card_link_url", with: "consul.dev" attach_image_to_card click_button "Create card" @@ -59,10 +64,10 @@ feature 'Cards' do click_link "Edit" end - fill_in "widget_card_label", with: "Card label updated" - fill_in "widget_card_title", with: "Card text updated" - fill_in "widget_card_description", with: "Card description updated" - fill_in "widget_card_link_text", with: "Link text updated" + fill_in "widget_card_label_en", with: "Card label updated" + fill_in "widget_card_title_en", with: "Card text updated" + fill_in "widget_card_description_en", with: "Card description updated" + fill_in "widget_card_link_text_en", with: "Link text updated" fill_in "widget_card_link_url", with: "consul.dev updated" click_button "Save card" @@ -99,10 +104,10 @@ feature 'Cards' do visit admin_homepage_path click_link "Create header" - fill_in "widget_card_label", with: "Header label" - fill_in "widget_card_title", with: "Header text" - fill_in "widget_card_description", with: "Header description" - fill_in "widget_card_link_text", with: "Link text" + fill_in "widget_card_label_en", with: "Header label" + fill_in "widget_card_title_en", with: "Header text" + fill_in "widget_card_description_en", with: "Header description" + fill_in "widget_card_link_text_en", with: "Link text" fill_in "widget_card_link_url", with: "consul.dev" click_button "Create header" @@ -135,4 +140,4 @@ feature 'Cards' do make_visible: true) expect(page).to have_field('widget_card_image_attributes_title', with: "clippy.jpg") end -end \ No newline at end of file +end diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index a8b86cc75..843df2c09 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -192,6 +192,8 @@ def update_button_text "Update poll" when "Poll::Question" "Save" + when "Widget::Card" + "Save card" else "Save changes" end From 86dbe799ba7a9fe0f88e5960d24080db76c65420 Mon Sep 17 00:00:00 2001 From: Marko Lovic Date: Wed, 29 Aug 2018 12:45:49 +0200 Subject: [PATCH 2/4] Remove extra tag element from cards form --- app/views/admin/widget/cards/_form.html.erb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/views/admin/widget/cards/_form.html.erb b/app/views/admin/widget/cards/_form.html.erb index a823274de..c22225e74 100644 --- a/app/views/admin/widget/cards/_form.html.erb +++ b/app/views/admin/widget/cards/_form.html.erb @@ -12,7 +12,6 @@
<%= f.translatable_text_field :link_text %> - <%= f.label :link_text %>
From 832e6178d74aa19be3d17df44be77cd93be44d67 Mon Sep 17 00:00:00 2001 From: Marko Lovic Date: Tue, 28 Aug 2018 18:30:35 +0200 Subject: [PATCH 3/4] Add Spanish translation to Card seed data Instead of using I18n for the field texts and forcing the locale (e.g. `I18n.t('seeds...', locale: :es)`), I'm moving content out of `config/locales/` and into the seed file. This is because this content is less about reacting to different locales (the locale would always be forced so we don't actually use the main i18n functionality), and more about demonstrating the "translatable fields" feature. --- db/dev_seeds/widgets.rb | 64 ++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/db/dev_seeds/widgets.rb b/db/dev_seeds/widgets.rb index 668ce81b6..884bd7cb8 100644 --- a/db/dev_seeds/widgets.rb +++ b/db/dev_seeds/widgets.rb @@ -9,41 +9,73 @@ section "Creating header and cards for the homepage" do end Widget::Card.create!( - title: 'CONSUL', - description: 'Free software for citizen participation.', - link_text: 'More information', + title_en: 'CONSUL', + title_es: 'CONSUL', + + description_en: 'Free software for citizen participation.', + description_es: 'Software libre para la participación ciudadana.', + + link_text_en: 'More information', + link_text_es: 'Más información', + + label_en: 'Welcome to', + label_es: 'Bienvenido a', + link_url: 'help_path', - label: 'Welcome to', header: TRUE, image_attributes: create_image_attachment('header') ) Widget::Card.create!( - title: 'How do debates work?', - description: 'Anyone can open threads on any subject, creating separate spaces where people can discuss the proposed topic. Debates are valued by everybody, to highlight the most important issues.', - link_text: 'More about debates', + title_en: 'How do debates work?', + title_es: '¿Cómo funcionan los debates?', + + description_en: 'Anyone can open threads on any subject, creating separate spaces where people can discuss the proposed topic. Debates are valued by everybody, to highlight the most important issues.', + description_es: 'Cualquiera puede iniciar un debate sobre cualquier tema, creando un espacio separado donde compartir puntos de vista con otras personas. Los debates son valorados por todos para destacar los temas más importantes.', + + link_text_en: 'More about debates', + link_text_es: 'Más sobre debates', + + label_en: 'Debates', + label_es: 'Debates', + link_url: 'https://youtu.be/zU_0UN4VajY', - label: 'Debates', header: FALSE, image_attributes: create_image_attachment('debate') ) Widget::Card.create!( - title: 'How do citizen proposals work?', - description: "A space for everyone to create a citizens' proposal and seek supports. Proposals which reach to enough supports will be voted and so, together we can decide the issues that matter to us.", - link_text: 'More about proposals', + title_en: 'How do citizen proposals work?', + title_es: '¿Cómo funcionan las propuestas ciudadanas?', + + description_en: "A space for everyone to create a citizens' proposal and seek supports. Proposals which reach to enough supports will be voted and so, together we can decide the issues that matter to us.", + description_es: 'Un espacio para que el ciudadano cree una propuesta y busque apoyo. Las propuestas que obtengan el apoyo necesario serán votadas. Así juntos podemos decidir sobre los temas que nos importan.', + + link_text_en: 'More about proposals', + link_text_es: 'Más sobre propuestas', + + label_en: 'Citizen proposals', + label_es: 'Propuestas ciudadanas', + link_url: 'https://youtu.be/ZHqBpT4uCoM', - label: 'Citizen proposals', header: FALSE, image_attributes: create_image_attachment('proposal') ) Widget::Card.create!( - title: 'How do participatory budgets work?', - description: " Participatory budgets allow citizens to propose and decide directly how to spend part of the budget, with monitoring and rigorous evaluation of proposals by the institution. Maximum effectiveness and control with satisfaction for everyone.", - link_text: 'More about Participatory budgets', + title_en: 'How do participatory budgets work?', + title_es: '¿Cómo funcionan los propuestos participativos?', + + description_en: " Participatory budgets allow citizens to propose and decide directly how to spend part of the budget, with monitoring and rigorous evaluation of proposals by the institution. Maximum effectiveness and control with satisfaction for everyone.", + description_es: "Los presupuestos participativos permiten que los ciudadanos propongan y decidan directamente cómo gastar parte del presupuesto, con la supervisión y valoración de la institución. Máxima eficacia y control para la satisfacción de todos", + + link_text_en: 'More about Participatory budgets', + link_text_es: 'Más sobre presupuestos participativos', + + label_en: 'Participatory budgets', + label_es: 'Presupuestos participativos', + link_url: 'https://youtu.be/igQ8KGZdk9c', - label: 'Participatory budgets', header: FALSE, image_attributes: create_image_attachment('budget') ) From 1ffaa680a3c1296eec4016d2f4b612afe8b03fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 12 Sep 2018 19:19:17 +0200 Subject: [PATCH 4/4] Create correct translation when visiting root page We set `I18n.locale = :en` before each test, and so creating a new card will automatically create English translations. So visiting the Spanish page won't show the card, since no Spanish translation exists for it. If we visit the klingon page after doing so, the last used locale (Spanish) will still be used, and so the test will fail. Specifically creating Spanish translations instead of the English ones makes the translations visible when visiting the Spanish homepage. --- spec/features/localization_spec.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/spec/features/localization_spec.rb b/spec/features/localization_spec.rb index 514170252..eb345cb7c 100644 --- a/spec/features/localization_spec.rb +++ b/spec/features/localization_spec.rb @@ -3,11 +3,13 @@ require 'rails_helper' feature 'Localization' do scenario 'Wrong locale' do - card = create(:widget_card, title: 'Bienvenido a CONSUL', - description: 'Software libre para la participación ciudadana.', - link_text: 'Más información', - link_url: 'http://consulproject.org/', - header: true) + Globalize.with_locale(:es) do + create(:widget_card, title: 'Bienvenido a CONSUL', + description: 'Software libre para la participación ciudadana.', + link_text: 'Más información', + link_url: 'http://consulproject.org/', + header: true) + end visit root_path(locale: :es) visit root_path(locale: :klingon)