From d5560ddabdf781416664086de3ebd372d0f57662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 14:47:13 +0200 Subject: [PATCH 01/12] Created migration to add `locale` to `site_customization_pages` --- .../20170702105956_add_locale_to_site_customization_pages.rb | 5 +++++ db/schema.rb | 3 ++- lib/tasks/settings.rake | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20170702105956_add_locale_to_site_customization_pages.rb diff --git a/db/migrate/20170702105956_add_locale_to_site_customization_pages.rb b/db/migrate/20170702105956_add_locale_to_site_customization_pages.rb new file mode 100644 index 000000000..2675d9fa7 --- /dev/null +++ b/db/migrate/20170702105956_add_locale_to_site_customization_pages.rb @@ -0,0 +1,5 @@ +class AddLocaleToSiteCustomizationPages < ActiveRecord::Migration + def change + add_column :site_customization_pages, :locale, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 4cfa7229f..ef7abce74 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: 20170620132731) do +ActiveRecord::Schema.define(version: 20170702105956) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -791,6 +791,7 @@ ActiveRecord::Schema.define(version: 20170620132731) do t.string "status", default: "draft" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "locale" end create_table "spending_proposals", force: :cascade do |t| diff --git a/lib/tasks/settings.rake b/lib/tasks/settings.rake index 29dc256bb..61c5fd5c0 100644 --- a/lib/tasks/settings.rake +++ b/lib/tasks/settings.rake @@ -4,7 +4,7 @@ namespace :settings do task per_page_code_migration: :environment do per_page_code_setting = Setting.where(key: 'per_page_code').first - Setting['per_page_code_head'] = per_page_code_setting&.value.to_s if Setting.where(key: 'per_page_code_head').first.blank? + Setting['per_page_code_head'] = per_page_code_setting.value.to_s if Setting.where(key: 'per_page_code_head').first.blank? per_page_code_setting.destroy if per_page_code_setting.present? end From 720a2af0467efa8e05ae23d4fdd94ccdb3966308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 14:49:56 +0200 Subject: [PATCH 02/12] Update pages controller and views Added new attribute `locale` to pages index list and form. --- app/controllers/admin/site_customization/pages_controller.rb | 3 ++- app/views/admin/site_customization/pages/_form.html.erb | 4 ++++ app/views/admin/site_customization/pages/index.html.erb | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/site_customization/pages_controller.rb b/app/controllers/admin/site_customization/pages_controller.rb index 4d92a6a1e..dd65fb52e 100644 --- a/app/controllers/admin/site_customization/pages_controller.rb +++ b/app/controllers/admin/site_customization/pages_controller.rb @@ -38,7 +38,8 @@ class Admin::SiteCustomization::PagesController < Admin::SiteCustomization::Base :content, :more_info_flag, :print_content_flag, - :status + :status, + :locale ) end end diff --git a/app/views/admin/site_customization/pages/_form.html.erb b/app/views/admin/site_customization/pages/_form.html.erb index 2c0d38ba6..771d6c9e7 100644 --- a/app/views/admin/site_customization/pages/_form.html.erb +++ b/app/views/admin/site_customization/pages/_form.html.erb @@ -30,6 +30,10 @@
<% end %> +
+ <%= f.label :locale %> + <%= f.select :locale, I18n.available_locales, { default: nil, label: false, include_blank: true } %> +
<%= f.label :slug %> <%= f.text_field :slug, label: false, size: 80, maxlength: 80 %> diff --git a/app/views/admin/site_customization/pages/index.html.erb b/app/views/admin/site_customization/pages/index.html.erb index 18ba5a9b7..85c3c7fc8 100644 --- a/app/views/admin/site_customization/pages/index.html.erb +++ b/app/views/admin/site_customization/pages/index.html.erb @@ -15,6 +15,7 @@ <%= t("admin.site_customization.pages.page.created_at") %> <%= t("admin.site_customization.pages.page.updated_at") %> <%= t("admin.site_customization.pages.page.status") %> + <%= t("admin.site_customization.pages.page.locale") %> <%= t("admin.actions.actions") %> @@ -27,6 +28,7 @@ <%= I18n.l page.created_at, format: :short %> <%= I18n.l page.created_at, format: :short %> <%= t("admin.site_customization.pages.page.status_#{page.status}") %> + <%= page.locale %> <% if page.status == "published" %> From f3b8d70d1215a6fb7ef369062533d4ef118d723f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 14:50:57 +0200 Subject: [PATCH 03/12] Added new locale to pages form --- config/locales/activerecord.en.yml | 1 + config/locales/activerecord.es.yml | 1 + config/locales/activerecord.fr.yml | 1 + config/locales/activerecord.nl.yml | 3 ++- 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index d45c569d4..06ce24fd6 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -164,6 +164,7 @@ en: updated_at: Updated at more_info_flag: Show in more information page print_content_flag: Print content button + locale: Language site_customization/image: name: Name image: Image diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 678053f84..d00b4ab88 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -159,6 +159,7 @@ es: updated_at: última actualización more_info_flag: Mostrar en la página de más información print_content_flag: Botón de imprimir contenido + locale: Idioma site_customization/image: name: Nombre image: Imagen diff --git a/config/locales/activerecord.fr.yml b/config/locales/activerecord.fr.yml index b977176ce..b588d37dc 100644 --- a/config/locales/activerecord.fr.yml +++ b/config/locales/activerecord.fr.yml @@ -139,6 +139,7 @@ fr: updated_at: Mis-à-jour le more_info_flag: Montrer dans la page Plus d'informations print_content_flag: Bouton impression du contenu + locale: Langue site_customization/image: name: Nom image: Image diff --git a/config/locales/activerecord.nl.yml b/config/locales/activerecord.nl.yml index 5e930ac65..1c8eee00e 100644 --- a/config/locales/activerecord.nl.yml +++ b/config/locales/activerecord.nl.yml @@ -135,7 +135,8 @@ nl: title: Titel updated_at: Bijgewerkt op more_info_flag: Toon in extra informatie pagina - print_content_flag: Print inhoud + print_content_flag: Print inhoud + locale: Taal site_customization/image: name: Naam image: Beeld From ed7b7f0361ce6eaa900edf8f5e08241a19ba7313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 14:54:11 +0200 Subject: [PATCH 04/12] Added scope to pages model New scope to use when listing pages to show only the ones with current locale. --- app/models/site_customization/page.rb | 1 + app/views/pages/more_info/_other.html.erb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/site_customization/page.rb b/app/models/site_customization/page.rb index c2a9b1467..d064187c1 100644 --- a/app/models/site_customization/page.rb +++ b/app/models/site_customization/page.rb @@ -9,6 +9,7 @@ class SiteCustomization::Page < ActiveRecord::Base scope :published, -> { where(status: 'published').order('id DESC') } scope :with_more_info_flag, -> { where(status: 'published', more_info_flag: true).order('id ASC') } + scope :with_same_locale, -> { where(locale: I18n.locale).order('id ASC') } def url "/#{slug}" diff --git a/app/views/pages/more_info/_other.html.erb b/app/views/pages/more_info/_other.html.erb index c33f17fd6..5ed1b9f4e 100644 --- a/app/views/pages/more_info/_other.html.erb +++ b/app/views/pages/more_info/_other.html.erb @@ -3,7 +3,7 @@
  • <%= link_to t("pages.more_info.other.how_to_use", org_name: setting['org_name']), how_to_use_path %>
  • - <% SiteCustomization::Page.with_more_info_flag.each do |custom_page| %> + <% SiteCustomization::Page.with_more_info_flag.with_same_locale.each do |custom_page| %>
  • <%= link_to custom_page.title, page_path(custom_page.slug) %>
  • <% end %>
From 8894ec4f7c575fc15649a419da238ba775598f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 21:28:13 +0200 Subject: [PATCH 05/12] Pages form improvements Added `locale` validation to model and improved its form select tag performance. --- app/models/site_customization/page.rb | 1 + app/views/admin/site_customization/pages/_form.html.erb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/site_customization/page.rb b/app/models/site_customization/page.rb index d064187c1..2b60dea81 100644 --- a/app/models/site_customization/page.rb +++ b/app/models/site_customization/page.rb @@ -6,6 +6,7 @@ class SiteCustomization::Page < ActiveRecord::Base format: { with: /\A[0-9a-zA-Z\-_]*\Z/, message: :slug_format } validates :title, presence: true validates :status, presence: true, inclusion: { in: VALID_STATUSES } + validates :locale, presence: true scope :published, -> { where(status: 'published').order('id DESC') } scope :with_more_info_flag, -> { where(status: 'published', more_info_flag: true).order('id ASC') } diff --git a/app/views/admin/site_customization/pages/_form.html.erb b/app/views/admin/site_customization/pages/_form.html.erb index 771d6c9e7..a8c2788b3 100644 --- a/app/views/admin/site_customization/pages/_form.html.erb +++ b/app/views/admin/site_customization/pages/_form.html.erb @@ -32,7 +32,7 @@
<%= f.label :locale %> - <%= f.select :locale, I18n.available_locales, { default: nil, label: false, include_blank: true } %> + <%= f.select :locale, I18n.available_locales.map { |l| [name_for_locale(l), l.to_s] }, { include_blank: true, label: false } %>
<%= f.label :slug %> From dca346bd4c04d2c0dc5759b7f094473df0d30ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 22:13:40 +0200 Subject: [PATCH 06/12] Fixed tests Added new custom pages attribute to tests to make them run properly. --- spec/factories.rb | 1 + .../admin/site_customization/pages_spec.rb | 1 + .../site_customization/custom_pages_spec.rb | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 31a14ed50..8a1abf22b 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -733,6 +733,7 @@ LOREM_IPSUM more_info_flag false print_content_flag false status 'draft' + locale 'en' trait :published do status "published" diff --git a/spec/features/admin/site_customization/pages_spec.rb b/spec/features/admin/site_customization/pages_spec.rb index 3e940ea69..1b09a8f01 100644 --- a/spec/features/admin/site_customization/pages_spec.rb +++ b/spec/features/admin/site_customization/pages_spec.rb @@ -30,6 +30,7 @@ feature "Admin custom pages" do fill_in "site_customization_page_subtitle", with: "Page subtitle" fill_in "site_customization_page_slug", with: "example-page" fill_in "site_customization_page_content", with: "This page is about..." + select 'English', from:"site_customization_page_locale" click_button "Create Custom page" diff --git a/spec/features/site_customization/custom_pages_spec.rb b/spec/features/site_customization/custom_pages_spec.rb index 2e2fac84a..640d41848 100644 --- a/spec/features/site_customization/custom_pages_spec.rb +++ b/spec/features/site_customization/custom_pages_spec.rb @@ -7,7 +7,8 @@ feature "Custom Pages" do slug: "conditions", title: "Custom conditions", content: "New text for conditions page", - print_content_flag: true + print_content_flag: true, + locale: "en" ) visit custom_page.url @@ -23,7 +24,8 @@ feature "Custom Pages" do slug: "conditions", title: "Custom conditions", content: "New text for conditions page", - print_content_flag: true + print_content_flag: true, + locale: "en" ) visit custom_page.url @@ -42,7 +44,8 @@ feature "Custom Pages" do slug: "other-slug", title: "Custom page", content: "Text for new custom page", - print_content_flag: false + print_content_flag: false, + locale: "en" ) visit custom_page.url @@ -57,7 +60,8 @@ feature "Custom Pages" do slug: "other-slug", title: "Custom page", content: "Text for new custom page", - print_content_flag: false + print_content_flag: false, + locale: "en" ) visit custom_page.url @@ -72,7 +76,8 @@ feature "Custom Pages" do custom_page = create(:site_customization_page, :published, slug: "another-slug", title: "Another custom page", subtitle: "Subtitle for custom page", - more_info_flag: true + more_info_flag: true, + locale: "en" ) visit more_info_path @@ -84,7 +89,8 @@ feature "Custom Pages" do custom_page = create(:site_customization_page, :published, slug: "another-slug", title: "Another custom page", subtitle: "Subtitle for custom page", - more_info_flag: false + more_info_flag: false, + locale: "en" ) visit more_info_path From ff4455212fffc7c804dc80e2a7054b74e4c91b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 22:14:33 +0200 Subject: [PATCH 07/12] Reverted unnecessary change --- lib/tasks/settings.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/settings.rake b/lib/tasks/settings.rake index 61c5fd5c0..29dc256bb 100644 --- a/lib/tasks/settings.rake +++ b/lib/tasks/settings.rake @@ -4,7 +4,7 @@ namespace :settings do task per_page_code_migration: :environment do per_page_code_setting = Setting.where(key: 'per_page_code').first - Setting['per_page_code_head'] = per_page_code_setting.value.to_s if Setting.where(key: 'per_page_code_head').first.blank? + Setting['per_page_code_head'] = per_page_code_setting&.value.to_s if Setting.where(key: 'per_page_code_head').first.blank? per_page_code_setting.destroy if per_page_code_setting.present? end From 67145c07719e6ee613740bd82f517202b9306317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 22:14:41 +0200 Subject: [PATCH 08/12] Added new locales --- config/locales/admin.en.yml | 1 + config/locales/admin.es.yml | 1 + config/locales/admin.fr.yml | 1 + config/locales/admin.nl.yml | 1 + 4 files changed, 4 insertions(+) diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 156decd7d..436382afa 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -927,3 +927,4 @@ en: updated_at: Updated at status_draft: Draft status_published: Published + locale: Language diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index ba66a4052..b81e32583 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -927,3 +927,4 @@ es: updated_at: Última actualización status_draft: Borrador status_published: Publicada + locale: Idioma diff --git a/config/locales/admin.fr.yml b/config/locales/admin.fr.yml index 549ddd813..5e6aaed9f 100644 --- a/config/locales/admin.fr.yml +++ b/config/locales/admin.fr.yml @@ -740,3 +740,4 @@ fr: updated_at: Mis-à-jour le status_draft: Brouillon status_published: Publié + locale: Langue diff --git a/config/locales/admin.nl.yml b/config/locales/admin.nl.yml index e401952c9..34f981f00 100755 --- a/config/locales/admin.nl.yml +++ b/config/locales/admin.nl.yml @@ -737,3 +737,4 @@ nl: updated_at: Updated at status_draft: Draft status_published: Published + locale: Taal From 6f24c68ab5b74947b64964fe3883872e27d9045f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 23:12:47 +0200 Subject: [PATCH 09/12] Improved pages index Locale can be seen as text instead of the code. --- app/views/admin/site_customization/pages/index.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/admin/site_customization/pages/index.html.erb b/app/views/admin/site_customization/pages/index.html.erb index 85c3c7fc8..a629a8a8d 100644 --- a/app/views/admin/site_customization/pages/index.html.erb +++ b/app/views/admin/site_customization/pages/index.html.erb @@ -28,7 +28,7 @@ <%= I18n.l page.created_at, format: :short %> <%= I18n.l page.created_at, format: :short %> <%= t("admin.site_customization.pages.page.status_#{page.status}") %> - <%= page.locale %> + <%= name_for_locale(page.locale) rescue nil %> <% if page.status == "published" %> From 599c805c77cd57ee847d1ed3602635b04f60e28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Sun, 2 Jul 2017 23:28:29 +0200 Subject: [PATCH 10/12] Update schema.rb --- db/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 0b9d540be..a72f6ac5c 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: 20170626081337) do +ActiveRecord::Schema.define(version: 20170702105956) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" From da2b55942a459b48e82a82908fbb798ab95a003d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Mon, 3 Jul 2017 12:53:12 +0200 Subject: [PATCH 11/12] Added specs for custom pages New test to check pages are rendered in their matching locale. --- .../site_customization/custom_pages_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/features/site_customization/custom_pages_spec.rb b/spec/features/site_customization/custom_pages_spec.rb index 640d41848..653f783cd 100644 --- a/spec/features/site_customization/custom_pages_spec.rb +++ b/spec/features/site_customization/custom_pages_spec.rb @@ -103,6 +103,25 @@ feature "Custom Pages" do expect(page).to have_selector("h1", text: "Another custom page") expect(page).to have_content("Subtitle for custom page") end + + scenario "Not listed in more information page due to different locale" do + custom_page = create(:site_customization_page, :published, + slug: "another-slug", title: "Ce texte est en français", + subtitle: "Subtitle for custom page", + more_info_flag: false, + locale: "fr" + ) + + visit more_info_path + + expect(page).to_not have_content("Ce texte est en français") + + visit custom_page.url + + expect(page).to have_title("Ce texte est en français") + expect(page).to have_selector("h1", text: "Ce texte est en français") + expect(page).to have_content("Subtitle for custom page") + end end end end From 3e2cbabc9c20aac76484ea77689280b59cddf346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Mon, 3 Jul 2017 12:55:22 +0200 Subject: [PATCH 12/12] Locales helper improvement Added a rescue block in `name_for_locale()` to ensure the application doesn't crash in case it receives an invalid locale. --- app/helpers/locales_helper.rb | 4 +++- app/views/admin/site_customization/pages/index.html.erb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/helpers/locales_helper.rb b/app/helpers/locales_helper.rb index 717be9e9b..78ab25189 100644 --- a/app/helpers/locales_helper.rb +++ b/app/helpers/locales_helper.rb @@ -3,6 +3,8 @@ module LocalesHelper def name_for_locale(locale) default = I18n.t("locale", locale: locale) I18n.backend.translate(locale, "i18n.language.name", default: default) + rescue + nil end -end \ No newline at end of file +end diff --git a/app/views/admin/site_customization/pages/index.html.erb b/app/views/admin/site_customization/pages/index.html.erb index a629a8a8d..dd9cfd3ad 100644 --- a/app/views/admin/site_customization/pages/index.html.erb +++ b/app/views/admin/site_customization/pages/index.html.erb @@ -28,7 +28,7 @@ <%= I18n.l page.created_at, format: :short %> <%= I18n.l page.created_at, format: :short %> <%= t("admin.site_customization.pages.page.status_#{page.status}") %> - <%= name_for_locale(page.locale) rescue nil %> + <%= name_for_locale(page.locale) %> <% if page.status == "published" %>