From 05bfa193cdc55433a5b099887f505d1d02463cb6 Mon Sep 17 00:00:00 2001 From: Marko Lovic Date: Sun, 12 Aug 2018 21:43:47 +0200 Subject: [PATCH] Make Legislation Draft Versions translatable --- .../javascripts/markdown_editor.js.coffee | 24 +-- .../legislation/draft_versions_controller.rb | 13 +- app/models/legislation/draft_version.rb | 14 ++ .../legislation/draft_versions/_form.html.erb | 17 +- .../admin/legislation/draft_versions_spec.rb | 146 +++++++++++++++++- spec/models/legislation/draft_version_spec.rb | 9 ++ 6 files changed, 201 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/markdown_editor.js.coffee b/app/assets/javascripts/markdown_editor.js.coffee index 510bf83ec..29e74e51c 100644 --- a/app/assets/javascripts/markdown_editor.js.coffee +++ b/app/assets/javascripts/markdown_editor.js.coffee @@ -1,10 +1,15 @@ App.MarkdownEditor = refresh_preview: (element, md) -> - textarea_content = element.find('textarea').val() + textarea_content = App.MarkdownEditor.find_textarea(element).val() result = md.render(textarea_content) element.find('#markdown-preview').html(result) + # Multi-locale (translatable) form fields work by hiding inputs of locales + # which are not "active". + find_textarea: (editor) -> + editor.find('textarea:visible') + initialize: -> $('.markdown-editor').each -> md = window.markdownit({ @@ -13,18 +18,18 @@ App.MarkdownEditor = typographer: true, }) - App.MarkdownEditor.refresh_preview($(this), md) + editor = $(this) - $(this).on 'change input paste keyup', -> + editor.on 'input', -> App.MarkdownEditor.refresh_preview($(this), md) $('.legislation-draft-versions-edit .warning').show() return - $(this).find('textarea').on 'scroll', -> + editor.find('textarea').on 'scroll', -> $('#markdown-preview').scrollTop($(this).scrollTop()) - $(this).find('.fullscreen-toggle').on 'click', -> - $('.markdown-editor').toggleClass('fullscreen') + editor.find('.fullscreen-toggle').on 'click', -> + editor.toggleClass('fullscreen') $('.fullscreen-container').toggleClass('medium-8', 'medium-12') span = $(this).find('span') current_html = span.html() @@ -33,7 +38,8 @@ App.MarkdownEditor = else span.html(span.data('open-text')) - if $('.markdown-editor').hasClass('fullscreen') - $('.markdown-editor textarea').height($(window).height() - 100) + if editor.hasClass('fullscreen') + App.MarkdownEditor.find_textarea(editor).height($(window).height() - 100) + App.MarkdownEditor.refresh_preview(editor, md) else - $('.markdown-editor textarea').height("10em") + App.MarkdownEditor.find_textarea(editor).height("10em") diff --git a/app/controllers/admin/legislation/draft_versions_controller.rb b/app/controllers/admin/legislation/draft_versions_controller.rb index 4e7ae18a8..703d8a543 100644 --- a/app/controllers/admin/legislation/draft_versions_controller.rb +++ b/app/controllers/admin/legislation/draft_versions_controller.rb @@ -1,6 +1,8 @@ class Admin::Legislation::DraftVersionsController < Admin::Legislation::BaseController - load_and_authorize_resource :process, class: "Legislation::Process" - load_and_authorize_resource :draft_version, class: "Legislation::DraftVersion", through: :process + include Translatable + + load_and_authorize_resource :draft_version, class: "Legislation::DraftVersion", through: :process, prepend: true + load_and_authorize_resource :process, class: "Legislation::Process", prepend: true def index @draft_versions = @process.draft_versions @@ -44,7 +46,12 @@ class Admin::Legislation::DraftVersionsController < Admin::Legislation::BaseCont :status, :final_version, :body, - :body_html + :body_html, + *translation_params(Legislation::DraftVersion) ) end + + def resource + @draft_version + end end diff --git a/app/models/legislation/draft_version.rb b/app/models/legislation/draft_version.rb index 7b9e6a0aa..14bc210c8 100644 --- a/app/models/legislation/draft_version.rb +++ b/app/models/legislation/draft_version.rb @@ -4,6 +4,13 @@ class Legislation::DraftVersion < ActiveRecord::Base acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases + translates :title, touch: true + translates :changelog, touch: true + translates :body, touch: true + translates :body_html, touch: true + translates :toc_html, touch: true + globalize_accessors locales: [:en, :es, :fr, :nl, :val, :pt_br] + belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id' has_many :annotations, class_name: 'Legislation::Annotation', foreign_key: 'legislation_draft_version_id', dependent: :destroy @@ -21,6 +28,13 @@ class Legislation::DraftVersion < ActiveRecord::Base self.body_html = Redcarpet::Markdown.new(renderer).render(body) self.toc_html = Redcarpet::Markdown.new(toc_renderer).render(body) + + translations.each do |translation| + if translation.body_changed? + translation.body_html = Redcarpet::Markdown.new(renderer).render(translation.body) + translation.toc_html = Redcarpet::Markdown.new(toc_renderer).render(translation.body) + end + end end def display_title diff --git a/app/views/admin/legislation/draft_versions/_form.html.erb b/app/views/admin/legislation/draft_versions/_form.html.erb index 45c04f3a6..672f1b119 100644 --- a/app/views/admin/legislation/draft_versions/_form.html.erb +++ b/app/views/admin/legislation/draft_versions/_form.html.erb @@ -1,4 +1,6 @@ -<%= form_for [:admin, @process, @draft_version], url: url, html: {data: {watch_changes: true}} do |f| %> +<%= render "admin/shared/globalize_locales", resource: @draft_version %> + +<%= translatable_form_for [:admin, @process, @draft_version], url: url, html: {data: {watch_changes: true}} do |f| %> <% if @draft_version.errors.any? %>
@@ -14,14 +16,17 @@ <% end %>
- <%= f.label :title %> - <%= f.text_field :title, label: false, placeholder: t("admin.legislation.draft_versions.form.title_placeholder") %> + <%= f.translatable_text_field :title, + placeholder: t("admin.legislation.draft_versions.form.title_placeholder") %>
<%= f.label :changelog %> <%= t("admin.legislation.draft_versions.form.use_markdown") %> - <%= f.text_area :changelog, label: false, rows: 5, placeholder: t("admin.legislation.draft_versions.form.changelog_placeholder") %> + <%= f.translatable_text_area :changelog, + label: false, + rows: 5, + placeholder: t("admin.legislation.draft_versions.form.changelog_placeholder") %>
@@ -65,7 +70,9 @@ <% end %>
- <%= f.text_area :body, label: false, placeholder: t("admin.legislation.draft_versions.form.body_placeholder") %> + <%= f.translatable_text_area :body, + label: false, + placeholder: t("admin.legislation.draft_versions.form.body_placeholder") %>
diff --git a/spec/features/admin/legislation/draft_versions_spec.rb b/spec/features/admin/legislation/draft_versions_spec.rb index 9bb07f9a3..9ea09bc49 100644 --- a/spec/features/admin/legislation/draft_versions_spec.rb +++ b/spec/features/admin/legislation/draft_versions_spec.rb @@ -7,6 +7,11 @@ feature 'Admin legislation draft versions' do login_as(admin.user) end + it_behaves_like "translatable", + "legislation_draft_version", + "edit_admin_legislation_process_draft_version_path", + %w[title changelog] + context "Feature flag" do scenario 'Disabled with a feature flag' do @@ -53,9 +58,9 @@ feature 'Admin legislation draft versions' do click_link 'Create version' - fill_in 'legislation_draft_version_title', with: 'Version 3' - fill_in 'legislation_draft_version_changelog', with: 'Version 3 changes' - fill_in 'legislation_draft_version_body', with: 'Version 3 body' + fill_in 'legislation_draft_version_title_en', with: 'Version 3' + fill_in 'legislation_draft_version_changelog_en', with: 'Version 3 changes' + fill_in 'legislation_draft_version_body_en', with: 'Version 3 body' within('.end') do click_button 'Create version' @@ -86,11 +91,11 @@ feature 'Admin legislation draft versions' do click_link 'Version 1' - fill_in 'legislation_draft_version_title', with: 'Version 1b' + fill_in 'legislation_draft_version_title_en', with: 'Version 1b' click_link 'Launch text editor' - fill_in 'legislation_draft_version_body', with: '# Version 1 body\r\n\r\nParagraph\r\n\r\n>Quote' + fill_in 'legislation_draft_version_body_en', with: '# Version 1 body\r\n\r\nParagraph\r\n\r\n>Quote' within('.fullscreen') do click_link 'Close text editor' @@ -101,4 +106,135 @@ feature 'Admin legislation draft versions' do expect(page).to have_content 'Version 1b' end end + + context "Translations" do + + let!(:draft_version) { create(:legislation_draft_version, + title_en: "Title in English", + title_es: "Título en Español", + changelog_en: "Changes in English", + changelog_es: "Cambios en Español", + body_en: "Body in English", + body_es: "Texto en Español") } + + before do + @edit_draft_version_url = edit_admin_legislation_process_draft_version_path(draft_version.process, draft_version) + end + + scenario "Add a translation", :js do + visit @edit_draft_version_url + + select "Français", from: "translation_locale" + fill_in 'legislation_draft_version_title_fr', with: 'Titre en Français' + + click_button 'Save changes' + + visit @edit_draft_version_url + expect(page).to have_field('legislation_draft_version_title_en', with: 'Title in English') + + click_link "Español" + expect(page).to have_field('legislation_draft_version_title_es', with: 'Título en Español') + + click_link "Français" + expect(page).to have_field('legislation_draft_version_title_fr', with: 'Titre en Français') + end + + scenario "Update a translation", :js do + draft_version.update!(status: 'published') + draft_version.process.update!(title_es: 'Título de proceso') + + visit @edit_draft_version_url + + click_link "Español" + fill_in 'legislation_draft_version_title_es', with: 'Título correcto en Español' + + click_button 'Save changes' + + visit legislation_process_draft_version_path(draft_version.process, draft_version) + + expect(page).to have_content("Title in English") + + select('Español', from: 'locale-switcher') + + expect(page).to have_content('Título correcto en Español') + end + + scenario "Remove a translation", :js do + visit @edit_draft_version_url + + click_link "Español" + click_link "Remove language" + + expect(page).not_to have_link "Español" + + click_button "Save changes" + visit @edit_draft_version_url + expect(page).not_to have_link "Español" + end + + scenario 'Add translation through markup editor', :js do + visit @edit_draft_version_url + + select "Français", from: "translation_locale" + + click_link 'Launch text editor' + + fill_in 'legislation_draft_version_body_fr', with: 'Texte en Français' + + click_link 'Close text editor' + click_button "Save changes" + + visit @edit_draft_version_url + + click_link "Français" + click_link 'Launch text editor' + + expect(page).to have_field('legislation_draft_version_body_fr', with: 'Texte en Français') + end + + context "Globalize javascript interface" do + + scenario "Highlight current locale", :js do + visit @edit_draft_version_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_draft_version_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_draft_version_url + + expect(page).to have_field('legislation_draft_version_title_en', with: 'Title in English') + + click_link "Español" + + expect(page).to have_field('legislation_draft_version_title_es', with: 'Título en Español') + end + + scenario "Select a locale and add it to the draft_version form", :js do + visit @edit_draft_version_url + + select "Français", from: "translation_locale" + + expect(page).to have_link "Français" + + click_link "Français" + + expect(page).to have_field('legislation_draft_version_title_fr') + end + end + end end diff --git a/spec/models/legislation/draft_version_spec.rb b/spec/models/legislation/draft_version_spec.rb index 1b0427a6c..d88cdcee6 100644 --- a/spec/models/legislation/draft_version_spec.rb +++ b/spec/models/legislation/draft_version_spec.rb @@ -16,6 +16,15 @@ RSpec.describe Legislation::DraftVersion, type: :model do expect(legislation_draft_version.toc_html).to eq(toc_html) end + it "renders and saves the html from the markdown body field with alternative translation" do + legislation_draft_version.body_fr = body_markdown + + legislation_draft_version.save! + + expect(legislation_draft_version.body_html_fr).to eq(body_html) + expect(legislation_draft_version.toc_html_fr).to eq(toc_html) + end + def body_markdown <<-BODY_MARKDOWN # Title 1